-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathuseLoadTheme.ts
116 lines (101 loc) · 3.5 KB
/
useLoadTheme.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useCallback, useEffect } from 'react'
import { useTheme } from 'next-themes'
import { fs, joinPath } from '@janhq/core'
import { useAtom, useAtomValue } from 'jotai'
import cssVars from '@/utils/jsonToCssVariables'
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import {
selectedThemeIdAtom,
themeDataAtom,
themesOptionsAtom,
} from '@/helpers/atoms/Setting.atom'
import DefaultTheme from '@/theme.json'
type NativeThemeProps = 'light' | 'dark'
export const useLoadTheme = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const [themeOptions, setThemeOptions] = useAtom(themesOptionsAtom)
const [themeData, setThemeData] = useAtom(themeDataAtom)
const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom)
const { setTheme } = useTheme()
const setNativeTheme = useCallback(
(nativeTheme: NativeThemeProps) => {
if (nativeTheme === 'dark') {
window?.electronAPI?.setNativeThemeDark()
setTheme('dark')
localStorage.setItem('nativeTheme', 'dark')
} else {
window?.electronAPI?.setNativeThemeLight()
setTheme('light')
localStorage.setItem('nativeTheme', 'light')
}
},
[setTheme]
)
const applyTheme = (theme: Theme) => {
const variables = cssVars(theme.variables)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
}
const getThemes = useCallback(async () => {
if (!janDataFolderPath.length) return
const folderPath = await joinPath([janDataFolderPath, 'themes'])
const installedThemes = await fs.readdirSync(folderPath)
const themesOptions: { name: string; value: string }[] = installedThemes
.filter((x: string) => x !== '.DS_Store')
.map(async (x: string) => {
const y = await joinPath([`${folderPath}/${x}`, `theme.json`])
const c: Theme = JSON.parse(await fs.readFileSync(y, 'utf-8'))
return { name: c?.displayName, value: c.id }
})
Promise.all(themesOptions).then((results) => {
setThemeOptions(results)
})
if (janDataFolderPath.length > 0) {
if (!selectedIdTheme.length) return setSelectedIdTheme('joi-light')
const filePath = await joinPath([
`${folderPath}/${selectedIdTheme}`,
`theme.json`,
])
const theme: Theme = JSON.parse(await fs.readFileSync(filePath, 'utf-8'))
setThemeData(theme)
setNativeTheme(theme.nativeTheme)
applyTheme(theme)
}
}, [
janDataFolderPath,
selectedIdTheme,
setNativeTheme,
setSelectedIdTheme,
setThemeData,
setThemeOptions,
])
const configureTheme = useCallback(async () => {
if (!window.electronAPI) {
const theme = DefaultTheme as Theme
setThemeData(theme)
const variables = cssVars(theme.variables)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
return
}
if (!themeData || !themeOptions) {
await getThemes()
} else {
applyTheme(themeData)
}
setNativeTheme(themeData?.nativeTheme as NativeThemeProps)
}, [themeData, themeOptions, getThemes, setNativeTheme])
useEffect(() => {
configureTheme()
}, [
configureTheme,
selectedIdTheme,
setNativeTheme,
setSelectedIdTheme,
themeData?.nativeTheme,
])
}