use-project-entities.js 796 B

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