|
import { useStore } from "@/app/store" |
|
import { useEffect, useRef } from "react" |
|
import { useIsBusy } from "./useIsBusy" |
|
|
|
export function useProgressTimer() { |
|
const runningRef = useRef(false) |
|
const timerRef = useRef<NodeJS.Timeout>() |
|
|
|
const progress = useStore(s => s.progress) |
|
const stage = useStore(s => s.stage) |
|
const { isBusy, busyRef } = useIsBusy() |
|
|
|
const timerFn = async () => { |
|
const { progress, stage } = useStore.getState() |
|
|
|
let isBusy = busyRef.current |
|
|
|
clearTimeout(timerRef.current) |
|
if (!isBusy || stage === "idle") { |
|
return |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useStore.setState({ |
|
|
|
progress: Math.min(100, progress + 1) |
|
}) |
|
|
|
|
|
timerRef.current = setTimeout(timerFn, 1600) |
|
} |
|
|
|
|
|
|
|
useEffect(() => { |
|
timerFn() |
|
clearTimeout(timerRef.current) |
|
let isBusy = busyRef.current |
|
if (!isBusy) { |
|
return |
|
} |
|
|
|
|
|
|
|
|
|
timerRef.current = setTimeout(timerFn, 0) |
|
}, [isBusy]) |
|
|
|
return { isBusy, busyRef, progress, stage } |
|
} |