Line data Source code
1 : import { useIntervalFn } from '@vueuse/core'
2 :
3 : import { useToast } from '@/components/ui/toast/toastStore'
4 : import { t } from '@/i18n'
5 : import { createToastId } from '@/types/toastId'
6 :
7 : import type { CpuServerStep } from './cpuServerStep'
8 :
9 2355 : const STEP_DETAIL = {
10 : creating: 'lateCustomNodes.stepCreating',
11 : building: 'lateCustomNodes.stepBuilding',
12 : starting: 'lateCustomNodes.stepStarting'
13 : } as const satisfies Record<CpuServerStep, string>
14 :
15 0 : function packNameOf(ext: string): string {
16 0 : const match = /^\/?extensions\/([^/]+)\//.exec(ext)
17 0 : return match ? match[1] : ext
18 : }
19 :
20 0 : function packsOf(scripts: Iterable<string>): string {
21 0 : return [...new Set([...scripts].map(packNameOf))].join(', ')
22 : }
23 :
24 : /** Shows the seconds since `startedAt`, the CPU server's step when known, and the packs still in `waiting`, kept current every second until dismissed. */
25 0 : export function showStartingToast(
26 : startedAt: number,
27 : waiting: ReadonlySet<string>,
28 : step: () => CpuServerStep | undefined
29 : ) {
30 0 : const toast = useToast()
31 0 : const id = createToastId()
32 0 : function text() {
33 0 : const packs = packsOf(waiting)
34 0 : const current = step()
35 0 : return {
36 : title: t('lateCustomNodes.starting', {
37 : seconds: Math.round((Date.now() - startedAt) / 1000)
38 : }),
39 : description: [
40 : t(current ? STEP_DETAIL[current] : 'lateCustomNodes.startingDetail'),
41 : packs ? t('lateCustomNodes.waitingFor', { packs }) : ''
42 : ]
43 : .filter(Boolean)
44 : .join('\n')
45 : }
46 : }
47 0 : const first = text()
48 0 : toast.loading(first.title, {
49 : id,
50 : description: first.description,
51 : closable: false
52 : })
53 0 : function refresh() {
54 0 : toast.update(id, text())
55 : }
56 0 : const ticker = useIntervalFn(refresh, 1000)
57 0 : return {
58 : refresh,
59 : dismiss: () => {
60 0 : ticker.pause()
61 0 : toast.dismiss(id)
62 : }
63 : }
64 : }
65 :
66 0 : export function showReadyToast() {
67 0 : useToast().info(t('lateCustomNodes.ready'), { duration: 5000 })
68 : }
69 :
70 : /** Names the packs that did not start, with a Reload action. */
71 0 : export function showFailedToast(scripts: string[]) {
72 0 : useToast().warning(t('lateCustomNodes.failed'), {
73 : description: t('lateCustomNodes.failedDetail', { packs: packsOf(scripts) }),
74 : action: {
75 : label: t('lateCustomNodes.reload'),
76 : onClick: () => window.location.reload()
77 : }
78 : })
79 : }
|