({ children }: { children: ReactNode })
| 97 | ); |
| 98 | |
| 99 | function AuthGate({ children }: { children: ReactNode }) { |
| 100 | const auth = useAuth(); |
| 101 | // When unauthenticated, decide between first-run setup and sign-in by asking |
| 102 | // the server whether the instance still has zero members. |
| 103 | const [setupStatus, setSetupStatus] = useState< |
| 104 | | { state: "checking"; attempt: number } |
| 105 | | { state: "ready"; needsSetup: boolean } |
| 106 | | { state: "error"; attempt: number } |
| 107 | >({ state: "checking", attempt: 0 }); |
| 108 | useEffect(() => { |
| 109 | if (auth.status !== "unauthenticated") return; |
| 110 | let alive = true; |
| 111 | setSetupStatus((current) => ({ state: "checking", attempt: current.attempt })); |
| 112 | void fetchNeedsSetup().then( |
| 113 | (value) => { |
| 114 | if (alive) setSetupStatus({ state: "ready", needsSetup: value }); |
| 115 | }, |
| 116 | () => { |
| 117 | if (alive) { |
| 118 | setSetupStatus((current) => ({ |
| 119 | state: "error", |
| 120 | attempt: current.state === "ready" ? 0 : current.attempt, |
| 121 | })); |
| 122 | } |
| 123 | }, |
| 124 | ); |
| 125 | return () => { |
| 126 | alive = false; |
| 127 | }; |
| 128 | }, [auth.status, setupStatus.attempt]); |
| 129 | |
| 130 | if (auth.status === "loading") return <Loading />; |
| 131 | if (auth.status === "unauthenticated") { |
| 132 | if (setupStatus.state === "checking") return <Loading />; |
| 133 | if (setupStatus.state === "error") { |
| 134 | return ( |
| 135 | <SetupStatusErrorCard |
| 136 | onRetry={() => |
| 137 | setSetupStatus((current) => ({ |
| 138 | state: "checking", |
| 139 | attempt: current.state === "ready" ? 0 : current.attempt + 1, |
| 140 | })) |
| 141 | } |
| 142 | /> |
| 143 | ); |
| 144 | } |
| 145 | return setupStatus.needsSetup ? <SetupPage /> : <LoginPage />; |
| 146 | } |
| 147 | return <>{children}</>; |
| 148 | } |
| 149 | |
| 150 | function AuthenticatedApp() { |
| 151 | const auth = useAuth(); |
nothing calls this directly
no test coverage detected