use-word-count.js 721 B

1234567891011121314151617181920212223242526
  1. import useAbortController from '../../../shared/hooks/use-abort-controller'
  2. import { fetchWordCount } from '../utils/api'
  3. import { useEffect, useState } from 'react'
  4. export function useWordCount(projectId, clsiServerId) {
  5. const [loading, setLoading] = useState(true)
  6. const [error, setError] = useState(false)
  7. const [data, setData] = useState()
  8. const { signal } = useAbortController()
  9. useEffect(() => {
  10. fetchWordCount(projectId, clsiServerId, { signal })
  11. .then(data => {
  12. setData(data.texcount)
  13. })
  14. .catch(() => {
  15. setError(true)
  16. })
  17. .finally(() => {
  18. setLoading(false)
  19. })
  20. }, [signal, clsiServerId, projectId])
  21. return { data, error, loading }
  22. }