resources: set up home page

This commit is contained in:
Jean-Philippe Sirois
2019-09-17 01:58:15 -04:00
parent 2d16e9489c
commit 5bd0787112
5 changed files with 308 additions and 2 deletions
@@ -0,0 +1,76 @@
import chain from "ramda/es/chain"
import partition from "ramda/es/partition"
import { traversePathsToFiles } from "../../utils"
import {
IAllResourcesQuery,
IFile,
IFileOrFolder,
IFileQuery,
IFolder,
} from "./index"
function generateFolder({
title,
path,
targets,
}: {
title: IFolder["title"]
path: IFile["path"]
targets: IFolder[]
}): IFolder {
const children = join(chain(target => target.children, targets))
return {
title,
type: "folder",
path,
children: children.sort((a, b) =>
a.title.split("/").length > b.title.split("/").length
? -1
: a.title.localeCompare(b.title)
),
}
}
function generateFile({
title,
path,
}: {
title: IFile["title"]
path: IFile["path"]
}): IFile {
return {
title,
type: "file",
path,
}
}
function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] {
if (!head) {
return []
}
const [similarFs, remaining] = partition(
obj => obj.title === head.title && obj.type === head.type,
tail
)
const targets = [head, ...similarFs]
const { title, path } = head
const current =
head.type === "folder"
? generateFolder({ title, path, targets: targets as IFolder[] })
: generateFile({ title, path })
return [current, ...join(remaining)]
}
export default function useBuildTree(resources: IAllResourcesQuery) {
const objects = resources.allFile.edges.map(({ node: file }: IFileQuery) =>
traversePathsToFiles(file.relativePath.split("/"))
)
return join(objects)
}