Skip to content
Open
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
23 changes: 16 additions & 7 deletions apps/editor/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,24 @@ function AppInner() {
}, [authStatus, route])

// First login: give the user the default role (idempotent) and refresh their token if it did.
// Nothing loads server data until this is done: a request with the token from before the
// role was given gets 403.
const ensureSelfAttempted = useRef<string | null>(null)
const [ensureSelfDone, setEnsureSelfDone] = useState<string | null>(null)
useEffect(() => {
if (authStatus !== 'authenticated' || !tenantId) return
const key = tenantId
if (ensureSelfAttempted.current === key) return
ensureSelfAttempted.current = key
ensureDefaultMembership(api, tenantId, refreshAccessToken).catch((err: unknown) => {
console.warn('[App] ensure-self failed:', err)
})
}).finally(() => setEnsureSelfDone(key))
}, [authStatus, tenantId, api, refreshAccessToken])
const ready = authStatus === 'authenticated' && (!tenantId || ensureSelfDone === tenantId)

// Fetch the full definitions catalogue from the management endpoint once authenticated.
useEffect(() => {
if (authStatus !== 'authenticated' || !tenantId) return
if (!ready || !tenantId) return
let cancelled = false
console.log('[App] Fetching definitions catalogue for tenant:', tenantId)
api.listDefinitions({ tenantId }).then((defs) => {
Expand Down Expand Up @@ -234,11 +238,11 @@ function AppInner() {
console.warn('[App] Failed to load definitions catalogue:', err)
})
return () => { cancelled = true }
}, [authStatus, tenantId, api])
}, [ready, tenantId, api])

// Fetch server framework list so tree-view crosswalk selector can show all tenant frameworks.
useEffect(() => {
if (authStatus !== 'authenticated') return
if (!ready) return
let cancelled = false
api.listCfDocuments({ caseVersion: 'v1p1', includeArchived: false }).then((docs) => {
if (cancelled) return
Expand All @@ -247,7 +251,7 @@ function AppInner() {
console.warn('[App] Failed to load server framework list:', err)
})
return () => { cancelled = true }
}, [authStatus, api])
}, [ready, api])

const activeFramework = useMemo(() => {
if (!activeFrameworkId) return null
Expand Down Expand Up @@ -421,7 +425,7 @@ function AppInner() {
// Falls back to home if it can't be loaded (deleted, no access, etc.).
useEffect(() => {
if (!activeFrameworkId) return
if (authStatus !== 'authenticated') return
if (!ready) return
if (serverVerifiedIds.has(activeFrameworkId)) return
let cancelled = false
openRemoteFramework(activeFrameworkId, { replace: true }).catch((err: unknown) => {
Expand All @@ -430,7 +434,7 @@ function AppInner() {
navigateHome({ replace: true })
})
return () => { cancelled = true }
}, [activeFrameworkId, authStatus, serverVerifiedIds, openRemoteFramework, navigateHome])
}, [activeFrameworkId, ready, serverVerifiedIds, openRemoteFramework, navigateHome])

// Load a framework from the server into the local session without navigating to it.
// Used by TreePanelView when the user selects a crosswalk target that isn't loaded locally yet.
Expand Down Expand Up @@ -749,6 +753,11 @@ function AppInner() {
return <LoginScreen />
}

if (!ready) {
// First login: the default role and its new token, before any screen loads server data.
return null
}

const homeScreen = (
<HomeScreen
onOpenRemoteFramework={openRemoteFramework}
Expand Down
Loading