Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import {
janThemesPathAtom,
selectedThemeIdAtom,
themeDataAtom,
themesOptionsAtom,
Expand All @@ -21,7 +20,6 @@
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 @@ -29,9 +27,9 @@
const setNativeTheme = useCallback(
(nativeTheme: NativeThemeProps) => {
if (nativeTheme === 'dark') {
window?.electronAPI?.setNativeThemeDark()
setTheme('dark')
localStorage.setItem('nativeTheme', 'dark')

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

View workflow job for this annotation

GitHub Actions / coverage-check

30-32 lines are not covered with tests
} else {
window?.electronAPI?.setNativeThemeLight()
setTheme('light')
Expand All @@ -41,6 +39,14 @@
[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 @@

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 @@

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 @@
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
Loading