refactor: rework assets fetching
This commit is contained in:
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,
|
||||
});
|
||||
Reference in New Issue
Block a user