overleaf/services/web/frontend/js/features/source-editor/hooks/use-current-project-folders.ts
Tim Down c34c95b46d Merge pull request #15444 from overleaf/td-remove-root-folder-scope-use
Remove use of root folder scope in figure modal

GitOrigin-RevId: d07247b644d312ef711f5601d3c10a3274e43416
2023-10-27 08:03:13 +00:00

51 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])
}