({ children }: { children: ReactNode })
| 113 | ); |
| 114 | |
| 115 | function AuthGate({ children }: { children: ReactNode }) { |
| 116 | const auth = useAuth(); |
| 117 | // When unauthenticated, decide between first-run setup and sign-in by asking |
| 118 | // the server whether the instance still has zero members. |
| 119 | const [setupStatus, setSetupStatus] = useState< |
| 120 | | { state: "checking"; attempt: number } |
| 121 | | { state: "ready"; needsSetup: boolean } |
| 122 | | { state: "error"; attempt: number } |
| 123 | >({ state: "checking", attempt: 0 }); |
| 124 | useEffect(() => { |
| 125 | if (auth.status !== "unauthenticated") return; |
| 126 | let alive = true; |
| 127 | setSetupStatus((current) => ({ state: "checking", attempt: current.attempt })); |
| 128 | void fetchNeedsSetup().then( |
| 129 | (value) => { |
| 130 | if (alive) setSetupStatus({ state: "ready", needsSetup: value }); |
| 131 | }, |
| 132 | () => { |
| 133 | if (alive) { |
| 134 | setSetupStatus((current) => ({ |
| 135 | state: "error", |
| 136 | attempt: current.state === "ready" ? 0 : current.attempt, |
| 137 | })); |
| 138 | } |
| 139 | }, |
| 140 | ); |
| 141 | return () => { |
| 142 | alive = false; |
| 143 | }; |
| 144 | }, [auth.status, setupStatus.attempt]); |
| 145 | |
| 146 | if (auth.status === "loading") return <Loading />; |
| 147 | if (auth.status === "unauthenticated") { |
| 148 | if (setupStatus.state === "checking") return <Loading />; |
| 149 | if (setupStatus.state === "error") { |
| 150 | return ( |
| 151 | <SetupStatusErrorCard |
| 152 | onRetry={() => |
| 153 | setSetupStatus((current) => ({ |
| 154 | state: "checking", |
| 155 | attempt: current.state === "ready" ? 0 : current.attempt + 1, |
| 156 | })) |
| 157 | } |
| 158 | /> |
| 159 | ); |
| 160 | } |
| 161 | return setupStatus.needsSetup ? <SetupPage /> : <LoginPage />; |
| 162 | } |
| 163 | return <>{children}</>; |
| 164 | } |
| 165 | |
| 166 | function AuthenticatedApp() { |
| 167 | const auth = useAuth(); |
nothing calls this directly
no test coverage detected