Skip to content

chore: remove organizationIds from AuthProvider #13917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions site/src/contexts/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export type AuthContextValue = {
isUpdatingProfile: boolean;
user: User | undefined;
permissions: Permissions | undefined;
organizationIds: readonly string[] | undefined;
signInError: unknown;
updateProfileError: unknown;
signOut: () => void;
Expand Down Expand Up @@ -119,7 +118,6 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
permissions: permissionsQuery.data as Permissions | undefined,
signInError: loginMutation.error,
updateProfileError: updateProfileMutation.error,
organizationIds: userQuery.data?.organization_ids,
}}
>
{children}
Expand Down
1 change: 0 additions & 1 deletion site/src/contexts/auth/RequireAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe("useAuthenticated", () => {
wrapper: createAuthWrapper({
user: MockUser,
permissions: MockPermissions,
organizationIds: [],
}),
});
}).not.toThrow();
Expand Down
6 changes: 1 addition & 5 deletions site/src/contexts/auth/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type RequireKeys<T, R extends keyof T> = Omit<T, R> & {
// values are not undefined when authenticated
type AuthenticatedAuthContextValue = RequireKeys<
AuthContextValue,
"user" | "permissions" | "organizationIds"
"user" | "permissions"
>;

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

if (!auth.organizationIds) {
throw new Error("Organization ID is not available.");
}

return auth as AuthenticatedAuthContextValue;
};
35 changes: 7 additions & 28 deletions site/src/modules/dashboard/DashboardProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
createContext,
type FC,
type PropsWithChildren,
useState,
} from "react";
import { createContext, type FC, type PropsWithChildren } from "react";
import { useQuery } from "react-query";
import { appearance } from "api/queries/appearance";
import { entitlements } from "api/queries/entitlements";
Expand All @@ -15,12 +10,14 @@ import type {
} from "api/typesGenerated";
import { Loader } from "components/Loader/Loader";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";

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

export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
const { metadata } = useEmbeddedMetadata();
const { user, organizationIds } = useAuthenticated();
const { user } = useAuthenticated();
const entitlementsQuery = useQuery(entitlements(metadata.entitlements));
const experimentsQuery = useQuery(experiments(metadata.experiments));
const appearanceQuery = useQuery(appearance(metadata.appearance));

const isLoading =
!entitlementsQuery.data || !appearanceQuery.data || !experimentsQuery.data;

const lastUsedOrganizationId = localStorage.getItem(
`user:${user.id}.lastUsedOrganizationId`,
);
const [activeOrganizationId, setActiveOrganizationId] = useState(() =>
lastUsedOrganizationId && organizationIds.includes(lastUsedOrganizationId)
? lastUsedOrganizationId
: organizationIds[0],
);

const setOrganizationId = useEffectEvent((id: string) => {
if (!organizationIds.includes(id)) {
throw new ReferenceError("Invalid organization ID");
}
localStorage.setItem(`user:${user.id}.lastUsedOrganizationId`, id);
setActiveOrganizationId(id);
});

if (isLoading) {
return <Loader fullscreen />;
}

return (
<DashboardContext.Provider
value={{
organizationId: activeOrganizationId,
setOrganizationId: setOrganizationId,
organizationId: user.organization_ids[0] ?? "default",
entitlements: entitlementsQuery.data,
experiments: experimentsQuery.data,
appearance: appearanceQuery.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useOrganizationSettings = (): OrganizationSettingsContextValue => {

export const ManagementSettingsLayout: FC = () => {
const location = useLocation();
const { permissions, organizationIds } = useAuthenticated();
const { permissions } = useAuthenticated();
const { experiments } = useDashboard();
const { organization } = useParams() as { organization: string };
const deploymentConfigQuery = useQuery(deploymentConfig());
Expand All @@ -61,7 +61,7 @@ export const ManagementSettingsLayout: FC = () => {
currentOrganizationId: !inOrganizationSettings
? undefined
: !organization
? organizationIds[0]
? organizationsQuery.data[0]?.id
: organizationsQuery.data.find(
(org) => org.name === organization,
)?.id,
Expand Down
1 change: 0 additions & 1 deletion site/src/testHelpers/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const withDashboardProvider = (
<DashboardContext.Provider
value={{
organizationId: "",
setOrganizationId: () => {},
entitlements,
experiments,
appearance: MockAppearanceConfig,
Expand Down
Loading