-
Notifications
You must be signed in to change notification settings - Fork 585
/
Copy pathuse-demo-progress.ts
75 lines (60 loc) · 1.8 KB
/
use-demo-progress.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {useEffect, useState} from 'react'
import {useNavigate} from 'react-router-dom'
import {AppState} from '@/trpc/trpc'
function incrementProgress(progress: number) {
return Math.min(progress + Math.round(Math.random() * 30), 100)
}
type UsableState = Extract<AppState, 'not-installed' | 'installing' | 'ready'>
export function useDemoInstallProgress() {
const [state, setState] = useState<UsableState>('not-installed')
const [progress, setProgress] = useState(0)
useEffect(() => {
if (state === 'installing') {
const interval = setInterval(() => setProgress(incrementProgress), Math.round(Math.random() * 500))
if (progress == 100) {
// Wait after install so you can see the 100%
setTimeout(() => {
setState('ready')
setProgress(100)
clearInterval(interval)
}, 500)
}
return () => clearInterval(interval)
}
}, [state, progress])
return {
state,
progress,
install() {
setState('installing')
},
}
}
export function useDemoMigrateProgress({onSuccess, onFail}: {onSuccess: () => void; onFail: () => void}) {
const navigate = useNavigate()
const [progress, setProgress] = useState(0)
useEffect(() => {
const interval = setInterval(() => setProgress(incrementProgress), Math.round(Math.random() * 500))
if (progress == 100) {
// Wait after install so you can see the 100%
setTimeout(() => {
setProgress(100)
const didFail = Math.random() > 0.5
if (didFail) {
onFail()
// navigate('/migrate/failed')
} else {
onSuccess()
// navigate('/migrate/success')
}
clearInterval(interval)
}, 500)
}
return () => clearInterval(interval)
// Don't mind onSuccess and onFail, they're just for demo purposes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [progress, navigate])
return {
progress,
}
}