jan/web-app/src/hooks/useTheme.ts
Dinh Long Nguyen a30eb7f968
feat: Jan Web (reusing Jan Desktop UI) (#6298)
* add platform guards

* add service management

* fix types

* move to zustand for servicehub

* update App Updater

* update tauri missing move

* update app updater

* refactor: move PlatformFeatures to separate const file

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* change tauri fetch name

* update implementation

* update extension fetch

* make web version run properly

* disabled unused web settings

* fix all tests

* fix lint

* fix tests

* add mock for extension

* fix build

* update make and mise

* fix tsconfig for web-extensions

* fix loader type

* cleanup

* fix test

* update error handling + mcp should be working

* Update mcp init

* use separate is_web_app build property

* Remove fixed model catalog url

* fix additional tests

* fix download issue (event emitter not implemented correctly)

* Update Title html

* fix app logs

* update root tsx render timing

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-05 01:47:46 +07:00

57 lines
1.7 KiB
TypeScript

import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'
import { getServiceHub } from '@/hooks/useServiceHub'
import type { ThemeMode } from '@/services/theme/types'
import { localStorageKey } from '@/constants/localStorage'
// Function to check if OS prefers dark mode
export const checkOSDarkMode = (): boolean => {
return (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
)
}
export type ThemeState = {
activeTheme: AppTheme
setTheme: (theme: AppTheme) => void
isDark: boolean
setIsDark: (isDark: boolean) => void
}
export const useTheme = create<ThemeState>()(
persist(
(set) => {
// Initialize isDark based on OS preference if theme is auto
const initialState = {
activeTheme: 'auto' as AppTheme,
isDark: checkOSDarkMode(),
setTheme: async (activeTheme: AppTheme) => {
if (activeTheme === 'auto') {
const isDarkMode = checkOSDarkMode()
await getServiceHub().theme().setTheme(null)
set(() => ({ activeTheme, isDark: isDarkMode }))
} else {
await getServiceHub().theme().setTheme(activeTheme as ThemeMode)
set(() => ({ activeTheme, isDark: activeTheme === 'dark' }))
}
},
setIsDark: (isDark: boolean) => set(() => ({ isDark })),
}
// Check if we should initialize with dark mode
if (initialState.activeTheme === 'auto') {
initialState.isDark = checkOSDarkMode()
} else {
initialState.isDark = initialState.activeTheme === 'dark'
}
return initialState
},
{
name: localStorageKey.theme,
storage: createJSONStorage(() => localStorage),
}
)
)