@@ -8,26 +8,76 @@ export const selectedTags: Writable<string[]> = writable([])
88export const isDarkMode : Writable < boolean > = writable ( false )
99export const selectedIcon : Writable < IIcon | null > = writable ( null )
1010export const isModalOpen : Writable < boolean > = writable ( false )
11+ export const isLoading : Writable < boolean > = writable ( false )
12+ export const error : Writable < string | null > = writable ( null )
1113
12- export function initializeStore (
13- initialIcons : IIcon [ ] ,
14- initialTags : string [ ] ,
15- ) : void {
16- icons . set ( initialIcons )
17- availableTags . set ( initialTags )
14+ export async function fetchIconsData ( ) : Promise < void > {
15+ isLoading . set ( true )
16+ error . set ( null )
17+
18+ try {
19+ const response = await fetch (
20+ "https://raw.githubusercontent.com/devicons/devicon/master/devicon.json" ,
21+ )
22+
23+ if ( ! response . ok ) {
24+ throw new Error ( `Failed to fetch icons: ${ response . statusText } ` )
25+ }
26+
27+ const iconsData : IIcon [ ] = await response . json ( )
28+
29+ // Extract all available tags
30+ const availableTagsSet = new Set < string > ( )
31+ iconsData . forEach ( ( icon ) => {
32+ if ( icon . tags && Array . isArray ( icon . tags ) ) {
33+ icon . tags . forEach ( ( tag ) => availableTagsSet . add ( tag ) )
34+ }
35+ } )
1836
19- // Check local storage for dark mode preference
37+ const sortedTags = Array . from ( availableTagsSet ) . sort ( )
38+
39+ // Set the data in stores
40+ icons . set ( iconsData )
41+ availableTags . set ( sortedTags )
42+
43+ // Initialize dark mode preference
44+ initializeDarkMode ( )
45+ } catch ( err : any ) {
46+ console . error ( "Error fetching icons:" , err )
47+ error . set ( err . message )
48+ icons . set ( [ ] )
49+ availableTags . set ( [ ] )
50+ } finally {
51+ isLoading . set ( false )
52+ }
53+ }
54+
55+ function initializeDarkMode ( ) : void {
2056 if ( typeof window !== "undefined" ) {
21- const storedDarkMode = localStorage . getItem ( "darkMode" ) === "true"
22- isDarkMode . set ( storedDarkMode )
57+ // Check local storage for dark mode preference, default to true if not set
58+ const storedDarkMode = localStorage . getItem ( "darkMode" )
59+ const isDark = storedDarkMode === null ? true : storedDarkMode === "true"
60+
61+ isDarkMode . set ( isDark )
2362
2463 // Apply theme to document
25- if ( storedDarkMode ) {
64+ if ( isDark ) {
2665 document . documentElement . classList . add ( "dark" )
66+ } else {
67+ document . documentElement . classList . remove ( "dark" )
2768 }
2869 }
2970}
3071
72+ export function initializeStore (
73+ initialIcons : IIcon [ ] ,
74+ initialTags : string [ ] ,
75+ ) : void {
76+ icons . set ( initialIcons )
77+ availableTags . set ( initialTags )
78+ initializeDarkMode ( )
79+ }
80+
3181export const filteredIcons : Readable < IIcon [ ] > = derived (
3282 [ icons , searchTerm , selectedTags ] ,
3383 ( [ $icons , $searchTerm , $selectedTags ] ) => {
0 commit comments