forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-customization-provider.tsx
More file actions
262 lines (236 loc) · 7.59 KB
/
Copy paththeme-customization-provider.tsx
File metadata and controls
262 lines (236 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react'
import { getCookie, removeCookie, setCookie } from '@/lib/cookies'
import {
CONTENT_LAYOUT_VALUES,
type ContentLayout,
DEFAULT_THEME_CUSTOMIZATION,
resolveThemeFont,
THEME_COOKIE_KEYS,
THEME_FONT_VALUES,
THEME_PRESET_VALUES,
THEME_RADIUS_VALUES,
THEME_SCALE_VALUES,
type ThemeCustomization,
type ThemeFont,
type ThemePreset,
type ThemeRadius,
type ThemeScale,
} from '@/lib/theme-customization'
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365 // 1 year
function readCookie<T extends string>(
name: string,
allowed: ReadonlySet<T>,
fallback: T
): T {
const value = getCookie(name)
return value && allowed.has(value as T) ? (value as T) : fallback
}
function applyAttribute(name: string, value: string | null) {
if (typeof document === 'undefined') return
const body = document.body
if (!body) return
if (value === null) {
body.removeAttribute(name)
} else {
body.setAttribute(name, value)
}
}
type ThemeCustomizationContextType = {
defaults: ThemeCustomization
customization: ThemeCustomization
setPreset: (preset: ThemePreset) => void
setFont: (font: ThemeFont) => void
setRadius: (radius: ThemeRadius) => void
setScale: (scale: ThemeScale) => void
setContentLayout: (contentLayout: ContentLayout) => void
resetCustomization: () => void
}
// Fallback used when a consumer renders outside the provider (e.g. an error
// route mounted before providers are ready, or stale HMR boundaries). Keeping
// it permissive prevents the whole tree from crashing — the UI just behaves
// like the defaults until the real provider re-mounts.
const FALLBACK_CONTEXT: ThemeCustomizationContextType = {
defaults: DEFAULT_THEME_CUSTOMIZATION,
customization: DEFAULT_THEME_CUSTOMIZATION,
setPreset: () => {},
setFont: () => {},
setRadius: () => {},
setScale: () => {},
setContentLayout: () => {},
resetCustomization: () => {},
}
const ThemeCustomizationContext =
createContext<ThemeCustomizationContextType>(FALLBACK_CONTEXT)
export function ThemeCustomizationProvider(props: {
children: React.ReactNode
}) {
const [preset, _setPreset] = useState<ThemePreset>(() =>
readCookie<ThemePreset>(
THEME_COOKIE_KEYS.preset,
THEME_PRESET_VALUES,
DEFAULT_THEME_CUSTOMIZATION.preset
)
)
const [font, _setFont] = useState<ThemeFont>(() =>
readCookie<ThemeFont>(
THEME_COOKIE_KEYS.font,
THEME_FONT_VALUES,
DEFAULT_THEME_CUSTOMIZATION.font
)
)
const [radius, _setRadius] = useState<ThemeRadius>(() =>
readCookie<ThemeRadius>(
THEME_COOKIE_KEYS.radius,
THEME_RADIUS_VALUES,
DEFAULT_THEME_CUSTOMIZATION.radius
)
)
const [scale, _setScale] = useState<ThemeScale>(() =>
readCookie<ThemeScale>(
THEME_COOKIE_KEYS.scale,
THEME_SCALE_VALUES,
DEFAULT_THEME_CUSTOMIZATION.scale
)
)
const [contentLayout, _setContentLayout] = useState<ContentLayout>(() =>
readCookie<ContentLayout>(
THEME_COOKIE_KEYS.contentLayout,
CONTENT_LAYOUT_VALUES,
DEFAULT_THEME_CUSTOMIZATION.contentLayout
)
)
// Mirror state to the <body> via data-* attributes so theme-presets.css can
// override CSS variables at the right cascade layer.
useEffect(() => {
applyAttribute(
'data-theme-preset',
preset === DEFAULT_THEME_CUSTOMIZATION.preset ? null : preset
)
}, [preset])
// Font is the one axis where we resolve before writing the attribute:
// the persisted preference may be `default`, but CSS works in terms of
// the concrete `sans`/`serif` choice that should drive the cascade.
// Resolving here (instead of in CSS via `:not()` selectors) keeps the
// stylesheet to one simple `[data-theme-font='serif']` selector and lets
// future presets opt into typography via `PRESET_DEFAULT_FONT` alone.
useEffect(() => {
applyAttribute('data-theme-font', resolveThemeFont(font, preset))
}, [font, preset])
useEffect(() => {
applyAttribute(
'data-theme-radius',
radius === DEFAULT_THEME_CUSTOMIZATION.radius ? null : radius
)
}, [radius])
useEffect(() => {
applyAttribute(
'data-theme-scale',
scale === DEFAULT_THEME_CUSTOMIZATION.scale ? null : scale
)
}, [scale])
useEffect(() => {
applyAttribute('data-theme-content-layout', contentLayout)
}, [contentLayout])
const setPreset = useCallback((value: ThemePreset) => {
_setPreset(value)
if (value === DEFAULT_THEME_CUSTOMIZATION.preset) {
removeCookie(THEME_COOKIE_KEYS.preset)
} else {
setCookie(THEME_COOKIE_KEYS.preset, value, COOKIE_MAX_AGE)
}
}, [])
const setFont = useCallback((value: ThemeFont) => {
_setFont(value)
if (value === DEFAULT_THEME_CUSTOMIZATION.font) {
removeCookie(THEME_COOKIE_KEYS.font)
} else {
setCookie(THEME_COOKIE_KEYS.font, value, COOKIE_MAX_AGE)
}
}, [])
const setRadius = useCallback((value: ThemeRadius) => {
_setRadius(value)
if (value === DEFAULT_THEME_CUSTOMIZATION.radius) {
removeCookie(THEME_COOKIE_KEYS.radius)
} else {
setCookie(THEME_COOKIE_KEYS.radius, value, COOKIE_MAX_AGE)
}
}, [])
const setScale = useCallback((value: ThemeScale) => {
_setScale(value)
if (value === DEFAULT_THEME_CUSTOMIZATION.scale) {
removeCookie(THEME_COOKIE_KEYS.scale)
} else {
setCookie(THEME_COOKIE_KEYS.scale, value, COOKIE_MAX_AGE)
}
}, [])
const setContentLayout = useCallback((value: ContentLayout) => {
_setContentLayout(value)
if (value === DEFAULT_THEME_CUSTOMIZATION.contentLayout) {
removeCookie(THEME_COOKIE_KEYS.contentLayout)
} else {
setCookie(THEME_COOKIE_KEYS.contentLayout, value, COOKIE_MAX_AGE)
}
}, [])
const resetCustomization = useCallback(() => {
setPreset(DEFAULT_THEME_CUSTOMIZATION.preset)
setFont(DEFAULT_THEME_CUSTOMIZATION.font)
setRadius(DEFAULT_THEME_CUSTOMIZATION.radius)
setScale(DEFAULT_THEME_CUSTOMIZATION.scale)
setContentLayout(DEFAULT_THEME_CUSTOMIZATION.contentLayout)
}, [setPreset, setFont, setRadius, setScale, setContentLayout])
const value = useMemo<ThemeCustomizationContextType>(
() => ({
defaults: DEFAULT_THEME_CUSTOMIZATION,
customization: { preset, font, radius, scale, contentLayout },
setPreset,
setFont,
setRadius,
setScale,
setContentLayout,
resetCustomization,
}),
[
preset,
font,
radius,
scale,
contentLayout,
setPreset,
setFont,
setRadius,
setScale,
setContentLayout,
resetCustomization,
]
)
return (
<ThemeCustomizationContext.Provider value={value}>
{props.children}
</ThemeCustomizationContext.Provider>
)
}
// eslint-disable-next-line react-refresh/only-export-components
export function useThemeCustomization() {
return useContext(ThemeCustomizationContext)
}