refactor: rework assets fetching
This commit is contained in:
@@ -1,48 +1,48 @@
|
||||
import { File, Icon } from "./types";
|
||||
import { type Child, type Icon } from "./tree";
|
||||
|
||||
export const getIcon = (file: File | string | undefined): Icon => {
|
||||
if (file === undefined) return ICONS["UNKNOWN"];
|
||||
export const getIcon = (file: Child | string | undefined): Icon => {
|
||||
if (!file) return DEFAULT_ICON;
|
||||
|
||||
let iconName;
|
||||
if (typeof file === "string") {
|
||||
const parts = file.split(".");
|
||||
iconName = parts[parts.length - 1];
|
||||
} else {
|
||||
iconName = file.icon;
|
||||
if (!iconName) {
|
||||
const parts = file.name.split(".");
|
||||
iconName = parts[parts.length - 1];
|
||||
}
|
||||
const iconName = parts[parts.length - 1];
|
||||
|
||||
return ICONS[EXT_TO_LANGUAGE[iconName]] ?? DEFAULT_ICON;
|
||||
}
|
||||
|
||||
if (!ICONS[iconName]) iconName = "UNKNOWN";
|
||||
return ICONS[iconName];
|
||||
return file.icon ?? DEFAULT_ICON;
|
||||
};
|
||||
|
||||
export const EXT_TO_LANGUAGE: Record<string, string> = {
|
||||
asc: "Key",
|
||||
md: "Markdown",
|
||||
};
|
||||
|
||||
export const ICONS: Record<string, Icon> = {
|
||||
md: {
|
||||
Markdown: {
|
||||
char: " ",
|
||||
color: "#89bafa",
|
||||
},
|
||||
asc: {
|
||||
Key: {
|
||||
char: " ",
|
||||
color: "#f9e2af",
|
||||
},
|
||||
ts: {
|
||||
TypeScript: {
|
||||
char: " ",
|
||||
color: "#4d86a2",
|
||||
},
|
||||
rs: {
|
||||
Rust: {
|
||||
char: " ",
|
||||
color: "#be8f78",
|
||||
},
|
||||
instagram: {
|
||||
Instagram: {
|
||||
char: " ",
|
||||
color: "#e1306c",
|
||||
},
|
||||
github: {
|
||||
Github: {
|
||||
char: " ",
|
||||
color: "#ffffff",
|
||||
},
|
||||
UNKNOWN: { char: " ", color: "#f599ae" },
|
||||
};
|
||||
|
||||
export const DEFAULT_ICON = { char: " ", color: "#f599ae" };
|
||||
|
||||
90
src/utils/tree.ts
Normal file
90
src/utils/tree.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { DEFAULT_ICON, ICONS } from "./icons";
|
||||
|
||||
export type Icon = {
|
||||
char: string;
|
||||
color: string;
|
||||
};
|
||||
|
||||
export type Project = {
|
||||
type: "project";
|
||||
name: string;
|
||||
url: string;
|
||||
private: boolean;
|
||||
content: string;
|
||||
icon: Icon;
|
||||
};
|
||||
|
||||
export type File = {
|
||||
type: "file";
|
||||
name: string;
|
||||
content: string;
|
||||
icon: Icon;
|
||||
};
|
||||
|
||||
export type Link = {
|
||||
type: "link";
|
||||
name: string;
|
||||
url: string;
|
||||
icon: Icon;
|
||||
};
|
||||
|
||||
export type Folder = {
|
||||
name: string;
|
||||
type: "folder";
|
||||
children: Array<Child>;
|
||||
opened: boolean;
|
||||
};
|
||||
|
||||
export type Child = Link | Project | File;
|
||||
|
||||
export const sortFiles = (files: Array<Folder | Child>) =>
|
||||
files
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
.sort((a, b) =>
|
||||
a.type === "folder" && b.type !== "folder"
|
||||
? -1
|
||||
: a.type !== "folder" && b.type === "folder"
|
||||
? 1
|
||||
: 0,
|
||||
);
|
||||
|
||||
export const folder = (name: string, children: Array<Child>): Folder => ({
|
||||
type: "folder",
|
||||
name,
|
||||
opened: false,
|
||||
children,
|
||||
});
|
||||
|
||||
export const link = (name: string, url: string, icon: Icon): Link => ({
|
||||
type: "link",
|
||||
name,
|
||||
url,
|
||||
icon,
|
||||
});
|
||||
|
||||
export const file = (name: string, content: string, icon: Icon): File => ({
|
||||
type: "file",
|
||||
name,
|
||||
content,
|
||||
icon,
|
||||
});
|
||||
|
||||
export const project = (
|
||||
name: string,
|
||||
content: string,
|
||||
url: string,
|
||||
language: string,
|
||||
priv: boolean,
|
||||
): Project => ({
|
||||
type: "project",
|
||||
name,
|
||||
content,
|
||||
url,
|
||||
icon: ICONS[language] ?? DEFAULT_ICON,
|
||||
private: priv,
|
||||
});
|
||||
|
||||
export const icon = (char: string, color: string): Icon => ({
|
||||
char,
|
||||
color,
|
||||
});
|
||||
@@ -6,39 +6,3 @@ export type Prettify<T> = NonNullable<{ [K in keyof T]: T[K] }>;
|
||||
export type InnerKittyProps<T extends (...args: any[]) => any> = Prettify<
|
||||
Parameters<T>[0] & KittyContextProps
|
||||
>;
|
||||
|
||||
export type RootManifest = {
|
||||
files: Array<string>;
|
||||
projects: Array<{
|
||||
name: string;
|
||||
icon: string;
|
||||
}>;
|
||||
links: Array<{
|
||||
name: string;
|
||||
url: string;
|
||||
icon: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
export type Icon = {
|
||||
char: string;
|
||||
color: string;
|
||||
};
|
||||
|
||||
export type File = {
|
||||
name: string;
|
||||
} & (
|
||||
| {
|
||||
type: "link";
|
||||
url: string;
|
||||
icon: string;
|
||||
}
|
||||
| { type: "file"; repo: string; fileName: string; icon?: string }
|
||||
);
|
||||
|
||||
export type Directory = {
|
||||
name: string;
|
||||
type: "directory";
|
||||
files: Array<File>;
|
||||
opened: boolean;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user