fix: change theme settings do not work after relocated data folder (#4317)

This commit is contained in:
Louis 2024-12-23 13:09:26 +07:00 committed by GitHub
parent 24222679fb
commit 1f4c00dc41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 28 deletions

View File

@ -20,12 +20,7 @@ export const CHAT_WIDTH = 'chatWidth'
export const themesOptionsAtom = atomWithStorage< export const themesOptionsAtom = atomWithStorage<
{ name: string; value: string }[] { name: string; value: string }[]
>(THEME_OPTIONS, [], undefined, { getOnInit: true }) >(THEME_OPTIONS, [], undefined, { getOnInit: true })
export const janThemesPathAtom = atomWithStorage<string | undefined>(
THEME_PATH,
undefined,
undefined,
{ getOnInit: true }
)
export const selectedThemeIdAtom = atomWithStorage<string>( export const selectedThemeIdAtom = atomWithStorage<string>(
THEME, THEME,
'', '',

View File

@ -10,7 +10,6 @@ import cssVars from '@/utils/jsonToCssVariables'
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom' import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import { import {
janThemesPathAtom,
selectedThemeIdAtom, selectedThemeIdAtom,
themeDataAtom, themeDataAtom,
themesOptionsAtom, themesOptionsAtom,
@ -21,7 +20,6 @@ type NativeThemeProps = 'light' | 'dark'
export const useLoadTheme = () => { export const useLoadTheme = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom) const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const [themeOptions, setThemeOptions] = useAtom(themesOptionsAtom) const [themeOptions, setThemeOptions] = useAtom(themesOptionsAtom)
const [themePath, setThemePath] = useAtom(janThemesPathAtom)
const [themeData, setThemeData] = useAtom(themeDataAtom) const [themeData, setThemeData] = useAtom(themeDataAtom)
const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom) const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom)
const { setTheme } = useTheme() const { setTheme } = useTheme()
@ -41,6 +39,14 @@ export const useLoadTheme = () => {
[setTheme] [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 () => { const getThemes = useCallback(async () => {
if (!janDataFolderPath.length) return if (!janDataFolderPath.length) return
const folderPath = await joinPath([janDataFolderPath, 'themes']) const folderPath = await joinPath([janDataFolderPath, 'themes'])
@ -59,7 +65,6 @@ export const useLoadTheme = () => {
if (janDataFolderPath.length > 0) { if (janDataFolderPath.length > 0) {
if (!selectedIdTheme.length) return setSelectedIdTheme('joi-light') if (!selectedIdTheme.length) return setSelectedIdTheme('joi-light')
setThemePath(folderPath)
const filePath = await joinPath([ const filePath = await joinPath([
`${folderPath}/${selectedIdTheme}`, `${folderPath}/${selectedIdTheme}`,
`theme.json`, `theme.json`,
@ -68,11 +73,7 @@ export const useLoadTheme = () => {
setThemeData(theme) setThemeData(theme)
setNativeTheme(theme.nativeTheme) setNativeTheme(theme.nativeTheme)
const variables = cssVars(theme.variables) applyTheme(theme)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
} }
}, [ }, [
janDataFolderPath, janDataFolderPath,
@ -81,26 +82,21 @@ export const useLoadTheme = () => {
setSelectedIdTheme, setSelectedIdTheme,
setThemeData, setThemeData,
setThemeOptions, setThemeOptions,
setThemePath,
]) ])
const applyTheme = useCallback(async () => { const configureTheme = useCallback(async () => {
if (!themeData || !themeOptions || !themePath) { if (!themeData || !themeOptions) {
await getThemes() await getThemes()
} else { } else {
const variables = cssVars(themeData.variables) applyTheme(themeData)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
} }
setNativeTheme(themeData?.nativeTheme as NativeThemeProps) setNativeTheme(themeData?.nativeTheme as NativeThemeProps)
}, [themeData, themeOptions, themePath, getThemes]) }, [themeData, themeOptions, getThemes, setNativeTheme])
useEffect(() => { useEffect(() => {
applyTheme() configureTheme()
}, [ }, [
applyTheme, configureTheme,
selectedIdTheme, selectedIdTheme,
setNativeTheme, setNativeTheme,
setSelectedIdTheme, setSelectedIdTheme,

View File

@ -8,9 +8,10 @@ import { useAtom, useAtomValue } from 'jotai'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import { import {
chatWidthAtom, chatWidthAtom,
janThemesPathAtom,
reduceTransparentAtom, reduceTransparentAtom,
selectedThemeIdAtom, selectedThemeIdAtom,
spellCheckAtom, spellCheckAtom,
@ -21,8 +22,8 @@ import {
export default function AppearanceOptions() { export default function AppearanceOptions() {
const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom) const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom)
const themeOptions = useAtomValue(themesOptionsAtom) const themeOptions = useAtomValue(themesOptionsAtom)
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const { setTheme, theme } = useTheme() const { setTheme, theme } = useTheme()
const janThemesPath = useAtomValue(janThemesPathAtom)
const [themeData, setThemeData] = useAtom(themeDataAtom) const [themeData, setThemeData] = useAtom(themeDataAtom)
const [reduceTransparent, setReduceTransparent] = useAtom( const [reduceTransparent, setReduceTransparent] = useAtom(
reduceTransparentAtom reduceTransparentAtom
@ -48,6 +49,7 @@ export default function AppearanceOptions() {
const handleClickTheme = useCallback( const handleClickTheme = useCallback(
async (e: string) => { async (e: string) => {
setSelectedIdTheme(e) setSelectedIdTheme(e)
const janThemesPath = await joinPath([janDataFolderPath, 'themes'])
const filePath = await joinPath([`${janThemesPath}/${e}`, `theme.json`]) const filePath = await joinPath([`${janThemesPath}/${e}`, `theme.json`])
const theme: Theme = JSON.parse(await fs.readFileSync(filePath, 'utf-8')) const theme: Theme = JSON.parse(await fs.readFileSync(filePath, 'utf-8'))
setThemeData(theme) setThemeData(theme)
@ -59,7 +61,7 @@ export default function AppearanceOptions() {
} }
}, },
[ [
janThemesPath, janDataFolderPath,
reduceTransparent, reduceTransparent,
setReduceTransparent, setReduceTransparent,
setSelectedIdTheme, setSelectedIdTheme,