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: Glitch theme applies on launch #4168

Merged
merged 2 commits into from
Dec 2, 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
21 changes: 18 additions & 3 deletions web/helpers/atoms/Setting.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@ export const janSettingScreenAtom = atom<SettingScreen[]>([])
export const THEME = 'themeAppearance'
export const REDUCE_TRANSPARENT = 'reduceTransparent'
export const SPELL_CHECKING = 'spellChecking'
export const themesOptionsAtom = atom<{ name: string; value: string }[]>([])
export const janThemesPathAtom = atom<string | undefined>(undefined)
export const THEME_DATA = 'themeData'
export const THEME_OPTIONS = 'themeOptions'
export const THEME_PATH = 'themePath'
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,
'',
undefined,
{ getOnInit: true }
)
export const themeDataAtom = atom<Theme | undefined>(undefined)
export const themeDataAtom = atomWithStorage<Theme | undefined>(
THEME_DATA,
undefined,
undefined,
{ getOnInit: true }
)
export const reduceTransparentAtom = atomWithStorage<boolean>(
REDUCE_TRANSPARENT,
false,
Expand Down
26 changes: 23 additions & 3 deletions web/hooks/useLoadTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { fs, joinPath } from '@janhq/core'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'

import { useLoadTheme } from './useLoadTheme'
import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import {
selectedThemeIdAtom,
themeDataAtom,
} from '@/helpers/atoms/Setting.atom'

// Mock dependencies
jest.mock('next-themes')
Expand Down Expand Up @@ -36,10 +41,25 @@ describe('useLoadTheme', () => {

it('should load theme and set variables', async () => {
// Mock Jotai hooks
;(useAtomValue as jest.Mock).mockReturnValue(mockJanDataFolderPath)
;(useAtomValue as jest.Mock).mockImplementation((atom) => {
switch (atom) {
case janDataFolderPathAtom:
return mockJanDataFolderPath
default:
return undefined
}
})
;(useSetAtom as jest.Mock).mockReturnValue(jest.fn())
;(useAtom as jest.Mock).mockReturnValue([mockSelectedThemeId, jest.fn()])
;(useAtom as jest.Mock).mockReturnValue([mockThemeData, jest.fn()])
;(useAtom as jest.Mock).mockImplementation((atom) => {
switch (atom) {
case selectedThemeIdAtom:
return [mockSelectedThemeId, jest.fn()]
case themeDataAtom:
return [mockThemeData, jest.fn()]
default:
return [undefined, jest.fn()]
}
})

// Mock fs and joinPath
;(fs.readdirSync as jest.Mock).mockResolvedValue(['joi-light', 'joi-dark'])
Expand Down
24 changes: 18 additions & 6 deletions web/hooks/useLoadTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { fs, joinPath } from '@janhq/core'

import { useAtom, useAtomValue, useSetAtom } from 'jotai'
import { useAtom, useAtomValue } from 'jotai'

import cssVars from '@/utils/jsonToCssVariables'

Expand All @@ -20,8 +20,8 @@

export const useLoadTheme = () => {
const janDataFolderPath = useAtomValue(janDataFolderPathAtom)
const setThemeOptions = useSetAtom(themesOptionsAtom)
const setThemePath = useSetAtom(janThemesPathAtom)
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 +29,9 @@
const setNativeTheme = useCallback(
(nativeTheme: NativeThemeProps) => {
if (nativeTheme === 'dark') {
window?.electronAPI?.setNativeThemeDark()
setTheme('dark')
localStorage.setItem('nativeTheme', 'dark')

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

View workflow job for this annotation

GitHub Actions / coverage-check

32-34 lines are not covered with tests
} else {
window?.electronAPI?.setNativeThemeLight()
setTheme('light')
Expand Down Expand Up @@ -84,11 +84,23 @@
setThemePath,
])

useEffect(() => {
getThemes()
const applyTheme = useCallback(async () => {
if (!themeData || !themeOptions || !themePath) {
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)

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

View workflow job for this annotation

GitHub Actions / coverage-check

91-95 lines are not covered with tests
}
setNativeTheme(themeData?.nativeTheme as NativeThemeProps)
}, [themeData, themeOptions, themePath, getThemes])

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

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

React Hook useCallback has a missing dependency: 'setNativeTheme'. Either include it or remove the dependency array

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

View workflow job for this annotation

GitHub Actions / test-on-macos

React Hook useCallback has a missing dependency: 'setNativeTheme'. Either include it or remove the dependency array

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

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

React Hook useCallback has a missing dependency: 'setNativeTheme'. Either include it or remove the dependency array

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

View workflow job for this annotation

GitHub Actions / coverage-check

React Hook useCallback has a missing dependency: 'setNativeTheme'. Either include it or remove the dependency array

useEffect(() => {
applyTheme()
}, [
getThemes,
applyTheme,
selectedIdTheme,
setNativeTheme,
setSelectedIdTheme,
Expand Down
Loading