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