🛠️ Refactor svgCard and index files; streamline image handling and improve type definitions

This commit is contained in:
pheralb
2025-09-01 11:48:51 +01:00
parent 2a38b834c3
commit 1591ea3146
2 changed files with 44 additions and 70 deletions
+23 -13
View File
@@ -1,11 +1,12 @@
import type { iSVG } from "@/types/svg";
import { svgs } from "./svgs";
import type { iSVG, ThemeOptions } from "@/types/svg";
import type { Category } from "@/types/categories";
import { svgs } from "@/data/svgs";
export const svgsData = svgs.map((svg: iSVG, index: number) => {
return { id: index, ...svg };
}) as iSVG[];
export const getCategories = () => {
export const getCategories = (): Category[] => {
const categories = svgs
.flatMap((svg) =>
Array.isArray(svg.category) ? svg.category : [svg.category],
@@ -14,14 +15,23 @@ export const getCategories = () => {
return categories;
};
export const getCategoriesForDirectory = () => {
const categories = svgs
.flatMap((svg) =>
Array.isArray(svg.category) ? svg.category : [svg.category],
)
.filter((category, index, array) => array.indexOf(category) === index)
.map((category) => ({
slug: category.toLowerCase(),
}));
return categories;
export const getSvgsByCategory = (category: string): iSVG[] =>
svgsData.filter((svg: iSVG) => {
if (Array.isArray(svg.category)) {
return svg.category.some(
(categoryItem) => categoryItem.toLowerCase() === category.toLowerCase(),
);
} else {
return svg.category.toLowerCase() === category.toLowerCase();
}
});
interface GetSvgImgUrl {
url: string | ThemeOptions;
isDark: boolean;
}
export const getSvgImgUrl = ({ url, isDark }: GetSvgImgUrl) => {
if (typeof url === "string") return url;
return isDark ? url.dark : url.light;
};