mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
🛠️ Refactor svgCard and index files; streamline image handling and improve type definitions
This commit is contained in:
+23
-13
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user