Skip to content

Commit 80cbffe

Browse files
authored
chore: remove organizationIds from AuthProvider (coder#13917)
1 parent f21f2dc commit 80cbffe

File tree

6 files changed

+10
-39
lines changed

6 files changed

+10
-39
lines changed

site/src/contexts/auth/AuthProvider.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export type AuthContextValue = {
3030
isUpdatingProfile: boolean;
3131
user: User | undefined;
3232
permissions: Permissions | undefined;
33-
organizationIds: readonly string[] | undefined;
3433
signInError: unknown;
3534
updateProfileError: unknown;
3635
signOut: () => void;
@@ -119,7 +118,6 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
119118
permissions: permissionsQuery.data as Permissions | undefined,
120119
signInError: loginMutation.error,
121120
updateProfileError: updateProfileMutation.error,
122-
organizationIds: userQuery.data?.organization_ids,
123121
}}
124122
>
125123
{children}

site/src/contexts/auth/RequireAuth.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ describe("useAuthenticated", () => {
9595
wrapper: createAuthWrapper({
9696
user: MockUser,
9797
permissions: MockPermissions,
98-
organizationIds: [],
9998
}),
10099
});
101100
}).not.toThrow();

site/src/contexts/auth/RequireAuth.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type RequireKeys<T, R extends keyof T> = Omit<T, R> & {
7474
// values are not undefined when authenticated
7575
type AuthenticatedAuthContextValue = RequireKeys<
7676
AuthContextValue,
77-
"user" | "permissions" | "organizationIds"
77+
"user" | "permissions"
7878
>;
7979

8080
export const useAuthenticated = (): AuthenticatedAuthContextValue => {
@@ -88,9 +88,5 @@ export const useAuthenticated = (): AuthenticatedAuthContextValue => {
8888
throw new Error("Permissions are not available.");
8989
}
9090

91-
if (!auth.organizationIds) {
92-
throw new Error("Organization ID is not available.");
93-
}
94-
9591
return auth as AuthenticatedAuthContextValue;
9692
};

site/src/modules/dashboard/DashboardProvider.tsx

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
createContext,
3-
type FC,
4-
type PropsWithChildren,
5-
useState,
6-
} from "react";
1+
import { createContext, type FC, type PropsWithChildren } from "react";
72
import { useQuery } from "react-query";
83
import { appearance } from "api/queries/appearance";
94
import { entitlements } from "api/queries/entitlements";
@@ -15,12 +10,14 @@ import type {
1510
} from "api/typesGenerated";
1611
import { Loader } from "components/Loader/Loader";
1712
import { useAuthenticated } from "contexts/auth/RequireAuth";
18-
import { useEffectEvent } from "hooks/hookPolyfills";
1913
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
2014

2115
export interface DashboardValue {
16+
/**
17+
* @deprecated Do not add new usage of this value. It is being removed as part
18+
* of the multi-org work.
19+
*/
2220
organizationId: string;
23-
setOrganizationId: (id: string) => void;
2421
entitlements: Entitlements;
2522
experiments: Experiments;
2623
appearance: AppearanceConfig;
@@ -32,40 +29,22 @@ export const DashboardContext = createContext<DashboardValue | undefined>(
3229

3330
export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
3431
const { metadata } = useEmbeddedMetadata();
35-
const { user, organizationIds } = useAuthenticated();
32+
const { user } = useAuthenticated();
3633
const entitlementsQuery = useQuery(entitlements(metadata.entitlements));
3734
const experimentsQuery = useQuery(experiments(metadata.experiments));
3835
const appearanceQuery = useQuery(appearance(metadata.appearance));
3936

4037
const isLoading =
4138
!entitlementsQuery.data || !appearanceQuery.data || !experimentsQuery.data;
4239

43-
const lastUsedOrganizationId = localStorage.getItem(
44-
`user:${user.id}.lastUsedOrganizationId`,
45-
);
46-
const [activeOrganizationId, setActiveOrganizationId] = useState(() =>
47-
lastUsedOrganizationId && organizationIds.includes(lastUsedOrganizationId)
48-
? lastUsedOrganizationId
49-
: organizationIds[0],
50-
);
51-
52-
const setOrganizationId = useEffectEvent((id: string) => {
53-
if (!organizationIds.includes(id)) {
54-
throw new ReferenceError("Invalid organization ID");
55-
}
56-
localStorage.setItem(`user:${user.id}.lastUsedOrganizationId`, id);
57-
setActiveOrganizationId(id);
58-
});
59-
6040
if (isLoading) {
6141
return <Loader fullscreen />;
6242
}
6343

6444
return (
6545
<DashboardContext.Provider
6646
value={{
67-
organizationId: activeOrganizationId,
68-
setOrganizationId: setOrganizationId,
47+
organizationId: user.organization_ids[0] ?? "default",
6948
entitlements: entitlementsQuery.data,
7049
experiments: experimentsQuery.data,
7150
appearance: appearanceQuery.data,

site/src/pages/ManagementSettingsPage/ManagementSettingsLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const useOrganizationSettings = (): OrganizationSettingsContextValue => {
3535

3636
export const ManagementSettingsLayout: FC = () => {
3737
const location = useLocation();
38-
const { permissions, organizationIds } = useAuthenticated();
38+
const { permissions } = useAuthenticated();
3939
const { experiments } = useDashboard();
4040
const { organization } = useParams() as { organization: string };
4141
const deploymentConfigQuery = useQuery(deploymentConfig());
@@ -61,7 +61,7 @@ export const ManagementSettingsLayout: FC = () => {
6161
currentOrganizationId: !inOrganizationSettings
6262
? undefined
6363
: !organization
64-
? organizationIds[0]
64+
? organizationsQuery.data[0]?.id
6565
: organizationsQuery.data.find(
6666
(org) => org.name === organization,
6767
)?.id,

site/src/testHelpers/storybook.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const withDashboardProvider = (
2727
<DashboardContext.Provider
2828
value={{
2929
organizationId: "",
30-
setOrganizationId: () => {},
3130
entitlements,
3231
experiments,
3332
appearance: MockAppearanceConfig,

0 commit comments

Comments
 (0)