| 53 | * should show a first-run welcome screen. |
| 54 | */ |
| 55 | export function useWorkspaceInit( |
| 56 | activeWorkspace: Workspace | null, |
| 57 | workspaceKey: string, |
| 58 | isSharedIdentity: boolean, |
| 59 | ): WorkspaceInitResult { |
| 60 | const [result, setResult] = useState<WorkspaceInitResult>({ |
| 61 | isReady: false, |
| 62 | needsSetup: false, |
| 63 | appliedKey: null, |
| 64 | }); |
| 65 | |
| 66 | // Track whether this is the initial mount or a workspace switch. |
| 67 | // On the initial mount we skip resetting singletons (they're fresh). |
| 68 | const hasInitializedRef = useRef(false); |
| 69 | |
| 70 | // biome-ignore lint/correctness/useExhaustiveDependencies: we intentionally depend on specific properties (id/relayUrl/token/reposDir) — depending on the whole object would trigger resets on name-only changes |
| 71 | useEffect(() => { |
| 72 | let cancelled = false; |
| 73 | |
| 74 | async function init() { |
| 75 | if (!activeWorkspace) { |
| 76 | try { |
| 77 | const defaultRelayUrl = await getDefaultRelayUrl(); |
| 78 | |
| 79 | if (isSharedIdentity) { |
| 80 | const identity = await getIdentity(); |
| 81 | if (cancelled) return; |
| 82 | initFirstWorkspace(defaultRelayUrl, identity.pubkey); |
| 83 | if (!cancelled) { |
| 84 | window.location.reload(); |
| 85 | } |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (!cancelled) { |
| 90 | setResult({ |
| 91 | isReady: false, |
| 92 | needsSetup: true, |
| 93 | defaultRelayUrl, |
| 94 | }); |
| 95 | } |
| 96 | } catch { |
| 97 | if (!cancelled) { |
| 98 | setResult({ |
| 99 | isReady: false, |
| 100 | needsSetup: true, |
| 101 | defaultRelayUrl: "ws://localhost:3000", |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Mark this workspace config as pending while it is applied to the |
| 109 | // backend. App.tsx also checks appliedKey against the active workspaceKey, |
| 110 | // which prevents rendering workspace-scoped UI for a new workspace until |
| 111 | // that exact config has finished applying. |
| 112 | setResult({ |