Skip to content

Commit 8ad3a18

Browse files
committed
chore: remove global organization id state
1 parent 166467c commit 8ad3a18

File tree

26 files changed

+170
-139
lines changed

26 files changed

+170
-139
lines changed

site/src/api/api.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ const BASE_CONTENT_TYPE_JSON = {
300300
"Content-Type": "application/json",
301301
} as const satisfies HeadersInit;
302302

303-
type TemplateOptions = Readonly<{
303+
export type GetTemplateOptions = Readonly<{
304304
readonly deprecated?: boolean;
305305
}>;
306306

@@ -625,12 +625,31 @@ class ApiMethods {
625625
return response.data;
626626
};
627627

628+
getTemplates = async (
629+
options?: GetTemplateOptions,
630+
): Promise<TypesGen.Template[]> => {
631+
const params: Record<string, string> = {};
632+
if (options?.deprecated !== undefined) {
633+
// Just want to check if it isn't undefined. If it has
634+
// a boolean value, convert it to a string and include
635+
// it as a param.
636+
params["deprecated"] = String(options.deprecated);
637+
}
638+
639+
const response = await this.axios.get<TypesGen.Template[]>(
640+
`/api/v2/templates`,
641+
{ params },
642+
);
643+
644+
return response.data;
645+
};
646+
628647
/**
629648
* @param organization Can be the organization's ID or name
630649
*/
631-
getTemplates = async (
650+
getTemplatesByOrganization = async (
632651
organization: string,
633-
options?: TemplateOptions,
652+
options?: GetTemplateOptions,
634653
): Promise<TypesGen.Template[]> => {
635654
const params: Record<string, string> = {};
636655
if (options?.deprecated !== undefined) {

site/src/api/queries/templates.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MutationOptions, QueryClient, QueryOptions } from "react-query";
2-
import { API } from "api/api";
2+
import { API, GetTemplateOptions } from "api/api";
33
import type {
44
CreateTemplateRequest,
55
CreateTemplateVersionRequest,
@@ -38,16 +38,30 @@ export const templateByName = (
3838
};
3939
};
4040

41-
const getTemplatesQueryKey = (organizationId: string, deprecated?: boolean) => [
42-
organizationId,
41+
const getTemplatesQueryKey = (options?: GetTemplateOptions) => [
4342
"templates",
44-
deprecated,
43+
options?.deprecated,
4544
];
4645

47-
export const templates = (organizationId: string, deprecated?: boolean) => {
46+
export const templates = (options?: GetTemplateOptions) => {
4847
return {
49-
queryKey: getTemplatesQueryKey(organizationId, deprecated),
50-
queryFn: () => API.getTemplates(organizationId, { deprecated }),
48+
queryKey: getTemplatesQueryKey(options),
49+
queryFn: () => API.getTemplates(options),
50+
};
51+
};
52+
53+
const getTemplatesByOrganizationQueryKey = (
54+
organization: string,
55+
options?: GetTemplateOptions,
56+
) => [organization, "templates", options?.deprecated];
57+
58+
export const templatesByOrganization = (
59+
organization: string,
60+
options: GetTemplateOptions = {},
61+
) => {
62+
return {
63+
queryKey: getTemplatesByOrganizationQueryKey(organization, options),
64+
queryFn: () => API.getTemplatesByOrganization(organization, options),
5165
};
5266
};
5367

@@ -100,7 +114,10 @@ export const setGroupRole = (
100114

101115
export const templateExamples = (organizationId: string) => {
102116
return {
103-
queryKey: [...getTemplatesQueryKey(organizationId), "examples"],
117+
queryKey: [
118+
...getTemplatesByOrganizationQueryKey(organizationId),
119+
"examples",
120+
],
104121
queryFn: () => API.getTemplateExamples(organizationId),
105122
};
106123
};

site/src/modules/dashboard/DashboardProvider.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ import type {
77
AppearanceConfig,
88
Entitlements,
99
Experiments,
10+
Organization,
1011
} from "api/typesGenerated";
1112
import { Loader } from "components/Loader/Loader";
1213
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
14+
import { organizations } from "api/queries/organizations";
15+
import { ErrorAlert } from "components/Alert/ErrorAlert";
1316

1417
export interface DashboardValue {
15-
/**
16-
* @deprecated Do not add new usage of this value. It is being removed as part
17-
* of the multi-org work.
18-
*/
19-
organizationId: string;
2018
entitlements: Entitlements;
2119
experiments: Experiments;
2220
appearance: AppearanceConfig;
21+
organizations: Organization[];
2322
}
2423

2524
export const DashboardContext = createContext<DashboardValue | undefined>(
@@ -31,9 +30,23 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
3130
const entitlementsQuery = useQuery(entitlements(metadata.entitlements));
3231
const experimentsQuery = useQuery(experiments(metadata.experiments));
3332
const appearanceQuery = useQuery(appearance(metadata.appearance));
33+
const organizationsQuery = useQuery(organizations());
34+
35+
const error =
36+
entitlementsQuery.error ||
37+
appearanceQuery.error ||
38+
experimentsQuery.error ||
39+
organizationsQuery.error;
40+
41+
if (error) {
42+
return <ErrorAlert error={error} />;
43+
}
3444

3545
const isLoading =
36-
!entitlementsQuery.data || !appearanceQuery.data || !experimentsQuery.data;
46+
!entitlementsQuery.data ||
47+
!appearanceQuery.data ||
48+
!experimentsQuery.data ||
49+
!organizationsQuery.data;
3750

3851
if (isLoading) {
3952
return <Loader fullscreen />;
@@ -42,10 +55,10 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
4255
return (
4356
<DashboardContext.Provider
4457
value={{
45-
organizationId: "00000000-0000-0000-0000-000000000000",
4658
entitlements: entitlementsQuery.data,
4759
experiments: experimentsQuery.data,
4860
appearance: appearanceQuery.data,
61+
organizations: organizationsQuery.data,
4962
}}
5063
>
5164
{children}

site/src/modules/navigation.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ export const linkToUsers = withFilter("/users", "status:active");
2727

2828
export const linkToTemplate =
2929
(organizationName: string, templateName: string): LinkThunk =>
30-
(dashboard) =>
31-
dashboard.experiments.includes("multi-organization") &&
32-
selectFeatureVisibility(dashboard.entitlements).multiple_organizations
30+
(dashboard) => {
31+
const hasMultipleOrganizations = dashboard.organizations.length > 1;
32+
const organizationsEnabled =
33+
dashboard.experiments.includes("multi-organization") &&
34+
selectFeatureVisibility(dashboard.entitlements).multiple_organizations;
35+
36+
return hasMultipleOrganizations || organizationsEnabled
3337
? `/templates/${organizationName}/${templateName}`
3438
: `/templates/${templateName}`;
39+
};

site/src/pages/CreateUserPage/CreateUserForm.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export interface CreateUserFormProps {
6565
onCancel: () => void;
6666
error?: unknown;
6767
isLoading: boolean;
68-
organizationId: string;
6968
authMethods?: TypesGen.AuthMethods;
7069
}
7170

@@ -86,15 +85,15 @@ const validationSchema = Yup.object({
8685

8786
export const CreateUserForm: FC<
8887
React.PropsWithChildren<CreateUserFormProps>
89-
> = ({ onSubmit, onCancel, error, isLoading, organizationId, authMethods }) => {
88+
> = ({ onSubmit, onCancel, error, isLoading, authMethods }) => {
9089
const form: FormikContextType<TypesGen.CreateUserRequest> =
9190
useFormik<TypesGen.CreateUserRequest>({
9291
initialValues: {
9392
email: "",
9493
password: "",
9594
username: "",
9695
name: "",
97-
organization_id: organizationId,
96+
organization_id: "default",
9897
disable_login: false,
9998
login_type: "",
10099
},

site/src/pages/CreateUserPage/CreateUserPage.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useNavigate } from "react-router-dom";
55
import { authMethods, createUser } from "api/queries/users";
66
import { displaySuccess } from "components/GlobalSnackbar/utils";
77
import { Margins } from "components/Margins/Margins";
8-
import { useDashboard } from "modules/dashboard/useDashboard";
98
import { pageTitle } from "utils/page";
109
import { CreateUserForm } from "./CreateUserForm";
1110

@@ -14,7 +13,6 @@ export const Language = {
1413
};
1514

1615
export const CreateUserPage: FC = () => {
17-
const { organizationId } = useDashboard();
1816
const navigate = useNavigate();
1917
const queryClient = useQueryClient();
2018
const createUserMutation = useMutation(createUser(queryClient));
@@ -38,7 +36,6 @@ export const CreateUserPage: FC = () => {
3836
navigate("..", { relative: "path" });
3937
}}
4038
isLoading={createUserMutation.isLoading}
41-
organizationId={organizationId}
4239
/>
4340
</Margins>
4441
);

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ export type CreateWorkspaceMode = (typeof createWorkspaceModes)[number];
3333
export type ExternalAuthPollingState = "idle" | "polling" | "abandoned";
3434

3535
const CreateWorkspacePage: FC = () => {
36-
const { template: templateName } = useParams() as {
37-
template: string;
38-
};
36+
const { organization: organizationName = "default", template: templateName } =
37+
useParams() as {
38+
organization?: string;
39+
template: string;
40+
};
3941
const { user: me } = useAuthenticated();
4042
const navigate = useNavigate();
4143
const [searchParams] = useSearchParams();
42-
const { experiments, organizationId } = useDashboard();
44+
const { experiments } = useDashboard();
4345

4446
const customVersionId = searchParams.get("version") ?? undefined;
4547
const defaultName = searchParams.get("name");
@@ -54,15 +56,19 @@ const CreateWorkspacePage: FC = () => {
5456
);
5557
const createWorkspaceMutation = useMutation(createWorkspace(queryClient));
5658

57-
const templateQuery = useQuery(templateByName(organizationId, templateName));
58-
59+
const templateQuery = useQuery(
60+
templateByName(organizationName, templateName),
61+
);
5962
const permissionsQuery = useQuery(
60-
checkAuthorization({
61-
checks: createWorkspaceChecks(organizationId),
62-
}),
63+
templateQuery.data
64+
? checkAuthorization({
65+
checks: createWorkspaceChecks(templateQuery.data.organization_id),
66+
})
67+
: { enabled: false },
6368
);
6469
const realizedVersionId =
6570
customVersionId ?? templateQuery.data?.active_version_id;
71+
const organizationId = templateQuery.data?.organization_id;
6672
const richParametersQuery = useQuery({
6773
...richParameters(realizedVersionId ?? ""),
6874
enabled: realizedVersionId !== undefined,
@@ -110,7 +116,7 @@ const CreateWorkspacePage: FC = () => {
110116

111117
const autoCreationStartedRef = useRef(false);
112118
const automateWorkspaceCreation = useEffectEvent(async () => {
113-
if (autoCreationStartedRef.current) {
119+
if (autoCreationStartedRef.current || !organizationId) {
114120
return;
115121
}
116122

site/src/pages/GroupsPage/GroupsPage.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import { getErrorMessage } from "api/errors";
55
import { groups } from "api/queries/groups";
66
import { displayError } from "components/GlobalSnackbar/utils";
77
import { useAuthenticated } from "contexts/auth/RequireAuth";
8-
import { useDashboard } from "modules/dashboard/useDashboard";
98
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
109
import { pageTitle } from "utils/page";
1110
import GroupsPageView from "./GroupsPageView";
1211

1312
export const GroupsPage: FC = () => {
1413
const { permissions } = useAuthenticated();
15-
const { organizationId } = useDashboard();
1614
const { createGroup: canCreateGroup } = permissions;
1715
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility();
18-
const groupsQuery = useQuery(groups(organizationId));
16+
const groupsQuery = useQuery(groups("default"));
1917

2018
useEffect(() => {
2119
if (groupsQuery.error) {

site/src/pages/ManagementSettingsPage/GroupsPage/GroupPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { pageTitle } from "utils/page";
5151

5252
export const GroupPage: FC = () => {
5353
const { organization = "default", groupName } = useParams() as {
54-
organization: string;
54+
organization?: string;
5555
groupName: string;
5656
};
5757
const queryClient = useQueryClient();

site/src/pages/ManagementSettingsPage/GroupsPage/GroupSettingsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import GroupSettingsPageView from "./GroupSettingsPageView";
1212

1313
export const GroupSettingsPage: FC = () => {
1414
const { organization = "default", groupName } = useParams() as {
15-
organization: string;
15+
organization?: string;
1616
groupName: string;
1717
};
1818
const queryClient = useQueryClient();

site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const GroupsPage: FC = () => {
3030
} = useFeatureVisibility();
3131
const { experiments } = useDashboard();
3232
const location = useLocation();
33-
const { organization = "default" } = useParams() as { organization: string };
33+
const { organization = "default" } = useParams() as { organization?: string };
3434
const groupsQuery = useQuery(groups(organization));
3535
const { organizations } = useOrganizationSettings();
3636

0 commit comments

Comments
 (0)