-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiconStore.ts
More file actions
149 lines (123 loc) · 3.96 KB
/
iconStore.ts
File metadata and controls
149 lines (123 loc) · 3.96 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
import { writable, derived, type Writable, type Readable } from "svelte/store"
import type { IIcon } from "$lib/types/types"
export const icons: Writable<IIcon[]> = writable([])
export const availableTags: Writable<string[]> = writable([])
export const searchTerm: Writable<string> = writable("")
export const selectedTags: Writable<string[]> = writable([])
export const isDarkMode: Writable<boolean> = writable(false)
export const selectedIcon: Writable<IIcon | null> = writable(null)
export const isModalOpen: Writable<boolean> = writable(false)
export const isLoading: Writable<boolean> = writable(false)
export const error: Writable<string | null> = writable(null)
export async function fetchIconsData(): Promise<void> {
isLoading.set(true)
error.set(null)
try {
const response = await fetch(
"https://raw.githubusercontent.com/devicons/devicon/master/devicon.json",
)
if (!response.ok) {
throw new Error(`Failed to fetch icons: ${response.statusText}`)
}
const iconsData: IIcon[] = await response.json()
// Extract all available tags
const availableTagsSet = new Set<string>()
iconsData.forEach((icon) => {
if (icon.tags && Array.isArray(icon.tags)) {
icon.tags.forEach((tag) => availableTagsSet.add(tag))
}
})
const sortedTags = Array.from(availableTagsSet).sort()
// Set the data in stores
icons.set(iconsData)
availableTags.set(sortedTags)
// Initialize dark mode preference
initializeDarkMode()
} catch (err: any) {
console.error("Error fetching icons:", err)
error.set(err.message)
icons.set([])
availableTags.set([])
} finally {
isLoading.set(false)
}
}
function initializeDarkMode(): void {
if (typeof window !== "undefined") {
// Check local storage for dark mode preference, default to true if not set
const storedDarkMode = localStorage.getItem("darkMode")
const isDark = storedDarkMode === null ? true : storedDarkMode === "true"
isDarkMode.set(isDark)
// Apply theme to document
if (isDark) {
document.documentElement.classList.add("dark")
} else {
document.documentElement.classList.remove("dark")
}
}
}
export function initializeStore(
initialIcons: IIcon[],
initialTags: string[],
): void {
icons.set(initialIcons)
availableTags.set(initialTags)
initializeDarkMode()
}
export const filteredIcons: Readable<IIcon[]> = derived(
[icons, searchTerm, selectedTags],
([$icons, $searchTerm, $selectedTags]) => {
return $icons.filter((icon) => {
const matchesSearch =
$searchTerm === "" ||
icon.name.toLowerCase().includes($searchTerm.toLowerCase()) ||
icon.altnames.some((altname) =>
altname.toLowerCase().includes($searchTerm.toLowerCase()),
)
const matchesTags =
$selectedTags.length === 0 ||
(icon.tags && $selectedTags.every((tag) => icon.tags!.includes(tag)))
return matchesSearch && matchesTags
})
},
)
export const totalVersions: Readable<number> = derived([icons], ([$icons]) =>
$icons.reduce(
(acc, icon) =>
acc +
(icon?.versions?.font?.length ?? 0) +
(icon?.versions?.svg?.length ?? 0),
0,
),
)
export function setSearchTerm(term: string): void {
searchTerm.set(term)
}
export function setSelectedTags(tags: string[]): void {
selectedTags.set(tags)
}
export function clearFilters(): void {
searchTerm.set("")
selectedTags.set([])
}
export function toggleDarkMode(): void {
isDarkMode.update((current) => {
const newValue = !current
if (typeof window !== "undefined") {
localStorage.setItem("darkMode", newValue.toString())
if (newValue) {
document.documentElement.classList.add("dark")
} else {
document.documentElement.classList.remove("dark")
}
}
return newValue
})
}
export function openIconModal(icon: IIcon): void {
selectedIcon.set(icon)
isModalOpen.set(true)
}
export function closeIconModal(): void {
isModalOpen.set(false)
}