2021-03-18 05:52:36 -04:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { fileCollator } from '../util/file-collator'
|
2021-06-23 04:09:23 -04:00
|
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2023-05-15 05:17:13 -04:00
|
|
|
export type Entity = {
|
|
|
|
path: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const alphabetical = (a: Entity, b: Entity) =>
|
|
|
|
fileCollator.compare(a.path, b.path)
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2023-05-15 05:17:13 -04:00
|
|
|
export function useProjectEntities(projectId?: string) {
|
2021-03-18 05:52:36 -04:00
|
|
|
const [loading, setLoading] = useState(false)
|
2023-05-15 05:17:13 -04:00
|
|
|
const [data, setData] = useState<Entity[] | null>(null)
|
|
|
|
const [error, setError] = useState<any>(false)
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (projectId) {
|
|
|
|
setLoading(true)
|
|
|
|
setError(false)
|
|
|
|
setData(null)
|
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
getJSON(`/project/${projectId}/entities`, { signal })
|
2021-03-18 05:52:36 -04:00
|
|
|
.then(data => {
|
|
|
|
setData(data.entities.sort(alphabetical))
|
|
|
|
})
|
|
|
|
.catch(error => setError(error))
|
|
|
|
.finally(() => setLoading(false))
|
|
|
|
}
|
2021-06-23 04:09:23 -04:00
|
|
|
}, [projectId, signal])
|
2021-03-18 05:52:36 -04:00
|
|
|
|
|
|
|
return { loading, data, error }
|
|
|
|
}
|