1
- import { getColors , ls , resetTheme , setColorsProperties } from "../lib/utils" ;
2
- import React , { useEffect , useState } from "react" ;
1
+ import { getColors , ls , print } from "../lib/utils" ;
2
+ import { useEffect , useState } from "react" ;
3
3
import { Item } from "./item" ;
4
- import { ThemeWithHSLColor } from "../lib/theme" ;
5
4
import { useTheme } from "next-themes" ;
6
5
import { useDebounceCallback } from "../hooks/useDebounceCallback" ;
7
- import z from "zod" ;
8
6
import { LOCAL_STORAGE_KEY } from "../lib/consts" ;
9
7
import { useEmittor } from "emittor" ;
10
8
import { themeEmittor } from "../lib/emittors" ;
11
-
12
- function print ( ...props : any ) {
13
- if (
14
- typeof window !== "undefined" &&
15
- ( window as any ) . shadcnThemeEditorDebugMode
16
- ) {
17
- console . log ( ...props ) ;
18
- }
19
- }
20
-
21
- const ZodTheme = z . array (
22
- z . object ( {
23
- title : z . string ( ) ,
24
- variable : z . string ( ) ,
25
- color : z . object ( {
26
- h : z . number ( ) ,
27
- s : z . number ( ) ,
28
- l : z . number ( ) ,
29
- } ) ,
30
- } )
31
- ) ;
9
+ import { setSavedTheme } from "../lib/set-saved-theme" ;
10
+ import { useIsMount } from "../hooks/useIsMount" ;
32
11
33
12
function SideBarColors ( ) {
34
-
35
13
const { resolvedTheme } = useTheme ( ) ;
36
14
const [ currentTheme , setCurrentTheme ] = useState < string | undefined > ( ) ;
15
+ const isMount = useIsMount ( ) ;
16
+
37
17
useEffect ( ( ) => {
38
18
setCurrentTheme ( resolvedTheme ) ;
39
19
} , [ resolvedTheme ] ) ;
@@ -45,27 +25,21 @@ function SideBarColors() {
45
25
} , 2000 ) ;
46
26
47
27
useEffect ( ( ) => {
48
- // resetTheme();
49
- print ( "reading theme" , LOCAL_STORAGE_KEY + ":" + currentTheme ) ;
50
- let theme = ls . getLocalStorageItem < ThemeWithHSLColor [ ] > (
51
- LOCAL_STORAGE_KEY + ":" + currentTheme
52
- ) ;
53
- if ( theme ) {
28
+ let isSavedThemeApplied = false ;
29
+ if ( typeof colors == "undefined" || isMount ) {
30
+ // If colors are not defined (i.e., they haven't been set by other functions yet, meaning it's the first time),
31
+ // or if this is due to a re-render caused by dependency changes (e.g., when currentTheme is updated).
54
32
try {
55
- const isValid = ZodTheme . parse ( theme ) ;
56
- print ( "theme is valid and appling" , isValid ) ;
57
- print ( "applied theme" , theme ) ;
58
- themeEmittor . applyTheme ( theme ) ;
33
+ isSavedThemeApplied = setSavedTheme ( currentTheme ) ;
59
34
return ;
60
- } catch ( error ) {
61
- print ( "invalid theme found in localStorage" ) ;
62
- // localStorage.removeItem(LOCAL_STORAGE_KEY+":"+currentTheme); //* remove key
63
- }
35
+ } catch ( error ) { }
36
+ }
37
+
38
+ if ( typeof colors == "undefined" ) {
39
+ themeEmittor . setDefaultTheme ( ) ;
64
40
}
65
- print ( "theme not found in localStorage" ) ;
66
- print ( "Now theme: " , theme ) ;
67
- themeEmittor . setDefaultTheme ( ) ;
68
41
} , [ currentTheme ] ) ;
42
+
69
43
return (
70
44
< >
71
45
{ colors ?. map ( ( color ) => (
0 commit comments