Files
photoreport/apps/app/src/auth.tsx
spark ae7f8eb6f9
ci / rust (push) Successful in 11m44s
ci / typescript (push) Successful in 13m59s
desktop-release / build (--target universal-apple-darwin, dmg,app, darwin-universal, macos) (push) Successful in 18m44s
desktop-release / build (deb,appimage, linux-x86_64, ubuntu-latest) (push) Successful in 33m41s
Work offline: library from cache, local arrivals, queued changes (0.3.7)
The desktop app opens and keeps working offline: events, photo lists, edit states, thumbnails and previews are kept on the computer; photos arriving by FTP show at once (filed by capture time) and can be culled, named and edited before upload; culling, IPTC and "send to" made offline wait in order and go when the server answers; uploads wait instead of failing. The autopilot never edits over a person's edit.

Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-13 18:34:30 +01:00

112 lines
3.5 KiB
TypeScript

import type { ApiClient, Me, Organization, Role } from "@photoreport/sdk";
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { createApi } from "./api.js";
import { isNetworkError } from "./offline/queue.js";
import { isDesktop, prefs } from "./platform.js";
type Status = "loading" | "signedOut" | "signedIn";
interface AuthState {
status: Status;
api: ApiClient;
me: Me | null;
organization: Organization | null;
role: Role | null;
selectOrganization: (id: string) => void;
signIn: (email: string, password: string) => Promise<void>;
completeDeviceSignIn: () => Promise<void>;
signOut: () => Promise<void>;
reload: () => Promise<void>;
}
const AuthContext = createContext<AuthState | null>(null);
export function AuthProvider({ children, api: injected }: { children: ReactNode; api?: ApiClient }) {
const [status, setStatus] = useState<Status>("loading");
const [me, setMe] = useState<Me | null>(null);
const [orgId, setOrgId] = useState<string | null>(() => prefs.get("org"));
const lost = useRef<() => void>(() => {});
const api = useMemo(() => injected ?? createApi(() => lost.current()), [injected]);
lost.current = () => {
setMe(null);
setStatus("signedOut");
};
const reload = useCallback(async () => {
const next = await api.me();
setMe(next);
setStatus("signedIn");
// Desktop app: who is signed in, so the app opens without a network (the session itself is in the keychain).
if (isDesktop()) prefs.set("me", JSON.stringify(next));
}, [api]);
useEffect(() => {
let alive = true;
let retry: ReturnType<typeof setInterval> | undefined;
const start = () =>
api.resume().then(async (ok) => {
if (!alive) return;
if (ok) await reload();
else setStatus("signedOut");
});
start().catch((err: unknown) => {
if (!alive) return;
// No network on the desktop app: carry on as the person last signed in here; the session is checked again
// once the server answers (a refused session still signs out then).
const kept = isDesktop() && isNetworkError(err) ? prefs.get("me") : null;
if (!kept) {
setStatus("signedOut");
return;
}
setMe(JSON.parse(kept) as Me);
setStatus("signedIn");
const again = () =>
void start().then(
() => clearInterval(retry),
() => {},
);
retry = setInterval(again, 20_000);
window.addEventListener("online", again, { once: true });
});
return () => {
alive = false;
clearInterval(retry);
};
}, [api, reload]);
const membership = me?.memberships.find((m) => m.organization.id === orgId) ?? me?.memberships[0] ?? null;
const value: AuthState = {
status,
api,
me,
organization: membership?.organization ?? null,
role: membership?.role ?? null,
selectOrganization: (id) => {
setOrgId(id);
prefs.set("org", id);
},
signIn: async (email, password) => {
await api.login(email, password);
await reload();
},
completeDeviceSignIn: reload,
signOut: async () => {
await api.logout().catch(() => {});
prefs.set("me", "");
setMe(null);
setStatus("signedOut");
},
reload,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
export function useAuth(): AuthState {
const ctx = useContext(AuthContext);
if (!ctx) throw new Error("useAuth outside AuthProvider");
return ctx;
}