use-user-projects.js 790 B

12345678910111213141516171819202122232425
  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.name, b.name)
  6. export function useUserProjects() {
  7. const [loading, setLoading] = useState(true)
  8. const [data, setData] = useState(null)
  9. const [error, setError] = useState(false)
  10. const { signal } = useAbortController()
  11. useEffect(() => {
  12. getJSON('/user/projects', { signal })
  13. .then(data => {
  14. setData(data.projects.sort(alphabetical))
  15. })
  16. .catch(error => setError(error))
  17. .finally(() => setLoading(false))
  18. }, [signal])
  19. return { loading, data, error }
  20. }