Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

DEVPROD-5762 Replace all hardcoded slugs with an enum slug and consolidate slug name to associated resource #2312

Closed
wants to merge 35 commits into from

Conversation

khelif96
Copy link
Contributor

@khelif96 khelif96 commented Mar 21, 2024

DEVPROD-5762

Description

This should not break any existing pages or urls and only serves to consolidate the slug names we use internally in the Spruce application.

  • Create Slugs for each existing hardcoded slug.
  • Update generic slugs so they correspond with the associated resource.
  • Update the usage of hardcoded slugs with their enum slug equivalent

Testing

Existing tests should suffice to catch any edge cases that could have been missed.

@khelif96 khelif96 changed the title DEVPROD-5762 Replace all hardcoded slugs with an enum slug and consolidate resource name to corresponding slug DEVPROD-5762 Replace all hardcoded slugs with an enum slug and consolidate slug name to associated resource Mar 21, 2024
@khelif96 khelif96 marked this pull request as ready for review March 22, 2024 14:09
@khelif96 khelif96 requested a review from a team March 22, 2024 14:22
Copy link
Contributor

@SupaJoon SupaJoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the explicit typing in useParams without the use of slug variables. When a slug changes, it will be super easy to see which useParams are affected because of type errors. These kinds of changes wouldn't have been easily possible before slugs.

* @param matchedPathParams - Params object from react-router-dom's matchPath
* @returns string | undefined
*/
const slugToId = (matchedPathParams: Params): string | undefined => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code could potentially save tab, taskName, variantName as id based on the order of Object.keys(slugs). If you move forward with this abstraction you should create a separate set of slugs that represent ids. Since the URLs in the legacy UI are done coded and complete, I think this abstraction is overkill.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on this I updated it so that non id slugs are no longer included in the check. I preferred this approach over the previous implementation of just casing on every single slug.

@@ -38,7 +38,7 @@ import { NameChangeModal } from "./version/NameChangeModal";
// docs/decisions/2023-12-13_version_page_logic.md
export const VersionPage: React.FC = () => {
const spruceConfig = useSpruceConfig();
const { id } = useParams<{ id: string }>();
const { [slugs.versionId]: id } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { [slugs.versionId]: id } = useParams();
const { [slugs.versionId]: versionId } = useParams();

@@ -33,7 +34,7 @@ const { getLimitFromSearch, getPageFromSearch } = url;

const Host: React.FC = () => {
const dispatchToast = useToastContext();
const { id } = useParams<{ id: string }>();
const { [slugs.hostId]: id } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { [slugs.hostId]: id } = useParams();
const { [slugs.hostId]: hostId } = useParams();

}>();
const { [slugs.projectIdentifier]: identifier, [slugs.tab]: tab } =
useParams<{
[slugs.projectIdentifier]: string | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious if | null is required here:

Suggested change
[slugs.projectIdentifier]: string | null;
[slugs.projectIdentifier]: string;

@@ -27,7 +27,7 @@ import TaskQueueTable from "./TaskQueueTable";
const TaskQueue = () => {
const taskQueueAnalytics = useTaskQueueAnalytics();

const { distro } = useParams<{ distro: string }>();
const { [slugs.distro]: distro } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we should just make TaskQueue use the distroId slug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea we can consolidate them

src/pages/version/VersionTabs.tsx Outdated Show resolved Hide resolved
import { usePageTitle } from "hooks";
import { PreferencesTabs } from "pages/preferences/PreferencesTabs";

const Preferences: React.FC = () => {
usePageTitle("Preferences");
const { tab } = useParams<{ tab: string }>();
const { [slugs.tab]: tab } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

import { SpawnHost } from "./SpawnHost";
import { SpawnVolume } from "./SpawnVolume";

const Spawn: React.FC = () => {
const { tab } = useParams<{ tab: string }>();
const { [slugs.tab]: tab } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can add more type specificity with <{ tab: SpawnTab }>

@@ -109,9 +109,9 @@ export const VersionTabs: React.FC<Props> = ({
taskCount,
childPatches,
numFailedChildPatches,
patchId: id,
patchId: versionId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing this parameter to be called versionId might make this a bit less confusing to read

hosts: paths.hosts,
jobLogs: `${paths.jobLogs}/:buildId`,
jobLogs: `${paths.jobLogs}/:${slugs.buildId}`,
jobLogsGroup: `${paths.jobLogs}/:${slugs.taskId}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does this one come from? is taskId the correct parameter rather than something like groupId?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch this is from my job logs pr I must have mixed up the commits

login: paths.login,
myPatches: `${paths.user}/${PageNames.Patches}`,
patch: `${paths.patch}/:id`,
patch: `${paths.patch}/:${slugs.versionId}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be patchId? (not sure, just guessing since it seems odd)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah this page should not exist it looks like we have 2 version pages with 2 urls

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i saw you tried deleting it but it didn't work, just curious what you found out this page is for)

@@ -152,7 +152,7 @@ export const useConfigurePatch = (
): HookResult => {
const navigate = useNavigate();
const location = useLocation();
const { tab } = useParams<{ tab: PatchTab | null }>();
const { [slugs.tab]: tab } = useParams<{ [slugs.tab]: PatchTab | null }>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tab will be undefined if the query param doesn't exist

@@ -125,7 +125,7 @@ interface HookResult extends ConfigurePatchState {
const useConfigurePatch = (patch: ConfigurePatchQuery["patch"]): HookResult => {
const navigate = useNavigate();
const location = useLocation();
const { tab } = useParams<{ tab: PatchTab | null }>();
const { [slugs.tab]: tab } = useParams();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump!

taskQueue: paths.taskQueue,
task: `${paths.task}/:${slugs.taskId}`,
taskHistory: `${paths.taskHistory}/:${slugs.projectIdentifier}/:${slugs.taskName}`,
taskQueue: `${paths.taskQueue}`,
Copy link
Contributor

@minnakt minnakt Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
taskQueue: `${paths.taskQueue}`,
taskQueue: paths.taskQueue,

login: paths.login,
myPatches: `${paths.user}/${PageNames.Patches}`,
patch: `${paths.patch}/:id`,
patch: `${paths.patch}/:${slugs.versionId}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i saw you tried deleting it but it didn't work, just curious what you found out this page is for)

@minnakt
Copy link
Contributor

minnakt commented Mar 28, 2024

Closing this PR since it has already been moved to the UI repo

@minnakt minnakt closed this Mar 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants