use-user-projects.ts 911 B

1234567891011121314151617181920212223242526272829303132
  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. export type Project = {
  6. _id: string
  7. name: string
  8. accessLevel: string
  9. }
  10. const alphabetical = (a: Project, b: Project) =>
  11. fileCollator.compare(a.name, b.name)
  12. export function useUserProjects() {
  13. const [loading, setLoading] = useState(true)
  14. const [data, setData] = useState<Project[] | null>(null)
  15. const [error, setError] = useState<any>(false)
  16. const { signal } = useAbortController()
  17. useEffect(() => {
  18. getJSON('/user/projects', { signal })
  19. .then(data => {
  20. setData(data.projects.sort(alphabetical))
  21. })
  22. .catch(error => setError(error))
  23. .finally(() => setLoading(false))
  24. }, [signal])
  25. return { loading, data, error }
  26. }