useVideoStartTimestamp.ts 724 B

123456789101112131415161718192021
  1. import { useEffect, useState } from 'react'
  2. import { useRouterQuery } from '@/hooks/useRouterQuery'
  3. export const useVideoStartTimestamp = (duration?: number | null, savedVideoTimeStamp?: number) => {
  4. const [startTimestamp, setStartTimestamp] = useState<number>()
  5. const timestampFromQuery = Number(useRouterQuery('time'))
  6. useEffect(() => {
  7. if (!timestampFromQuery || timestampFromQuery > (duration ?? 0)) {
  8. return
  9. }
  10. if (savedVideoTimeStamp && !timestampFromQuery && savedVideoTimeStamp < (duration ?? 0)) {
  11. setStartTimestamp(savedVideoTimeStamp)
  12. return
  13. }
  14. setStartTimestamp(timestampFromQuery)
  15. }, [duration, savedVideoTimeStamp, timestampFromQuery])
  16. return startTimestamp
  17. }