fix: pair code modal — auto-regenerate on expiry
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -68,23 +68,31 @@ function PairCodeModal({
|
||||
const [copied, setCopied] = useState(false)
|
||||
const [timeLeft, setTimeLeft] = useState(600)
|
||||
|
||||
useEffect(() => {
|
||||
const fetchCode = useCallback(() => {
|
||||
setLoading(true)
|
||||
api.workstations.pairCode(slug, workstation.id).then(r => {
|
||||
setCode(r.code)
|
||||
setExpiresAt(r.expiresAt)
|
||||
setTimeLeft(600)
|
||||
setLoading(false)
|
||||
})
|
||||
}, [slug, workstation.id])
|
||||
|
||||
useEffect(() => { fetchCode() }, [fetchCode])
|
||||
|
||||
useEffect(() => {
|
||||
if (!expiresAt) return
|
||||
const tick = setInterval(() => {
|
||||
const left = Math.max(0, Math.floor((new Date(expiresAt).getTime() - Date.now()) / 1000))
|
||||
setTimeLeft(left)
|
||||
if (left === 0) clearInterval(tick)
|
||||
if (left === 0) {
|
||||
clearInterval(tick)
|
||||
// Автоматически получаем новый код
|
||||
fetchCode()
|
||||
}
|
||||
}, 1000)
|
||||
return () => clearInterval(tick)
|
||||
}, [expiresAt])
|
||||
}, [expiresAt, fetchCode])
|
||||
|
||||
const copy = () => {
|
||||
if (!code) return
|
||||
|
||||
Reference in New Issue
Block a user