ThemeContext-DGVIadPK.js 3.6 KB

1
  1. import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{a as t,k as n,w as r}from"./client-DSw2TdH4.js";import{n as i}from"./AuthContext-DM3YxV_q.js";var a=e(n(),1),o=r(),s=(0,a.createContext)(void 0);function c({children:e}){let{authEnabled:n,user:r,loading:c,hasPermission:l}=i(),u=l(`settings:read`),[d,f]=(0,a.useState)(()=>{let e=localStorage.getItem(`theme-mode`),t=localStorage.getItem(`theme`);return e||t||`dark`}),[p,m]=(0,a.useState)(()=>window.matchMedia(`(prefers-color-scheme: dark)`).matches?`dark`:`light`);(0,a.useEffect)(()=>{let e=window.matchMedia(`(prefers-color-scheme: dark)`),t=e=>{m(e.matches?`dark`:`light`)};return e.addEventListener(`change`,t),()=>e.removeEventListener(`change`,t)},[]);let h=d===`system`?p:d,[g,_]=(0,a.useState)(()=>localStorage.getItem(`dark-style`)||`vibrant`),[v,y]=(0,a.useState)(()=>localStorage.getItem(`dark-background`)||`cool`),[b,x]=(0,a.useState)(()=>localStorage.getItem(`dark-accent`)||`green`),[S,C]=(0,a.useState)(()=>localStorage.getItem(`light-style`)||`classic`),[w,T]=(0,a.useState)(()=>localStorage.getItem(`light-background`)||`neutral`),[E,D]=(0,a.useState)(()=>localStorage.getItem(`light-accent`)||`green`),[O,k]=(0,a.useState)(()=>localStorage.getItem(`progress-in-title`)===`true`);return(0,a.useEffect)(()=>{c||n&&r===null||(u?t.getSettings():t.getUiSettings()).then(e=>{e.dark_style&&(_(e.dark_style),localStorage.setItem(`dark-style`,e.dark_style)),e.dark_background&&(y(e.dark_background),localStorage.setItem(`dark-background`,e.dark_background)),e.dark_accent&&(x(e.dark_accent),localStorage.setItem(`dark-accent`,e.dark_accent)),e.light_style&&(C(e.light_style),localStorage.setItem(`light-style`,e.light_style)),e.light_background&&(T(e.light_background),localStorage.setItem(`light-background`,e.light_background)),e.light_accent&&(D(e.light_accent),localStorage.setItem(`light-accent`,e.light_accent))}).catch(()=>{})},[c,n,r,u]),(0,a.useEffect)(()=>{let e=document.documentElement;e.classList.remove(`dark`,`style-classic`,`style-glow`,`style-vibrant`,`bg-neutral`,`bg-warm`,`bg-cool`,`bg-oled`,`bg-slate`,`bg-forest`,`accent-green`,`accent-teal`,`accent-blue`,`accent-orange`,`accent-purple`,`accent-red`),h===`dark`?(e.classList.add(`dark`),e.classList.add(`style-${g}`),e.classList.add(`bg-${v}`),e.classList.add(`accent-${b}`)):(e.classList.add(`style-${S}`),e.classList.add(`bg-${w}`),e.classList.add(`accent-${E}`)),localStorage.setItem(`theme-mode`,d),localStorage.removeItem(`theme`)},[d,h,g,v,b,S,w,E]),(0,o.jsx)(s.Provider,{value:{mode:d,resolvedMode:h,darkStyle:g,darkBackground:v,darkAccent:b,lightStyle:S,lightBackground:w,lightAccent:E,progressInTitle:O,setProgressInTitle:e=>{k(e),localStorage.setItem(`progress-in-title`,String(e))},toggleMode:()=>f(e=>e===`dark`?`light`:e===`light`?`system`:`dark`),setMode:e=>f(e),setDarkStyle:e=>{_(e),localStorage.setItem(`dark-style`,e),t.updateSettings({dark_style:e}).catch(()=>{})},setDarkBackground:e=>{y(e),localStorage.setItem(`dark-background`,e),t.updateSettings({dark_background:e}).catch(()=>{})},setDarkAccent:e=>{x(e),localStorage.setItem(`dark-accent`,e),t.updateSettings({dark_accent:e}).catch(()=>{})},setLightStyle:e=>{C(e),localStorage.setItem(`light-style`,e),t.updateSettings({light_style:e}).catch(()=>{})},setLightBackground:e=>{T(e),localStorage.setItem(`light-background`,e),t.updateSettings({light_background:e}).catch(()=>{})},setLightAccent:e=>{D(e),localStorage.setItem(`light-accent`,e),t.updateSettings({light_accent:e}).catch(()=>{})}},children:e})}function l(){let e=(0,a.useContext)(s);if(!e)throw Error(`useTheme must be used within ThemeProvider`);return e}export{l as n,c as t};