Mobile-first Web-App fuer persoenliches Health-, Ernaehrungs-, Aktivitaets- und Einkaufs-Tracking im Home-Lab-Betrieb. Stack: pnpm Monorepo - Next.js 16 + React 19 + Tailwind (apps/admin) - Hono + tRPC + Better Auth mit 2FA und Passkeys (packages/api) - PostgreSQL + Drizzle ORM (packages/db) - Docker fuer Postgres und pgAdmin. Module: - Auth mit Login, 2FA (TOTP + Backup-Codes), Passkeys und Admin-Recovery - Tagesansicht mit Essen, Trinken, Gewicht, Aktivitaeten und Coach-Hinweisen - Lebensmittelkatalog mit Naehrwerten, Portionen, OCR/Barcode/Open Food Facts - Rezepte mit Live-Naehrwertberechnung und Community-Vorschlaegen - Essensplanung und Wochenbudget mit Sportbonus - Einkaufslisten mit Maerkten und Laufweg-Sortierung - Hellfireclub mit Aktivitaetspunkten und gemeinsamer Aktivitaetsplanung - Wearables-Vorbereitung (Fitbit / Google Fit / Health Connect) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
import { trpc } from "@/lib/trpc";
|
|
|
|
type ThemeContextValue = { isDark: boolean; toggleDark: () => void };
|
|
|
|
const ThemeContext = createContext<ThemeContextValue>({
|
|
isDark: false,
|
|
toggleDark: () => {},
|
|
});
|
|
|
|
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
|
const [isDark, setIsDark] = useState(false);
|
|
const [localInit, setLocalInit] = useState(false);
|
|
|
|
// 1. Start neutral; the user profile below owns the persisted theme.
|
|
useEffect(() => {
|
|
setLocalInit(true);
|
|
}, []);
|
|
|
|
// 2. Sync from DB once profile is loaded — overrides the localStorage value
|
|
const { data: profile } = trpc.users.myProfile.useQuery(undefined, {
|
|
enabled: localInit,
|
|
retry: false,
|
|
});
|
|
|
|
const setTheme = trpc.users.setTheme.useMutation();
|
|
|
|
useEffect(() => {
|
|
if (!profile) return;
|
|
const userThemeKey = `hellth-theme:${profile.id}`;
|
|
const dbTheme = profile.theme;
|
|
if (dbTheme === null || dbTheme === undefined) {
|
|
const stored = localStorage.getItem(userThemeKey);
|
|
const dark = stored === "dark";
|
|
setIsDark(dark);
|
|
document.documentElement.classList.toggle("dark", dark);
|
|
return;
|
|
}
|
|
const dark = dbTheme === "dark";
|
|
setIsDark(dark);
|
|
document.documentElement.classList.toggle("dark", dark);
|
|
localStorage.setItem(userThemeKey, dark ? "dark" : "light");
|
|
}, [profile?.id, profile?.theme]);
|
|
|
|
function toggleDark() {
|
|
const next = !isDark;
|
|
setIsDark(next);
|
|
document.documentElement.classList.toggle("dark", next);
|
|
const themeValue = next ? "dark" : "light";
|
|
if (profile?.id) {
|
|
localStorage.setItem(`hellth-theme:${profile.id}`, themeValue);
|
|
}
|
|
setTheme.mutate({ theme: themeValue });
|
|
}
|
|
|
|
return (
|
|
<ThemeContext.Provider value={{ isDark, toggleDark }}>
|
|
{children}
|
|
</ThemeContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useTheme() {
|
|
return useContext(ThemeContext);
|
|
}
|