Skip to content

Commit

Permalink
fix: change theme settings do not work after relocated data folder
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Dec 23, 2024
1 parent abb718c commit 6b4e1bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
7 changes: 1 addition & 6 deletions web/helpers/atoms/Setting.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ export const CHAT_WIDTH = 'chatWidth'
export const themesOptionsAtom = atomWithStorage<
{ name: string; value: string }[]
>(THEME_OPTIONS, [], undefined, { getOnInit: true })
export const janThemesPathAtom = atomWithStorage<string | undefined>(
THEME_PATH,
undefined,
undefined,
{ getOnInit: true }
)

export const selectedThemeIdAtom = atomWithStorage<string>(
THEME,
'',
Expand Down
34 changes: 15 additions & 19 deletions web/hooks/useLoadTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import cssVars from '@/utils/jsonToCssVariables'

import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import {
janThemesPathAtom,
selectedThemeIdAtom,
themeDataAtom,
themesOptionsAtom,
Expand All @@ -21,7 +20,6 @@ type NativeThemeProps = 'light' | 'dark'
export const useLoadTheme = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const [themeOptions, setThemeOptions] = useAtom(themesOptionsAtom)
const [themePath, setThemePath] = useAtom(janThemesPathAtom)
const [themeData, setThemeData] = useAtom(themeDataAtom)
const [selectedIdTheme, setSelectedIdTheme] = useAtom(selectedThemeIdAtom)
const { setTheme } = useTheme()
Expand All @@ -41,6 +39,14 @@ export const useLoadTheme = () => {
[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'])
Expand All @@ -59,7 +65,6 @@ export const useLoadTheme = () => {

if (janDataFolderPath.length > 0) {
if (!selectedIdTheme.length) return setSelectedIdTheme('joi-light')
setThemePath(folderPath)
const filePath = await joinPath([
`${folderPath}/${selectedIdTheme}`,
`theme.json`,
Expand All @@ -68,11 +73,7 @@ export const useLoadTheme = () => {

setThemeData(theme)
setNativeTheme(theme.nativeTheme)
const variables = cssVars(theme.variables)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
applyTheme(theme)
}
}, [
janDataFolderPath,
Expand All @@ -81,26 +82,21 @@ export const useLoadTheme = () => {
setSelectedIdTheme,
setThemeData,
setThemeOptions,
setThemePath,
])

const applyTheme = useCallback(async () => {
if (!themeData || !themeOptions || !themePath) {
const configureTheme = useCallback(async () => {
if (!themeData || !themeOptions) {
await getThemes()
} else {
const variables = cssVars(themeData.variables)
const headTag = document.getElementsByTagName('head')[0]
const styleTag = document.createElement('style')
styleTag.innerHTML = `:root {${variables}}`
headTag.appendChild(styleTag)
applyTheme(themeData)

Check warning on line 91 in web/hooks/useLoadTheme.ts

View workflow job for this annotation

GitHub Actions / coverage-check

91 line is not covered with tests
}
setNativeTheme(themeData?.nativeTheme as NativeThemeProps)
}, [themeData, themeOptions, themePath, getThemes])
}, [themeData, themeOptions, getThemes, setNativeTheme])

useEffect(() => {
applyTheme()
configureTheme()
}, [
applyTheme,
configureTheme,
selectedIdTheme,
setNativeTheme,
setSelectedIdTheme,
Expand Down
8 changes: 5 additions & 3 deletions web/screens/Settings/Appearance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { useAtom, useAtomValue } from 'jotai'

import { twMerge } from 'tailwind-merge'

import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'

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

0 comments on commit 6b4e1bd

Please sign in to comment.