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 [copied, setCopied] = useState(false)
|
||||||
const [timeLeft, setTimeLeft] = useState(600)
|
const [timeLeft, setTimeLeft] = useState(600)
|
||||||
|
|
||||||
useEffect(() => {
|
const fetchCode = useCallback(() => {
|
||||||
|
setLoading(true)
|
||||||
api.workstations.pairCode(slug, workstation.id).then(r => {
|
api.workstations.pairCode(slug, workstation.id).then(r => {
|
||||||
setCode(r.code)
|
setCode(r.code)
|
||||||
setExpiresAt(r.expiresAt)
|
setExpiresAt(r.expiresAt)
|
||||||
|
setTimeLeft(600)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
})
|
})
|
||||||
}, [slug, workstation.id])
|
}, [slug, workstation.id])
|
||||||
|
|
||||||
|
useEffect(() => { fetchCode() }, [fetchCode])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!expiresAt) return
|
if (!expiresAt) return
|
||||||
const tick = setInterval(() => {
|
const tick = setInterval(() => {
|
||||||
const left = Math.max(0, Math.floor((new Date(expiresAt).getTime() - Date.now()) / 1000))
|
const left = Math.max(0, Math.floor((new Date(expiresAt).getTime() - Date.now()) / 1000))
|
||||||
setTimeLeft(left)
|
setTimeLeft(left)
|
||||||
if (left === 0) clearInterval(tick)
|
if (left === 0) {
|
||||||
|
clearInterval(tick)
|
||||||
|
// Автоматически получаем новый код
|
||||||
|
fetchCode()
|
||||||
|
}
|
||||||
}, 1000)
|
}, 1000)
|
||||||
return () => clearInterval(tick)
|
return () => clearInterval(tick)
|
||||||
}, [expiresAt])
|
}, [expiresAt, fetchCode])
|
||||||
|
|
||||||
const copy = () => {
|
const copy = () => {
|
||||||
if (!code) return
|
if (!code) return
|
||||||
|
|||||||
Reference in New Issue
Block a user