1- import  {  writable  }  from  "svelte/store" ; 
1+ import  storedWritable  from  "@app/lib/localStore" ; 
2+ import  {  boolean ,  literal ,  union ,  z  }  from  "zod" ; 
3+ 
4+ const  themeSchema  =  union ( [ literal ( "dark" ) ,  literal ( "light" ) ] ) ; 
5+ type  Theme  =  z . infer < typeof  themeSchema > ; 
6+ 
7+ export  const  followSystemTheme  =  storedWritable < boolean  |  undefined > ( 
8+   "followSystemTheme" , 
9+   boolean ( ) , 
10+   ! localStorage . getItem ( "theme" ) , 
11+   ! window . localStorage , 
12+ ) ; 
13+ export  const  theme  =  storedWritable < Theme > ( 
14+   "theme" , 
15+   themeSchema , 
16+   loadTheme ( ) , 
17+   ! window . localStorage , 
18+ ) ; 
219
3- export  type  Theme  =  "dark"  |  "light" ; 
4- export  const  followSystemTheme  =  writable < boolean > ( shouldFollowSystemTheme ( ) ) ; 
5- export  const  theme  =  writable < Theme > ( loadTheme ( ) ) ; 
20+ function  loadTheme ( ) : Theme  { 
21+   const  {  matches }  =  window . matchMedia ( "(prefers-color-scheme: dark)" ) ; 
22+ 
23+   return  matches  ? "dark"  : "light" ; 
24+ } 
25+ 
26+ const  codeFontSchema  =  union ( [ literal ( "jetbrains" ) ,  literal ( "system" ) ] ) ; 
27+ type  CodeFont  =  z . infer < typeof  codeFontSchema > ; 
628
7- export  type  CodeFont  =  "jetbrains"  |  "system" ; 
8- export  const  codeFont  =  writable < CodeFont > ( loadCodeFont ( ) ) ; 
29+ export  const  codeFont  =  storedWritable ( 
30+   "codefont" , 
31+   codeFontSchema , 
32+   "jetbrains" , 
33+   ! window . localStorage , 
34+ ) ; 
935
1036export  const  codeFonts : { 
1137  storedName : CodeFont ; 
@@ -19,64 +45,3 @@ export const codeFonts: {
1945  } , 
2046  {  storedName : "system" ,  fontFamily : "monospace" ,  displayName : "System"  } , 
2147] ; 
22- 
23- function  loadCodeFont ( ) : CodeFont  { 
24-   const  storedCodeFont  =  localStorage  ? localStorage . getItem ( "codefont" )  : null ; 
25- 
26-   if  ( storedCodeFont  ===  null )  { 
27-     return  "jetbrains" ; 
28-   }  else  { 
29-     return  storedCodeFont  as  CodeFont ; 
30-   } 
31- } 
32- 
33- function  shouldFollowSystemTheme ( ) : boolean  { 
34-   const  storedTheme  =  localStorage  ? localStorage . getItem ( "theme" )  : null ; 
35-   if  ( storedTheme  ===  null )  { 
36-     return  true ;  // default to following the system theme 
37-   }  else  { 
38-     return  storedTheme  ===  "system" ; 
39-   } 
40- } 
41- 
42- function  loadTheme ( ) : Theme  { 
43-   const  {  matches }  =  window . matchMedia ( "(prefers-color-scheme: dark)" ) ; 
44-   const  storedTheme  =  localStorage  ? localStorage . getItem ( "theme" )  : null ; 
45- 
46-   if  ( storedTheme  ===  null  ||  storedTheme  ===  "system" )  { 
47-     return  matches  ? "dark"  : "light" ; 
48-   }  else  { 
49-     return  storedTheme  as  Theme ; 
50-   } 
51- } 
52- 
53- export  function  storeTheme ( newTheme : Theme  |  "system" ) : void   { 
54-   followSystemTheme . set ( newTheme  ===  "system"  ? true  : false ) ; 
55-   if  ( localStorage )  { 
56-     localStorage . setItem ( "theme" ,  newTheme ) ; 
57-   }  else  { 
58-     console . warn ( 
59-       "localStorage isn't available, not able to persist the selected theme without it." , 
60-     ) ; 
61-   } 
62-   if  ( newTheme  !==  "system" )  { 
63-     // update the theme to newTheme 
64-     theme . set ( newTheme ) ; 
65-   }  else  { 
66-     // update the theme to the current system theme 
67-     theme . set ( 
68-       window . matchMedia ( "(prefers-color-scheme: dark)" )  ? "dark"  : "light" , 
69-     ) ; 
70-   } 
71- } 
72- 
73- export  function  storeCodeFont ( newCodeFont : CodeFont ) : void   { 
74-   codeFont . set ( newCodeFont ) ; 
75-   if  ( localStorage )  { 
76-     localStorage . setItem ( "codefont" ,  newCodeFont ) ; 
77-   }  else  { 
78-     console . warn ( 
79-       "localStorage isn't available, not able to persist the selected code font without it." , 
80-     ) ; 
81-   } 
82- } 
0 commit comments