CoastalCommitsPastes/client/app/(posts)/components/file-tree/index.tsx

72 lines
1.6 KiB
TypeScript
Raw Normal View History

import { File } from "@lib/server/prisma"
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-11-12 04:28:06 -05:00
import ShiftBy from "@components/shift-by"
2022-03-24 23:24:40 -04:00
import { useEffect, useState } from "react"
import { codeFileExtensions } from "@lib/constants"
2022-11-12 04:28:06 -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
}
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}>
<Card className={styles.card}>
2022-04-09 20:48:19 -04:00
<div className={styles.cardContent}>
<h4>Files</h4>
2022-04-09 20:48:19 -04:00
<ul className={styles.fileTree}>
{items.map(({ id, title, icon }) => (
<li key={id}>
<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