2022-12-23 18:06:19 -05:00
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
2023-01-03 12:46:37 -05:00
|
|
|
import type { OverallThemeMeta } from '../../../../../types/project-settings'
|
2023-11-21 09:29:44 -05:00
|
|
|
import { saveUserSettings } from '../utils/api'
|
|
|
|
import { UserSettings } from '../../../../../types/user-settings'
|
|
|
|
import { useUserSettingsContext } from '@/shared/context/user-settings-context'
|
2024-06-18 06:01:37 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
2024-09-06 08:36:15 -04:00
|
|
|
import { isBootstrap5 } from '@/features/utils/bootstrap-5'
|
2022-12-23 18:06:19 -05:00
|
|
|
|
|
|
|
export default function useSetOverallTheme() {
|
|
|
|
const [chosenTheme, setChosenTheme] = useState<OverallThemeMeta | null>(null)
|
2022-12-23 18:43:46 -05:00
|
|
|
const [loadingStyleSheet, setLoadingStyleSheet] = useScopeValue<boolean>(
|
|
|
|
'ui.loadingStyleSheet'
|
|
|
|
)
|
2023-11-21 09:29:44 -05:00
|
|
|
|
|
|
|
const { userSettings, setUserSettings } = useUserSettingsContext()
|
|
|
|
const { overallTheme } = userSettings
|
|
|
|
|
|
|
|
const setOverallTheme = useCallback(
|
|
|
|
(overallTheme: UserSettings['overallTheme']) => {
|
|
|
|
setUserSettings(settings => ({ ...settings, overallTheme }))
|
|
|
|
},
|
|
|
|
[setUserSettings]
|
|
|
|
)
|
2022-12-23 18:06:19 -05:00
|
|
|
|
2024-09-06 08:36:15 -04:00
|
|
|
const skipLoadingStyleSheet = isBootstrap5()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Sets `data-theme` attribute to the body element, needed for Bootstrap 5 theming
|
|
|
|
const theme = overallTheme === 'light-' ? 'light' : 'default'
|
|
|
|
document.body.dataset.theme = theme
|
|
|
|
}, [overallTheme])
|
|
|
|
|
2022-12-23 18:06:19 -05:00
|
|
|
useEffect(() => {
|
2024-09-06 08:36:15 -04:00
|
|
|
if (skipLoadingStyleSheet) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:06:19 -05:00
|
|
|
const docHeadEl = document.querySelector('head')
|
|
|
|
const oldStyleSheetEl = document.getElementById('main-stylesheet')
|
|
|
|
|
|
|
|
const newStyleSheetEl = document.createElement('link')
|
|
|
|
newStyleSheetEl.setAttribute('rel', 'stylesheet')
|
|
|
|
newStyleSheetEl.setAttribute('id', 'main-stylesheet')
|
|
|
|
newStyleSheetEl.setAttribute('href', chosenTheme?.path ?? '')
|
|
|
|
|
|
|
|
const loadEventCallback = () => {
|
|
|
|
setLoadingStyleSheet(false)
|
|
|
|
|
|
|
|
if (docHeadEl && oldStyleSheetEl) {
|
|
|
|
docHeadEl.removeChild(oldStyleSheetEl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadingStyleSheet) {
|
|
|
|
newStyleSheetEl.addEventListener('load', loadEventCallback, {
|
|
|
|
once: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
docHeadEl?.appendChild(newStyleSheetEl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
newStyleSheetEl.removeEventListener('load', loadEventCallback)
|
|
|
|
}
|
2024-09-06 08:36:15 -04:00
|
|
|
}, [
|
|
|
|
loadingStyleSheet,
|
|
|
|
setLoadingStyleSheet,
|
|
|
|
skipLoadingStyleSheet,
|
|
|
|
chosenTheme?.path,
|
|
|
|
])
|
2022-12-23 18:06:19 -05:00
|
|
|
|
2023-01-04 07:17:50 -05:00
|
|
|
return useCallback(
|
|
|
|
(newOverallTheme: UserSettings['overallTheme']) => {
|
|
|
|
if (overallTheme !== newOverallTheme) {
|
2022-12-23 18:06:19 -05:00
|
|
|
const chosenTheme = _.find(
|
2024-06-18 06:01:37 -04:00
|
|
|
getMeta('ol-overallThemes'),
|
2023-01-04 07:17:50 -05:00
|
|
|
theme => theme.val === newOverallTheme
|
2022-12-23 18:06:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if (chosenTheme) {
|
2024-09-06 08:36:15 -04:00
|
|
|
if (!skipLoadingStyleSheet) {
|
|
|
|
setLoadingStyleSheet(true)
|
|
|
|
}
|
2022-12-23 18:06:19 -05:00
|
|
|
setChosenTheme(chosenTheme)
|
2023-01-04 07:17:50 -05:00
|
|
|
setOverallTheme(newOverallTheme)
|
2023-01-06 09:29:03 -05:00
|
|
|
saveUserSettings('overallTheme', newOverallTheme)
|
2022-12-23 18:06:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-09-06 08:36:15 -04:00
|
|
|
[overallTheme, setLoadingStyleSheet, skipLoadingStyleSheet, setOverallTheme]
|
2022-12-23 18:06:19 -05:00
|
|
|
)
|
|
|
|
}
|