pdf-compile-time.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { memo, useEffect, useRef, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useDetachCompileContext } from '@/shared/context/detach-compile-context'
  4. import MaterialIcon from '@/shared/components/material-icon'
  5. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  6. function PdfCompileTime() {
  7. const { t } = useTranslation()
  8. const { compiling, deliveryLatencies } = useDetachCompileContext()
  9. const startRef = useRef<number | null>(null)
  10. const [elapsedMs, setElapsedMs] = useState<number | null>(null)
  11. useEffect(() => {
  12. if (!compiling) {
  13. startRef.current = null
  14. return
  15. }
  16. startRef.current = performance.now()
  17. setElapsedMs(0)
  18. const intervalId = window.setInterval(() => {
  19. if (startRef.current !== null) {
  20. setElapsedMs(performance.now() - startRef.current)
  21. }
  22. }, 100)
  23. return () => {
  24. window.clearInterval(intervalId)
  25. }
  26. }, [compiling])
  27. const ms = compiling
  28. ? (elapsedMs ?? 0)
  29. : deliveryLatencies?.compileTimeClientE2E
  30. if (ms == null) {
  31. return null
  32. }
  33. const seconds = (ms / 1000).toFixed(1)
  34. return (
  35. <OLTooltip
  36. id="pdf-compile-time"
  37. description={t('last_compile_duration', { seconds })}
  38. overlayProps={{ placement: 'bottom' }}
  39. >
  40. <span
  41. className="toolbar-pdf-compile-time"
  42. aria-label={t('last_compile_duration', { seconds })}
  43. >
  44. {compiling ? (
  45. <MaterialIcon type="hourglass_top" />
  46. ) : (
  47. <MaterialIcon type="timer" />
  48. )}
  49. <span>{seconds} s</span>
  50. </span>
  51. </OLTooltip>
  52. )
  53. }
  54. export default memo(PdfCompileTime)