2026-09-15 16:35:03 +07:00
|
|
|
import { createResource, createSignal, Show } from "solid-js";
|
|
|
|
|
import { api, type AuthUser } from "../lib/api";
|
2026-09-15 17:40:39 +07:00
|
|
|
import { Button } from "./ui/button";
|
|
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar";
|
2026-09-15 16:35:03 +07:00
|
|
|
|
2026-09-15 17:40:39 +07:00
|
|
|
// Theme: .dark class on <html> (shadcn convention). Default dark.
|
2026-09-15 16:35:03 +07:00
|
|
|
function theme(): string {
|
2026-09-15 17:40:39 +07:00
|
|
|
return localStorage.getItem("fs-theme") || "dark";
|
2026-09-15 16:35:03 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function applyTheme(t: string) {
|
2026-09-15 17:40:39 +07:00
|
|
|
document.documentElement.classList.toggle("dark", t === "dark");
|
2026-09-15 16:35:03 +07:00
|
|
|
localStorage.setItem("fs-theme", t);
|
|
|
|
|
}
|
|
|
|
|
applyTheme(theme());
|
|
|
|
|
|
|
|
|
|
export function ThemeToggle() {
|
|
|
|
|
const [t, setT] = createSignal(theme());
|
|
|
|
|
return (
|
2026-09-15 17:40:39 +07:00
|
|
|
<Button variant="ghost" size="icon" title="Toggle light/dark" onClick={() => {
|
2026-09-15 16:35:03 +07:00
|
|
|
const n = t() === "dark" ? "light" : "dark";
|
|
|
|
|
setT(n); applyTheme(n);
|
|
|
|
|
}}>
|
|
|
|
|
{t() === "dark" ? "☀️" : "🌙"}
|
2026-09-15 17:40:39 +07:00
|
|
|
</Button>
|
2026-09-15 16:35:03 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAuth() {
|
|
|
|
|
const [me, { refetch }] = createResource(async (): Promise<AuthUser | null> => {
|
|
|
|
|
try { return (await api.me()).user; } catch { return null; }
|
|
|
|
|
});
|
|
|
|
|
return { me, refetch };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function AuthButton(props: { me: AuthUser | null | undefined; onLogout: () => void }) {
|
|
|
|
|
return (
|
|
|
|
|
<Show when={props.me} fallback={
|
2026-09-15 17:40:39 +07:00
|
|
|
<a href="/api/auth/start"><Button>Sign in with Google</Button></a>
|
2026-09-15 16:35:03 +07:00
|
|
|
}>
|
|
|
|
|
{(u) => (
|
2026-09-15 17:40:39 +07:00
|
|
|
<span class="flex items-center gap-2">
|
|
|
|
|
<Avatar class="size-8">
|
|
|
|
|
{u().avatar_url ? <AvatarImage src={u().avatar_url} alt="" /> : null}
|
|
|
|
|
<AvatarFallback>{(u().name || u().email || "?").slice(0, 1).toUpperCase()}</AvatarFallback>
|
|
|
|
|
</Avatar>
|
|
|
|
|
<span class="hidden text-sm font-medium md:inline">{u().name || u().email}</span>
|
|
|
|
|
<Button variant="ghost" size="sm" onClick={props.onLogout}>Logout</Button>
|
2026-09-15 16:35:03 +07:00
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</Show>
|
|
|
|
|
);
|
|
|
|
|
}
|