mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 01:39:37 +08:00
- Remove generation artifact (IMPLEMENTATION_COMPLETE.md) - Consolidate duplicate theme checks in settings page - Simplify theme precedence logic (localStorage > backend) - Remove all console.log statements from theme code - Add debounced auto-save (500ms) to prevent API spam - Centralize theme API with subscriptions and constants - Improve error handling (silent fails for localStorage) - Guard all localStorage access for SSR compatibility Addresses all feedback from @scrrlt review: - No more redundant theme logic - Single source of truth for theme operations - Proper error handling without console noise - Debounced network calls for better performance
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* ThemeScript - Initializes theme from localStorage before React hydration
|
|
* This prevents the flash of wrong theme on page load
|
|
*/
|
|
export default function ThemeScript() {
|
|
const themeScript = `
|
|
(function() {
|
|
try {
|
|
const stored = localStorage.getItem('deeptutor-theme');
|
|
|
|
if (stored === 'dark') {
|
|
document.documentElement.classList.add('dark');
|
|
} else if (stored === 'light') {
|
|
document.documentElement.classList.remove('dark');
|
|
} else {
|
|
// Use system preference if not set
|
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
document.documentElement.classList.add('dark');
|
|
localStorage.setItem('deeptutor-theme', 'dark');
|
|
} else {
|
|
localStorage.setItem('deeptutor-theme', 'light');
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// Silently fail - localStorage may be disabled
|
|
}
|
|
})();
|
|
`;
|
|
|
|
return (
|
|
<script
|
|
dangerouslySetInnerHTML={{ __html: themeScript }}
|
|
suppressHydrationWarning
|
|
/>
|
|
);
|
|
}
|