mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-01 15:31:30 -05:00
31190b967b
GitOrigin-RevId: 3043d1369ed85b38b1fec7479385b123a304c05b
36 lines
1 KiB
TypeScript
36 lines
1 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
import { fileCollator } from '../util/file-collator'
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
|
|
|
export type Entity = {
|
|
path: string
|
|
}
|
|
|
|
const alphabetical = (a: Entity, b: Entity) =>
|
|
fileCollator.compare(a.path, b.path)
|
|
|
|
export function useProjectEntities(projectId?: string) {
|
|
const [loading, setLoading] = useState(false)
|
|
const [data, setData] = useState<Entity[] | null>(null)
|
|
const [error, setError] = useState<any>(false)
|
|
|
|
const { signal } = useAbortController()
|
|
|
|
useEffect(() => {
|
|
if (projectId) {
|
|
setLoading(true)
|
|
setError(false)
|
|
setData(null)
|
|
|
|
getJSON(`/project/${projectId}/entities`, { signal })
|
|
.then(data => {
|
|
setData(data.entities.sort(alphabetical))
|
|
})
|
|
.catch(error => setError(error))
|
|
.finally(() => setLoading(false))
|
|
}
|
|
}, [projectId, signal])
|
|
|
|
return { loading, data, error }
|
|
}
|