Skip to content
Merged
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
15 changes: 15 additions & 0 deletions ui/src/context/WorkspaceProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Accessor,
createContext,
createEffect,
createMemo,
createSignal,
JSXElement,
Expand All @@ -10,6 +11,7 @@ import {
import { getWorkspaces } from '../api'
import { WorkspaceListItemAttributes } from '../models/Workspace'
import type { FrozenReason } from '../models/Workspace'
import { useUser } from './UserProvider'

interface WorkspaceContextAttributes {
workspaces: Accessor<WorkspaceListItemAttributes[]>
Expand All @@ -27,6 +29,7 @@ interface WorkspaceContextAttributes {
const WorkspaceContext = createContext<WorkspaceContextAttributes | null>(null)

export const WorkspaceProvider = (props: { children: JSXElement }) => {
const { user } = useUser()
const [workspaces, setWorkspaces] = createSignal<
WorkspaceListItemAttributes[]
>([])
Expand Down Expand Up @@ -78,6 +81,18 @@ export const WorkspaceProvider = (props: { children: JSXElement }) => {
localStorage.setItem('currentWorkspaceId', String(workspace.id))
}

// Keep workspaces in sync with the authenticated user so any entry point
// (login, page refresh, deep link) ends up with a populated dropdown and
// a selected workspace without each page having to fetch on its own.
createEffect(() => {
if (user()) {
void fetchWorkspaces()
} else {
setWorkspaces([])
setCurrentWorkspace(null)
}
})

return (
<WorkspaceContext.Provider
value={{
Expand Down
Loading