chore: remove organizationIds from AuthProvider (#13917)

This commit is contained in:
Kayla Washburn-Love
2024-07-17 09:53:40 -06:00
committed by GitHub
parent f21f2dce57
commit 80cbffe843
6 changed files with 10 additions and 39 deletions
-2
View File
@@ -30,7 +30,6 @@ export type AuthContextValue = {
isUpdatingProfile: boolean; isUpdatingProfile: boolean;
user: User | undefined; user: User | undefined;
permissions: Permissions | undefined; permissions: Permissions | undefined;
organizationIds: readonly string[] | undefined;
signInError: unknown; signInError: unknown;
updateProfileError: unknown; updateProfileError: unknown;
signOut: () => void; signOut: () => void;
@@ -119,7 +118,6 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
permissions: permissionsQuery.data as Permissions | undefined, permissions: permissionsQuery.data as Permissions | undefined,
signInError: loginMutation.error, signInError: loginMutation.error,
updateProfileError: updateProfileMutation.error, updateProfileError: updateProfileMutation.error,
organizationIds: userQuery.data?.organization_ids,
}} }}
> >
{children} {children}
@@ -95,7 +95,6 @@ describe("useAuthenticated", () => {
wrapper: createAuthWrapper({ wrapper: createAuthWrapper({
user: MockUser, user: MockUser,
permissions: MockPermissions, permissions: MockPermissions,
organizationIds: [],
}), }),
}); });
}).not.toThrow(); }).not.toThrow();
+1 -5
View File
@@ -74,7 +74,7 @@ type RequireKeys<T, R extends keyof T> = Omit<T, R> & {
// values are not undefined when authenticated // values are not undefined when authenticated
type AuthenticatedAuthContextValue = RequireKeys< type AuthenticatedAuthContextValue = RequireKeys<
AuthContextValue, AuthContextValue,
"user" | "permissions" | "organizationIds" "user" | "permissions"
>; >;
export const useAuthenticated = (): AuthenticatedAuthContextValue => { export const useAuthenticated = (): AuthenticatedAuthContextValue => {
@@ -88,9 +88,5 @@ export const useAuthenticated = (): AuthenticatedAuthContextValue => {
throw new Error("Permissions are not available."); throw new Error("Permissions are not available.");
} }
if (!auth.organizationIds) {
throw new Error("Organization ID is not available.");
}
return auth as AuthenticatedAuthContextValue; return auth as AuthenticatedAuthContextValue;
}; };
@@ -1,9 +1,4 @@
import { import { createContext, type FC, type PropsWithChildren } from "react";
createContext,
type FC,
type PropsWithChildren,
useState,
} from "react";
import { useQuery } from "react-query"; import { useQuery } from "react-query";
import { appearance } from "api/queries/appearance"; import { appearance } from "api/queries/appearance";
import { entitlements } from "api/queries/entitlements"; import { entitlements } from "api/queries/entitlements";
@@ -15,12 +10,14 @@ import type {
} from "api/typesGenerated"; } from "api/typesGenerated";
import { Loader } from "components/Loader/Loader"; import { Loader } from "components/Loader/Loader";
import { useAuthenticated } from "contexts/auth/RequireAuth"; import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata"; import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
export interface DashboardValue { 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; organizationId: string;
setOrganizationId: (id: string) => void;
entitlements: Entitlements; entitlements: Entitlements;
experiments: Experiments; experiments: Experiments;
appearance: AppearanceConfig; appearance: AppearanceConfig;
@@ -32,7 +29,7 @@ export const DashboardContext = createContext<DashboardValue | undefined>(
export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => { export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
const { metadata } = useEmbeddedMetadata(); const { metadata } = useEmbeddedMetadata();
const { user, organizationIds } = useAuthenticated(); const { user } = useAuthenticated();
const entitlementsQuery = useQuery(entitlements(metadata.entitlements)); const entitlementsQuery = useQuery(entitlements(metadata.entitlements));
const experimentsQuery = useQuery(experiments(metadata.experiments)); const experimentsQuery = useQuery(experiments(metadata.experiments));
const appearanceQuery = useQuery(appearance(metadata.appearance)); const appearanceQuery = useQuery(appearance(metadata.appearance));
@@ -40,23 +37,6 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
const isLoading = const isLoading =
!entitlementsQuery.data || !appearanceQuery.data || !experimentsQuery.data; !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) { if (isLoading) {
return <Loader fullscreen />; return <Loader fullscreen />;
} }
@@ -64,8 +44,7 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
return ( return (
<DashboardContext.Provider <DashboardContext.Provider
value={{ value={{
organizationId: activeOrganizationId, organizationId: user.organization_ids[0] ?? "default",
setOrganizationId: setOrganizationId,
entitlements: entitlementsQuery.data, entitlements: entitlementsQuery.data,
experiments: experimentsQuery.data, experiments: experimentsQuery.data,
appearance: appearanceQuery.data, appearance: appearanceQuery.data,
@@ -35,7 +35,7 @@ export const useOrganizationSettings = (): OrganizationSettingsContextValue => {
export const ManagementSettingsLayout: FC = () => { export const ManagementSettingsLayout: FC = () => {
const location = useLocation(); const location = useLocation();
const { permissions, organizationIds } = useAuthenticated(); const { permissions } = useAuthenticated();
const { experiments } = useDashboard(); const { experiments } = useDashboard();
const { organization } = useParams() as { organization: string }; const { organization } = useParams() as { organization: string };
const deploymentConfigQuery = useQuery(deploymentConfig()); const deploymentConfigQuery = useQuery(deploymentConfig());
@@ -61,7 +61,7 @@ export const ManagementSettingsLayout: FC = () => {
currentOrganizationId: !inOrganizationSettings currentOrganizationId: !inOrganizationSettings
? undefined ? undefined
: !organization : !organization
? organizationIds[0] ? organizationsQuery.data[0]?.id
: organizationsQuery.data.find( : organizationsQuery.data.find(
(org) => org.name === organization, (org) => org.name === organization,
)?.id, )?.id,
-1
View File
@@ -27,7 +27,6 @@ export const withDashboardProvider = (
<DashboardContext.Provider <DashboardContext.Provider
value={{ value={{
organizationId: "", organizationId: "",
setOrganizationId: () => {},
entitlements, entitlements,
experiments, experiments,
appearance: MockAppearanceConfig, appearance: MockAppearanceConfig,