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" ;
33import { Item } from "./item" ;
4- import { ThemeWithHSLColor } from "../lib/theme" ;
54import { useTheme } from "next-themes" ;
65import { useDebounceCallback } from "../hooks/useDebounceCallback" ;
7- import z from "zod" ;
86import { LOCAL_STORAGE_KEY } from "../lib/consts" ;
97import { useEmittor } from "emittor" ;
108import { 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" ;
3211
3312function SideBarColors ( ) {
34-
3513 const { resolvedTheme } = useTheme ( ) ;
3614 const [ currentTheme , setCurrentTheme ] = useState < string | undefined > ( ) ;
15+ const isMount = useIsMount ( ) ;
16+
3717 useEffect ( ( ) => {
3818 setCurrentTheme ( resolvedTheme ) ;
3919 } , [ resolvedTheme ] ) ;
@@ -45,27 +25,21 @@ function SideBarColors() {
4525 } , 2000 ) ;
4626
4727 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).
5432 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 ) ;
5934 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 ( ) ;
6440 }
65- print ( "theme not found in localStorage" ) ;
66- print ( "Now theme: " , theme ) ;
67- themeEmittor . setDefaultTheme ( ) ;
6841 } , [ currentTheme ] ) ;
42+
6943 return (
7044 < >
7145 { colors ?. map ( ( color ) => (
0 commit comments