bolt.diy/app/shared/stores/theme.ts
KevIsDev 4d3222ee96 refactor: reorganize project structure by moving files to a more dev friendly setup
- Move stores/utils/types to their relative directories (i.e chat stores in chat directory)
- Move utility files to shared/utils
- Move component files to shared/components
- Move type definitions to shared/types
- Move stores to shared/stores
- Update import paths across the project
2025-06-16 15:33:59 +01:00

55 lines
1.4 KiB
TypeScript

import { atom } from 'nanostores';
import { logStore } from './logs';
export type Theme = 'dark' | 'light';
export const kTheme = 'bolt_theme';
export function themeIsDark() {
return themeStore.get() === 'dark';
}
export const DEFAULT_THEME = 'light';
export const themeStore = atom<Theme>(initStore());
function initStore() {
if (!import.meta.env.SSR) {
const persistedTheme = localStorage.getItem(kTheme) as Theme | undefined;
const themeAttribute = document.querySelector('html')?.getAttribute('data-theme');
return persistedTheme ?? (themeAttribute as Theme) ?? DEFAULT_THEME;
}
return DEFAULT_THEME;
}
export function toggleTheme() {
const currentTheme = themeStore.get();
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
// Update the theme store
themeStore.set(newTheme);
// Update localStorage
localStorage.setItem(kTheme, newTheme);
// Update the HTML attribute
document.querySelector('html')?.setAttribute('data-theme', newTheme);
// Update user profile if it exists
try {
const userProfile = localStorage.getItem('bolt_user_profile');
if (userProfile) {
const profile = JSON.parse(userProfile);
profile.theme = newTheme;
localStorage.setItem('bolt_user_profile', JSON.stringify(profile));
}
} catch (error) {
console.error('Error updating user profile theme:', error);
}
logStore.logSystem(`Theme changed to ${newTheme} mode`);
}