import { QueryClientProvider, useQuery, useQueryClient } from "@tanstack/preact-query" import { useEffect, useState } from "preact/hooks" import { api, getSession, sessionKey } from "../lib/api" import { createQueryClient } from "../lib/query" import { LoginForm } from "./LoginForm" import { Explorer } from "./files/Explorer" import { Navigation } from "./Navigation" import { AccountSettings } from "./AccountSettings" type Mode = "files" | "login" | "settings" function Account({ mode }: { mode: Mode }) { const client = useQueryClient() const session = useQuery({ queryKey: sessionKey, queryFn: ({ signal }) => getSession(signal), retry: false, }) const [error, setError] = useState("") const [loggingOut, setLoggingOut] = useState(false) const account = session.data useEffect(() => { if (account && mode === "login") window.location.replace("/files") }, [account, mode]) const logOut = async () => { setLoggingOut(true) setError("") try { await client.cancelQueries({ queryKey: ["files"] }) const response = await api.POST("/api/v1/logout") if (!response.response.ok && response.response.status !== 401) throw new Error("Logout failed. Please try again.") client.clear() window.location.replace("/login") } catch (error) { setError(error instanceof Error ? error.message : "Logout failed.") setLoggingOut(false) } } if (session.isPending) return ( <>

Restoring your ATProto session…

) if (session.isError) return ( <>

Cannot restore your session

{session.error.message}

{error && (

{error}

)}
) if (!account) return ( <> ) if (mode === "login") return

Opening your files…

return ( <> {error && (

{error}

)} {mode === "settings" ? ( void logOut()} /> ) : loggingOut ? (

Logging out…

) : ( )} ) } export default function DriveApp({ mode }: { mode: Mode }) { const [client] = useState(createQueryClient) return ( ) }