mirror of
https://github.com/coder/coder.git
synced 2026-09-22 13:10:21 +08:00
chore: remove organizationIds from AuthProvider (#13917)
This commit is contained in:
@@ -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;
|
||||
@@ -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}
|
||||
|
||||
@@ -95,7 +95,6 @@ describe("useAuthenticated", () => {
|
||||
wrapper: createAuthWrapper({
|
||||
user: MockUser,
|
||||
permissions: MockPermissions,
|
||||
organizationIds: [],
|
||||
}),
|
||||
});
|
||||
}).not.toThrow();
|
||||
|
||||
@@ -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 => {
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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";
|
||||
@@ -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;
|
||||
@@ -32,7 +29,7 @@ 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));
|
||||
@@ -40,23 +37,6 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
|
||||
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 />;
|
||||
}
|
||||
@@ -64,8 +44,7 @@ export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
|
||||
return (
|
||||
<DashboardContext.Provider
|
||||
value={{
|
||||
organizationId: activeOrganizationId,
|
||||
setOrganizationId: setOrganizationId,
|
||||
organizationId: user.organization_ids[0] ?? "default",
|
||||
entitlements: entitlementsQuery.data,
|
||||
experiments: experimentsQuery.data,
|
||||
appearance: appearanceQuery.data,
|
||||
|
||||
@@ -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());
|
||||
@@ -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,
|
||||
|
||||
@@ -27,7 +27,6 @@ export const withDashboardProvider = (
|
||||
<DashboardContext.Provider
|
||||
value={{
|
||||
organizationId: "",
|
||||
setOrganizationId: () => {},
|
||||
entitlements,
|
||||
experiments,
|
||||
appearance: MockAppearanceConfig,
|
||||
|
||||
Reference in New Issue
Block a user