use-project-entities.js 935 B

12345678910111213141516171819202122232425262728293031
  1. import { useEffect, useState } from 'react'
  2. import { getJSON } from '../../../infrastructure/fetch-json'
  3. import { fileCollator } from '../util/file-collator'
  4. import useAbortController from '../../../shared/hooks/use-abort-controller'
  5. const alphabetical = (a, b) => fileCollator.compare(a.path, b.path)
  6. export function useProjectEntities(projectId) {
  7. const [loading, setLoading] = useState(false)
  8. const [data, setData] = useState(null)
  9. const [error, setError] = useState(false)
  10. const { signal } = useAbortController()
  11. useEffect(() => {
  12. if (projectId) {
  13. setLoading(true)
  14. setError(false)
  15. setData(null)
  16. getJSON(`/project/${projectId}/entities`, { signal })
  17. .then(data => {
  18. setData(data.entities.sort(alphabetical))
  19. })
  20. .catch(error => setError(error))
  21. .finally(() => setLoading(false))
  22. }
  23. }, [projectId, signal])
  24. return { loading, data, error }
  25. }