2026-07-04 22:53:51 -05:00
|
|
|
import { createContext, useContext, useEffect, useState, type Dispatch, type SetStateAction, type ReactNode } from 'react';
|
refactor(ts): convert the app shell to TSX (B7)
App.tsx (routing + QueryClient), main.tsx (entry; index.html updated to
main.tsx), useAuth.tsx (typed AuthContextValue/User + MeResponse; uses the safe
useContext()||defaults pattern so no `never` trap), and the layout components
(Layout, Sidebar, NavPill, BrandBlock — NavItem type, LucideIcon-typed nav
config, casts for the untyped overdue/simplefin API responses). TS caught a dead
prop: App passed mainContentId to TrackerPage, which takes no params — removed.
typecheck 0, lint 0 errors (39 warns), build green, 17/17 e2e probe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 19:49:57 -05:00
|
|
|
import { api } from '@/api';
|
2026-07-05 12:26:11 -05:00
|
|
|
import type { User } from '@/types';
|
refactor(ts): convert the app shell to TSX (B7)
App.tsx (routing + QueryClient), main.tsx (entry; index.html updated to
main.tsx), useAuth.tsx (typed AuthContextValue/User + MeResponse; uses the safe
useContext()||defaults pattern so no `never` trap), and the layout components
(Layout, Sidebar, NavPill, BrandBlock — NavItem type, LucideIcon-typed nav
config, casts for the untyped overdue/simplefin API responses). TS caught a dead
prop: App passed mainContentId to TrackerPage, which takes no params — removed.
typecheck 0, lint 0 errors (39 warns), build green, 17/17 e2e probe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 19:49:57 -05:00
|
|
|
|
2026-07-05 12:26:11 -05:00
|
|
|
export type { User }; // re-export so existing `@/hooks/useAuth` importers keep working
|
refactor(ts): convert the app shell to TSX (B7)
App.tsx (routing + QueryClient), main.tsx (entry; index.html updated to
main.tsx), useAuth.tsx (typed AuthContextValue/User + MeResponse; uses the safe
useContext()||defaults pattern so no `never` trap), and the layout components
(Layout, Sidebar, NavPill, BrandBlock — NavItem type, LucideIcon-typed nav
config, casts for the untyped overdue/simplefin API responses). TS caught a dead
prop: App passed mainContentId to TrackerPage, which takes no params — removed.
typecheck 0, lint 0 errors (39 warns), build green, 17/17 e2e probe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 19:49:57 -05:00
|
|
|
|
|
|
|
|
interface MeResponse {
|
|
|
|
|
user?: User | null;
|
|
|
|
|
single_user_mode?: boolean;
|
|
|
|
|
has_new_version?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AuthContextValue {
|
|
|
|
|
user: User | null | undefined; // undefined = loading
|
|
|
|
|
singleUserMode: boolean;
|
2026-07-04 22:53:51 -05:00
|
|
|
setUser: Dispatch<SetStateAction<User | null | undefined>>;
|
refactor(ts): convert the app shell to TSX (B7)
App.tsx (routing + QueryClient), main.tsx (entry; index.html updated to
main.tsx), useAuth.tsx (typed AuthContextValue/User + MeResponse; uses the safe
useContext()||defaults pattern so no `never` trap), and the layout components
(Layout, Sidebar, NavPill, BrandBlock — NavItem type, LucideIcon-typed nav
config, casts for the untyped overdue/simplefin API responses). TS caught a dead
prop: App passed mainContentId to TrackerPage, which takes no params — removed.
typecheck 0, lint 0 errors (39 warns), build green, 17/17 e2e probe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 19:49:57 -05:00
|
|
|
logout: () => void | Promise<void>;
|
|
|
|
|
refresh: () => void;
|
|
|
|
|
hasNewVersion: boolean;
|
|
|
|
|
setHasNewVersion: (v: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AuthContext = createContext<AuthContextValue | null>(null);
|
|
|
|
|
|
|
|
|
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const [user, setUser] = useState<User | null | undefined>(undefined); // undefined = loading
|
|
|
|
|
const [singleUserMode, setSUM] = useState(false);
|
|
|
|
|
const [hasNewVersion, setHasNewVersion] = useState(false);
|
|
|
|
|
|
|
|
|
|
function applyMeResponse(raw: unknown) {
|
|
|
|
|
const d = raw as MeResponse;
|
|
|
|
|
setUser(d.user);
|
|
|
|
|
setSUM(d.single_user_mode || false);
|
|
|
|
|
setHasNewVersion(!!d.has_new_version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
api.authMode().then(d => {
|
|
|
|
|
if ((d as { auth_mode?: string }).auth_mode === 'single') setSUM(true);
|
|
|
|
|
}).catch(err => console.error('[useAuth] authMode check failed', err));
|
|
|
|
|
|
|
|
|
|
api.me().then(applyMeResponse).catch(() => setUser(null));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const logout = async () => {
|
|
|
|
|
await api.logout();
|
|
|
|
|
setUser(null);
|
|
|
|
|
setSUM(false);
|
|
|
|
|
setHasNewVersion(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const refresh = () => {
|
|
|
|
|
api.me().then(applyMeResponse).catch(() => {
|
|
|
|
|
setUser(null);
|
|
|
|
|
setSUM(false);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AuthContext.Provider value={{
|
|
|
|
|
user, singleUserMode, setUser, logout, refresh,
|
|
|
|
|
hasNewVersion, setHasNewVersion,
|
|
|
|
|
}}>
|
|
|
|
|
{children}
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAuth(): AuthContextValue {
|
|
|
|
|
return useContext(AuthContext) || {
|
|
|
|
|
user: null, setUser: () => {}, logout: () => {}, refresh: () => {},
|
|
|
|
|
singleUserMode: false, hasNewVersion: false, setHasNewVersion: () => {},
|
|
|
|
|
};
|
|
|
|
|
}
|