2025-04-16 16:18:20 +02:00
|
|
|
import { METADATA_URL } from "@/constants"
|
|
|
|
import type { IconFile, IconWithName } from "@/types/icons"
|
|
|
|
/**
|
|
|
|
* Fetches all icon data from the metadata.json file
|
|
|
|
*/
|
|
|
|
|
|
|
|
export async function getAllIcons(): Promise<IconFile> {
|
|
|
|
const file = await fetch(METADATA_URL)
|
|
|
|
return (await file.json()) as IconFile
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of all icon names.
|
|
|
|
*/
|
|
|
|
export const getIconNames = async (): Promise<string[]> => {
|
|
|
|
const iconsData = await getAllIcons()
|
|
|
|
return Object.keys(iconsData)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts icon data to an array format for easier rendering
|
|
|
|
*/
|
|
|
|
export async function getIconsArray(): Promise<IconWithName[]> {
|
|
|
|
const iconsData = await getAllIcons()
|
|
|
|
|
|
|
|
return Object.entries(iconsData)
|
|
|
|
.map(([name, data]) => ({
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
}))
|
|
|
|
.sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches data for a specific icon
|
|
|
|
*/
|
|
|
|
export async function getIconData(iconName: string): Promise<IconWithName | null> {
|
|
|
|
const iconsData = await getAllIcons()
|
|
|
|
const iconData = iconsData[iconName]
|
|
|
|
|
|
|
|
if (!iconData) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: iconName,
|
|
|
|
data: iconData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches author data from GitHub API
|
|
|
|
*/
|
|
|
|
export async function getAuthorData(authorId: number) {
|
|
|
|
const response = await fetch(`https://api.github.com/user/${authorId}`, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
|
|
|
|
"Cache-Control": "public, max-age=86400",
|
|
|
|
},
|
|
|
|
next: { revalidate: 86400 }, // Revalidate cache once a day
|
|
|
|
})
|
|
|
|
return response.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches featured icons for the homepage
|
|
|
|
*/
|
|
|
|
export async function getTotalIcons() {
|
|
|
|
const iconsData = await getAllIcons()
|
|
|
|
|
|
|
|
return {
|
|
|
|
totalIcons: Object.keys(iconsData).length,
|
|
|
|
}
|
|
|
|
}
|
2025-04-17 02:43:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches recently added icons sorted by timestamp
|
|
|
|
*/
|
|
|
|
export async function getRecentlyAddedIcons(limit = 8): Promise<IconWithName[]> {
|
|
|
|
const icons = await getIconsArray()
|
|
|
|
|
|
|
|
return icons
|
|
|
|
.sort((a, b) => {
|
|
|
|
// Sort by timestamp in descending order (newest first)
|
|
|
|
return new Date(b.data.update.timestamp).getTime() - new Date(a.data.update.timestamp).getTime()
|
|
|
|
})
|
|
|
|
.slice(0, limit)
|
|
|
|
}
|