2022-03-24 23:24:40 -04:00
|
|
|
import { File } from "@lib/types"
|
2022-04-09 20:48:19 -04:00
|
|
|
import FileIcon from "@geist-ui/icons/fileText"
|
|
|
|
import CodeIcon from "@geist-ui/icons/fileLambda"
|
|
|
|
import styles from "./file-tree.module.css"
|
2022-03-24 23:24:40 -04:00
|
|
|
import ShiftBy from "@components/shift-by"
|
|
|
|
import { useEffect, useState } from "react"
|
|
|
|
import { codeFileExtensions } from "@lib/constants"
|
2022-11-08 03:23:28 -05:00
|
|
|
import Link from "@components/link"
|
2022-03-24 23:24:40 -04:00
|
|
|
|
|
|
|
type Item = File & {
|
2022-04-09 20:48:19 -04:00
|
|
|
icon: JSX.Element
|
2022-03-24 23:24:40 -04:00
|
|
|
}
|
|
|
|
|
2022-11-08 03:23:28 -05:00
|
|
|
const Card = ({
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
...props
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
className?: string
|
|
|
|
} & React.ComponentProps<"div">) => (
|
|
|
|
<div className={styles.card} {...props}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const FileTree = ({ files }: { files: File[] }) => {
|
|
|
|
const [items, setItems] = useState<Item[]>([])
|
|
|
|
useEffect(() => {
|
|
|
|
const newItems = files.map((file) => {
|
|
|
|
const extension = file.title.split(".").pop()
|
|
|
|
if (codeFileExtensions.includes(extension || "")) {
|
|
|
|
return {
|
|
|
|
...file,
|
|
|
|
icon: <CodeIcon />
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
...file,
|
|
|
|
icon: <FileIcon />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setItems(newItems)
|
|
|
|
}, [files])
|
2022-03-24 23:24:40 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
// a list of files with an icon and a title
|
|
|
|
return (
|
|
|
|
<div className={styles.fileTreeWrapper}>
|
2022-11-08 03:23:28 -05:00
|
|
|
<Card className={styles.card}>
|
2022-04-09 20:48:19 -04:00
|
|
|
<div className={styles.cardContent}>
|
2022-11-08 03:23:28 -05:00
|
|
|
<h4>Files</h4>
|
2022-04-09 20:48:19 -04:00
|
|
|
<ul className={styles.fileTree}>
|
|
|
|
{items.map(({ id, title, icon }) => (
|
|
|
|
<li key={id}>
|
2022-11-08 03:23:28 -05:00
|
|
|
<Link href={`#${title}`}>
|
2022-04-09 20:48:19 -04:00
|
|
|
<ShiftBy y={5}>
|
|
|
|
<span className={styles.fileTreeIcon}>{icon}</span>
|
|
|
|
</ShiftBy>
|
|
|
|
<span className={styles.fileTreeTitle}>{title}</span>
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-24 23:24:40 -04:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export default FileTree
|