- Notifications
You must be signed in to change notification settings - Fork1k
chore: expose all organization ids from AuthContext#13268
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
c7b1889
45545c4
fbd5daa
7eb193e
387b3a2
cd7b026
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { | ||
createContext, | ||
type FC, | ||
type PropsWithChildren, | ||
useState, | ||
} from "react"; | ||
import { useQuery } from "react-query"; | ||
import { appearance } from "api/queries/appearance"; | ||
import { entitlements } from "api/queries/entitlements"; | ||
@@ -9,9 +14,13 @@ import type { | ||
Experiments, | ||
} 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 { | ||
organizationId: string; | ||
setOrganizationId: (id: string) => void; | ||
entitlements: Entitlements; | ||
experiments: Experiments; | ||
appearance: AppearanceConfig; | ||
@@ -23,20 +32,40 @@ export const DashboardContext = createContext<DashboardValue | undefined>( | ||
export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const { metadata } = useEmbeddedMetadata(); | ||
const { user, organizationIds } = 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], | ||
); | ||
Comment on lines +46 to +50 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Talked to@aslilac, we are not going to introduce the concept of "Default Org" to the FE. It is a stop gap on the backend to accommodate features like group sync, role sync, SCIM, etc. Those features need to be upgraded to not rely on default org. If a deployment exists with more than 1 org, this is no morebroken than it currently is today. | ||
const setOrganizationId = useEffectEvent((id: string) => { | ||
if (!organizationIds.includes(id)) { | ||
throw new ReferenceError("Invalid organization ID"); | ||
} | ||
localStorage.setItem(`user:${user.id}.lastUsedOrganizationId`, id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Smart assigning to a user! | ||
setActiveOrganizationId(id); | ||
}); | ||
if (isLoading) { | ||
return <Loader fullscreen />; | ||
} | ||
return ( | ||
<DashboardContext.Provider | ||
value={{ | ||
organizationId: activeOrganizationId, | ||
setOrganizationId: setOrganizationId, | ||
entitlements: entitlementsQuery.data, | ||
experiments: experimentsQuery.data, | ||
appearance: appearanceQuery.data, | ||
Uh oh!
There was an error while loading.Please reload this page.