refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
|
2026-05-03 19:51:57 -05:00
|
|
|
|
|
|
|
|
/**
|
2026-07-05 15:50:37 -05:00
|
|
|
* Themes: 'light' | 'dark' | 'system'
|
2026-05-03 19:51:57 -05:00
|
|
|
* Persisted to localStorage under key 'bt-theme'.
|
|
|
|
|
*
|
2026-07-05 15:50:37 -05:00
|
|
|
* 'system' follows the device's `prefers-color-scheme` and updates live when the
|
|
|
|
|
* OS flips. `resolvedTheme` is always the concrete 'light' | 'dark' actually
|
|
|
|
|
* applied — consume it for anything that needs a real value (e.g. Sonner).
|
|
|
|
|
*
|
2026-05-03 19:51:57 -05:00
|
|
|
* Class mapping on document.documentElement:
|
2026-07-05 15:50:37 -05:00
|
|
|
* light → (no classes) dark → 'dark'
|
2026-05-03 19:51:57 -05:00
|
|
|
*/
|
|
|
|
|
|
2026-07-05 15:50:37 -05:00
|
|
|
type Theme = 'light' | 'dark' | 'system';
|
|
|
|
|
type ResolvedTheme = 'light' | 'dark';
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
|
2026-05-03 19:51:57 -05:00
|
|
|
const STORAGE_KEY = 'bt-theme';
|
2026-07-05 15:50:37 -05:00
|
|
|
const VALID_THEMES: Theme[] = ['light', 'dark', 'system'];
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
const DEFAULT_THEME: Theme = 'dark';
|
2026-05-03 19:51:57 -05:00
|
|
|
|
2026-07-05 15:50:37 -05:00
|
|
|
// Keep in sync with the inline pre-bundle script in index.html.
|
2026-07-05 17:18:31 -05:00
|
|
|
const THEME_COLORS: Record<ResolvedTheme, string> = { light: '#f8faf9', dark: '#101417' };
|
2026-07-05 15:50:37 -05:00
|
|
|
|
|
|
|
|
function systemPrefersDark(): boolean {
|
|
|
|
|
return typeof window !== 'undefined' && !!window.matchMedia?.('(prefers-color-scheme: dark)').matches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveTheme(theme: Theme): ResolvedTheme {
|
|
|
|
|
if (theme === 'system') return systemPrefersDark() ? 'dark' : 'light';
|
|
|
|
|
return theme;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyTheme(theme: Theme): ResolvedTheme {
|
|
|
|
|
const resolved = resolveTheme(theme);
|
2026-05-03 19:51:57 -05:00
|
|
|
const root = document.documentElement;
|
2026-07-05 15:50:37 -05:00
|
|
|
root.classList.toggle('dark', resolved === 'dark');
|
|
|
|
|
const meta = document.querySelector('meta[name="theme-color"]');
|
|
|
|
|
if (meta) meta.setAttribute('content', THEME_COLORS[resolved]);
|
|
|
|
|
return resolved;
|
2026-05-03 19:51:57 -05:00
|
|
|
}
|
|
|
|
|
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
function loadStoredTheme(): Theme {
|
2026-05-03 19:51:57 -05:00
|
|
|
try {
|
|
|
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
|
|
|
if (stored === 'dark-purple') return 'dark';
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
if (stored && (VALID_THEMES as string[]).includes(stored)) return stored as Theme;
|
2026-05-03 19:51:57 -05:00
|
|
|
} catch {
|
|
|
|
|
// localStorage unavailable
|
|
|
|
|
}
|
|
|
|
|
return DEFAULT_THEME;
|
|
|
|
|
}
|
|
|
|
|
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
interface ThemeContextValue {
|
|
|
|
|
theme: Theme;
|
2026-07-05 15:50:37 -05:00
|
|
|
resolvedTheme: ResolvedTheme;
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
setTheme: (theme: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | null>(null);
|
2026-05-03 19:51:57 -05:00
|
|
|
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const [theme, setThemeState] = useState<Theme>(() => {
|
2026-05-03 19:51:57 -05:00
|
|
|
const stored = loadStoredTheme();
|
2026-07-05 15:50:37 -05:00
|
|
|
// Apply immediately (before first paint) to avoid flash.
|
2026-05-03 19:51:57 -05:00
|
|
|
applyTheme(stored);
|
|
|
|
|
return stored;
|
|
|
|
|
});
|
2026-07-05 15:50:37 -05:00
|
|
|
const [resolvedTheme, setResolvedTheme] = useState<ResolvedTheme>(() => resolveTheme(loadStoredTheme()));
|
2026-05-03 19:51:57 -05:00
|
|
|
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
const setTheme = (newTheme: string) => {
|
|
|
|
|
if (!(VALID_THEMES as string[]).includes(newTheme)) return;
|
2026-07-05 15:50:37 -05:00
|
|
|
const next = newTheme as Theme;
|
|
|
|
|
setResolvedTheme(applyTheme(next));
|
|
|
|
|
setThemeState(next);
|
2026-05-03 19:51:57 -05:00
|
|
|
try {
|
2026-07-05 15:50:37 -05:00
|
|
|
localStorage.setItem(STORAGE_KEY, next);
|
2026-05-03 19:51:57 -05:00
|
|
|
} catch {
|
|
|
|
|
// localStorage unavailable
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-05 15:50:37 -05:00
|
|
|
// Keep DOM in sync if theme state ever changes externally.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setResolvedTheme(applyTheme(theme));
|
|
|
|
|
}, [theme]);
|
|
|
|
|
|
|
|
|
|
// While on 'system', re-apply live when the OS light/dark preference flips.
|
2026-05-03 19:51:57 -05:00
|
|
|
useEffect(() => {
|
2026-07-05 15:50:37 -05:00
|
|
|
if (theme !== 'system' || typeof window === 'undefined' || !window.matchMedia) return;
|
|
|
|
|
const mq = window.matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
|
const onChange = () => setResolvedTheme(applyTheme('system'));
|
|
|
|
|
mq.addEventListener('change', onChange);
|
|
|
|
|
return () => mq.removeEventListener('change', onChange);
|
2026-05-03 19:51:57 -05:00
|
|
|
}, [theme]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-05 15:50:37 -05:00
|
|
|
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme }}>
|
2026-05-03 19:51:57 -05:00
|
|
|
{children}
|
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useTheme() {
|
|
|
|
|
const ctx = useContext(ThemeContext);
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
throw new Error('useTheme must be used within a ThemeProvider');
|
|
|
|
|
}
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|