mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
|
import { useMemo } from 'react'
|
||
|
import { File, FileOrDirectory, filterFolders } from '../utils/file'
|
||
|
import { useFileTreeData } from '@/shared/context/file-tree-data-context'
|
||
|
import { Folder } from '../../../../../types/folder'
|
||
|
import { Doc } from '../../../../../types/doc'
|
||
|
import { FileRef } from '../../../../../types/file-ref'
|
||
|
|
||
|
function docAdapter(doc: Doc): FileOrDirectory {
|
||
|
return {
|
||
|
id: doc._id,
|
||
|
name: doc.name,
|
||
|
type: 'doc',
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function fileRefAdapter(fileRef: FileRef): FileOrDirectory {
|
||
|
return {
|
||
|
id: fileRef._id,
|
||
|
name: fileRef.name,
|
||
|
type: 'file',
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function folderAdapter(folder: Folder): FileOrDirectory {
|
||
|
return {
|
||
|
id: folder._id,
|
||
|
name: folder.name,
|
||
|
type: 'folder',
|
||
|
children: folder.docs
|
||
|
.map(docAdapter)
|
||
|
.concat(
|
||
|
folder.fileRefs.map(fileRefAdapter),
|
||
|
folder.folders.map(folderAdapter)
|
||
|
),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const useCurrentProjectFolders: () => {
|
||
|
folders: File[] | undefined
|
||
|
rootFile: File
|
||
|
rootFolder: FileOrDirectory
|
||
|
} = () => {
|
||
|
const { fileTreeData } = useFileTreeData()
|
||
|
|
||
|
return useMemo(() => {
|
||
|
const rootFolder = folderAdapter(fileTreeData)
|
||
|
const rootFile = { ...rootFolder, path: '' }
|
||
|
const folders = filterFolders(rootFolder)
|
||
|
return { folders, rootFile, rootFolder }
|
||
|
}, [fileTreeData])
|
||
|
}
|