dsh-plugin-container-windows 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +109 -2
- package/README.zh-CN.md +110 -0
- package/cordis.patch.yml +3 -0
- package/docs/ai-chat.png +0 -0
- package/docs/ai-new-session.png +0 -0
- package/docs/apple-container.png +0 -0
- package/docs/attach.png +0 -0
- package/docs/docker-console.png +0 -0
- package/docs/docker-overview.png +0 -0
- package/docs/docker-volumes.png +0 -0
- package/docs/image-architecture.png +0 -0
- package/docs/package-install.png +0 -0
- package/docs/provider-switch.png +0 -0
- package/docs/run-container.png +0 -0
- package/index.js +141 -0
- package/lib/client.js +4092 -0
- package/package.json +72 -4
- package/src/client/api.js +98 -0
- package/src/client/attach.jsx +134 -0
- package/src/client/build-image.jsx +224 -0
- package/src/client/components.jsx +325 -0
- package/src/client/hooks.js +161 -0
- package/src/client/i18n.js +602 -0
- package/src/client/icons.jsx +239 -0
- package/src/client/index.jsx +90 -0
- package/src/client/page.jsx +854 -0
- package/src/client/platforms.js +2 -0
- package/src/client/push-image.jsx +113 -0
- package/src/client/run-container.jsx +187 -0
- package/src/client/runs.js +182 -0
- package/src/client/store.js +91 -0
- package/src/client/styles.js +383 -0
- package/src/client/tabs/compose.jsx +94 -0
- package/src/client/tabs/console.jsx +211 -0
- package/src/client/tabs/containers.jsx +148 -0
- package/src/client/tabs/images.jsx +154 -0
- package/src/client/tabs/networks.jsx +91 -0
- package/src/client/tabs/overview.jsx +266 -0
- package/src/client/tabs/volumes.jsx +76 -0
- package/src/host/apple-cli.js +319 -0
- package/src/host/apple.js +495 -0
- package/src/host/attach.js +246 -0
- package/src/host/commands.js +241 -0
- package/src/host/docker-cli.js +468 -0
- package/src/host/images.js +143 -0
- package/src/host/jobs.js +245 -0
- package/src/host/parsers.js +484 -0
- package/src/host/runtime.js +379 -0
- package/src/host/service.js +892 -0
- package/src/host/tools.js +198 -0
- package/src/host/util.js +97 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/** Small presentational building blocks shared by the Docker tabs. */
|
|
2
|
+
import { useEffect, useRef, useState } from 'react'
|
|
3
|
+
import { createPortal } from 'react-dom'
|
|
4
|
+
|
|
5
|
+
import { IconAlert, IconCheck, IconCopy, IconSearch, IconSpinner, IconX } from './icons.jsx'
|
|
6
|
+
|
|
7
|
+
export function cx(...values) {
|
|
8
|
+
return values.filter(Boolean).join(' ')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Button({ variant = 'ghost', size = 'md', className, type = 'button', children, ...rest }) {
|
|
12
|
+
return (
|
|
13
|
+
<button
|
|
14
|
+
type={type}
|
|
15
|
+
className={cx(
|
|
16
|
+
'dsh-docker-btn',
|
|
17
|
+
variant === 'primary' && 'dsh-docker-primary',
|
|
18
|
+
variant === 'danger' && 'dsh-docker-danger',
|
|
19
|
+
variant === 'danger-solid' && 'dsh-docker-danger-solid',
|
|
20
|
+
variant === 'ghost' && 'dsh-docker-ghost',
|
|
21
|
+
size === 'sm' && 'dsh-docker-sm',
|
|
22
|
+
size === 'lg' && 'dsh-docker-lg',
|
|
23
|
+
className,
|
|
24
|
+
)}
|
|
25
|
+
{...rest}
|
|
26
|
+
>
|
|
27
|
+
{children}
|
|
28
|
+
</button>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function IconButton({ label, size = 'md', className, children, ...rest }) {
|
|
33
|
+
const [tip, setTip] = useState(null)
|
|
34
|
+
const ref = useRef(null)
|
|
35
|
+
const showTip = () => {
|
|
36
|
+
const rect = ref.current?.getBoundingClientRect()
|
|
37
|
+
if (rect === undefined) return
|
|
38
|
+
const left = Math.min(Math.max(rect.left + rect.width / 2, 16), window.innerWidth - 16)
|
|
39
|
+
setTip({ left, top: Math.max(rect.top - 7, 8) })
|
|
40
|
+
}
|
|
41
|
+
const hideTip = () => setTip(null)
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<button
|
|
45
|
+
type="button"
|
|
46
|
+
ref={ref}
|
|
47
|
+
aria-label={label}
|
|
48
|
+
className={cx('dsh-docker-icon-btn', size === 'sm' && 'dsh-docker-sm', className)}
|
|
49
|
+
onMouseEnter={showTip}
|
|
50
|
+
onMouseLeave={hideTip}
|
|
51
|
+
onFocus={showTip}
|
|
52
|
+
onBlur={hideTip}
|
|
53
|
+
{...rest}
|
|
54
|
+
>
|
|
55
|
+
{children}
|
|
56
|
+
</button>
|
|
57
|
+
{tip !== null && createPortal(
|
|
58
|
+
<span className="dsh-docker-tooltip" style={{ left: tip.left, top: tip.top }} role="tooltip">{label}</span>,
|
|
59
|
+
document.body,
|
|
60
|
+
)}
|
|
61
|
+
</>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function Spinner({ size = 16, className }) {
|
|
66
|
+
return <IconSpinner size={size} className={className} />
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function StatusDot({ state = 'unknown' }) {
|
|
70
|
+
return <span className="dsh-docker-dot" data-state={state} aria-hidden="true" />
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function StateLabel({ state, t }) {
|
|
74
|
+
const normalized = state === 'running' ? 'running'
|
|
75
|
+
: state === 'restarting' ? 'restarting'
|
|
76
|
+
: state === 'paused' ? 'paused'
|
|
77
|
+
: state === 'dead' ? 'dead'
|
|
78
|
+
: 'exited'
|
|
79
|
+
const label = normalized === 'running' ? t('running')
|
|
80
|
+
: normalized === 'restarting' ? t('statusStarting')
|
|
81
|
+
: normalized === 'paused' ? t('statusStarting')
|
|
82
|
+
: normalized === 'dead' ? t('failed')
|
|
83
|
+
: t('stopped')
|
|
84
|
+
return (
|
|
85
|
+
<span className="dsh-docker-state" data-state={normalized}>
|
|
86
|
+
<StatusDot state={normalized} />
|
|
87
|
+
{label}
|
|
88
|
+
</span>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function Modal({ open, title, subtitle, wide, onClose, children, footer, closeLabel = 'Close' }) {
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (!open) return undefined
|
|
95
|
+
const onKey = (event) => {
|
|
96
|
+
if (event.key === 'Escape') {
|
|
97
|
+
event.stopPropagation()
|
|
98
|
+
onClose?.()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
document.addEventListener('keydown', onKey)
|
|
102
|
+
return () => document.removeEventListener('keydown', onKey)
|
|
103
|
+
}, [open, onClose])
|
|
104
|
+
|
|
105
|
+
if (!open) return null
|
|
106
|
+
return createPortal(
|
|
107
|
+
<div
|
|
108
|
+
className="dsh-docker-modal-backdrop"
|
|
109
|
+
onMouseDown={(event) => {
|
|
110
|
+
if (event.target === event.currentTarget) onClose?.()
|
|
111
|
+
}}
|
|
112
|
+
>
|
|
113
|
+
<div
|
|
114
|
+
className={cx('dsh-docker-modal', wide && 'dsh-docker-modal-wide')}
|
|
115
|
+
role="dialog"
|
|
116
|
+
aria-modal="true"
|
|
117
|
+
aria-label={title}
|
|
118
|
+
>
|
|
119
|
+
<div className="dsh-docker-modal-head">
|
|
120
|
+
<div>
|
|
121
|
+
<h2 className="dsh-docker-modal-title">{title}</h2>
|
|
122
|
+
{subtitle !== undefined && <p className="dsh-docker-modal-sub">{subtitle}</p>}
|
|
123
|
+
</div>
|
|
124
|
+
<IconButton label={closeLabel} autoFocus onClick={onClose}>
|
|
125
|
+
<IconX size={16} />
|
|
126
|
+
</IconButton>
|
|
127
|
+
</div>
|
|
128
|
+
<div className="dsh-docker-modal-body">{children}</div>
|
|
129
|
+
{footer !== undefined && <div className="dsh-docker-modal-foot">{footer}</div>}
|
|
130
|
+
</div>
|
|
131
|
+
</div>,
|
|
132
|
+
document.body,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function ConfirmDialog({ open, title, body, command, confirmLabel, cancelLabel, closeLabel = 'Close', danger = false, busy = false, onConfirm, onCancel }) {
|
|
137
|
+
return (
|
|
138
|
+
<Modal
|
|
139
|
+
open={open}
|
|
140
|
+
title={title}
|
|
141
|
+
closeLabel={closeLabel}
|
|
142
|
+
onClose={onCancel}
|
|
143
|
+
footer={
|
|
144
|
+
<>
|
|
145
|
+
<Button onClick={onCancel} disabled={busy}>{cancelLabel}</Button>
|
|
146
|
+
<Button variant={danger ? 'danger-solid' : 'primary'} onClick={onConfirm} disabled={busy}>
|
|
147
|
+
{busy ? <Spinner size={14} /> : null}
|
|
148
|
+
{confirmLabel}
|
|
149
|
+
</Button>
|
|
150
|
+
</>
|
|
151
|
+
}
|
|
152
|
+
>
|
|
153
|
+
<div className={cx(danger && 'dsh-docker-modal-danger')} style={{ whiteSpace: 'pre-wrap' }}>
|
|
154
|
+
<div>{body}</div>
|
|
155
|
+
{command !== undefined && (
|
|
156
|
+
<pre className="dsh-docker-json" style={{ marginTop: 10, whiteSpace: 'pre-wrap' }}>{command}</pre>
|
|
157
|
+
)}
|
|
158
|
+
</div>
|
|
159
|
+
</Modal>
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function ToastStack({ toasts, onDismiss }) {
|
|
164
|
+
if (toasts.length === 0) return null
|
|
165
|
+
return createPortal(
|
|
166
|
+
<div className="dsh-docker-toast-wrap" aria-live="polite">
|
|
167
|
+
{toasts.map((toast) => (
|
|
168
|
+
<div key={toast.id} className="dsh-docker-toast" data-tone={toast.tone}>
|
|
169
|
+
<span style={{ marginTop: 1 }}>
|
|
170
|
+
{toast.tone === 'success' ? <IconCheck size={14} /> : toast.tone === 'error' ? <IconAlert size={14} /> : null}
|
|
171
|
+
</span>
|
|
172
|
+
<span className="dsh-docker-toast-main">{toast.message}</span>
|
|
173
|
+
<IconButton label="Close" size="sm" onClick={() => onDismiss(toast.id)}>
|
|
174
|
+
<IconX size={13} />
|
|
175
|
+
</IconButton>
|
|
176
|
+
</div>
|
|
177
|
+
))}
|
|
178
|
+
</div>,
|
|
179
|
+
document.body,
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function EmptyState({ icon, title, text, action }) {
|
|
184
|
+
return (
|
|
185
|
+
<div className="dsh-docker-empty">
|
|
186
|
+
{icon !== undefined && <span className="dsh-docker-empty-icon">{icon}</span>}
|
|
187
|
+
<div className="dsh-docker-empty-title">{title}</div>
|
|
188
|
+
{text !== undefined && <div className="dsh-docker-empty-text">{text}</div>}
|
|
189
|
+
{action}
|
|
190
|
+
</div>
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function ErrorState({ title, text, retryLabel, onRetry }) {
|
|
195
|
+
return (
|
|
196
|
+
<div className="dsh-docker-error">
|
|
197
|
+
<span className="dsh-docker-empty-icon"><IconAlert size={21} /></span>
|
|
198
|
+
<div className="dsh-docker-error-title">{title}</div>
|
|
199
|
+
{text !== undefined && <div className="dsh-docker-error-text">{text}</div>}
|
|
200
|
+
{onRetry !== undefined && <Button onClick={onRetry}>{retryLabel}</Button>}
|
|
201
|
+
</div>
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function LoadingSkeleton({ rows = 3 }) {
|
|
206
|
+
return (
|
|
207
|
+
<div className="dsh-docker-skeleton" aria-label="Loading">
|
|
208
|
+
{Array.from({ length: rows }, (_, index) => <div key={index} className="dsh-docker-skel-line" />)}
|
|
209
|
+
</div>
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function SearchInput({ value, onChange, placeholder, label }) {
|
|
214
|
+
return (
|
|
215
|
+
<label className="dsh-docker-search">
|
|
216
|
+
<span className="dsh-docker-search-icon"><IconSearch size={14} /></span>
|
|
217
|
+
<input
|
|
218
|
+
className="dsh-docker-input"
|
|
219
|
+
value={value}
|
|
220
|
+
onChange={(event) => onChange(event.target.value)}
|
|
221
|
+
placeholder={placeholder}
|
|
222
|
+
aria-label={label ?? placeholder}
|
|
223
|
+
spellCheck="false"
|
|
224
|
+
/>
|
|
225
|
+
</label>
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function CopyButton({ text, label, copiedLabel, className, children }) {
|
|
230
|
+
const [copied, setCopied] = useState(false)
|
|
231
|
+
const write = async () => {
|
|
232
|
+
try {
|
|
233
|
+
if (navigator.clipboard?.writeText !== undefined) await navigator.clipboard.writeText(text)
|
|
234
|
+
else {
|
|
235
|
+
const area = document.createElement('textarea')
|
|
236
|
+
area.value = text
|
|
237
|
+
area.style.position = 'fixed'
|
|
238
|
+
area.style.opacity = '0'
|
|
239
|
+
document.body.appendChild(area)
|
|
240
|
+
area.select()
|
|
241
|
+
document.execCommand('copy')
|
|
242
|
+
area.remove()
|
|
243
|
+
}
|
|
244
|
+
setCopied(true)
|
|
245
|
+
setTimeout(() => setCopied(false), 1500)
|
|
246
|
+
} catch {
|
|
247
|
+
setCopied(false)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return (
|
|
251
|
+
<Button onClick={write} className={className} title={label}>
|
|
252
|
+
{copied ? <IconCheck size={14} /> : <IconCopy size={14} />}
|
|
253
|
+
{children ?? (copied ? copiedLabel : label)}
|
|
254
|
+
</Button>
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function Chips({ values, className, mono = false }) {
|
|
259
|
+
if (values.length === 0) return <span className="dsh-docker-muted">—</span>
|
|
260
|
+
return (
|
|
261
|
+
<span className={cx('dsh-docker-chips', className)}>
|
|
262
|
+
{values.map((value, index) => (
|
|
263
|
+
<span key={`${value}-${index}`} className={cx('dsh-docker-chip', mono && 'dsh-docker-chip-code')} title={value}>
|
|
264
|
+
{value}
|
|
265
|
+
</span>
|
|
266
|
+
))}
|
|
267
|
+
</span>
|
|
268
|
+
)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** @param {number} bytes */
|
|
272
|
+
export function formatBytes(bytes) {
|
|
273
|
+
if (!Number.isFinite(bytes) || bytes < 0) return '—'
|
|
274
|
+
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB']
|
|
275
|
+
let value = bytes
|
|
276
|
+
let unit = 0
|
|
277
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
278
|
+
value /= 1024
|
|
279
|
+
unit += 1
|
|
280
|
+
}
|
|
281
|
+
return `${unit === 0 ? Math.round(value) : value.toFixed(value >= 10 ? 1 : 2)} ${units[unit]}`
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** @param {number} ms */
|
|
285
|
+
export function formatDuration(ms) {
|
|
286
|
+
if (!Number.isFinite(ms) || ms < 0) return '—'
|
|
287
|
+
const seconds = Math.floor(ms / 1000)
|
|
288
|
+
if (seconds < 60) return `${seconds}s`
|
|
289
|
+
const minutes = Math.floor(seconds / 60)
|
|
290
|
+
if (minutes < 60) return `${minutes}m ${seconds % 60}s`
|
|
291
|
+
const hours = Math.floor(minutes / 60)
|
|
292
|
+
return `${hours}h ${minutes % 60}m`
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** @param {number | undefined} timestamp */
|
|
296
|
+
export function formatTime(timestamp) {
|
|
297
|
+
if (typeof timestamp !== 'number') return '—'
|
|
298
|
+
const date = new Date(timestamp)
|
|
299
|
+
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** @param {string} value */
|
|
303
|
+
export function shortId(value) {
|
|
304
|
+
if (typeof value !== 'string') return ''
|
|
305
|
+
return value.replace(/^sha256:/u, '').slice(0, 12)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** @param {unknown} value */
|
|
309
|
+
export function displayValue(value) {
|
|
310
|
+
if (value === undefined || value === null || value === '') return '—'
|
|
311
|
+
if (typeof value === 'boolean') return value ? 'true' : 'false'
|
|
312
|
+
return String(value)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** @param {unknown} error */
|
|
316
|
+
/** Replace `{key}` placeholders in a localized string. */
|
|
317
|
+
export function fill(text, values = {}) {
|
|
318
|
+
return String(text).replace(/\{(\w+)\}/gu, (match, key) => (values[key] === undefined ? match : String(values[key])))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function errorMessage(error) {
|
|
322
|
+
if (error === undefined || error === null) return ''
|
|
323
|
+
if (error instanceof Error) return error.message
|
|
324
|
+
return String(error)
|
|
325
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/** Data hooks for the Docker panel. */
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
import { cacheClear, cacheGet, cacheSet, isAbortError } from './api.js'
|
|
5
|
+
|
|
6
|
+
const LIST_TTL = 2000
|
|
7
|
+
const STATUS_INTERVAL = 10_000
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Load one Docker list resource. Manual refreshes bypass the short cache so a
|
|
11
|
+
* click always reflects a fresh command result.
|
|
12
|
+
*/
|
|
13
|
+
export function useDockerList(api, provider, resource, context, { all = true, refreshToken = 0, enabled = true } = {}) {
|
|
14
|
+
const key = `list:${provider}:${resource}:${context ?? ''}:${all ? 'all' : 'running'}`
|
|
15
|
+
const [state, setState] = useState(() => {
|
|
16
|
+
const cached = cacheGet(key)
|
|
17
|
+
return { data: cached ?? null, error: null, loading: cached === undefined }
|
|
18
|
+
})
|
|
19
|
+
const [localVersion, setLocalVersion] = useState(0)
|
|
20
|
+
const [reloadSignal, setReloadSignal] = useState(0)
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
setState({ data: null, error: null, loading: enabled })
|
|
24
|
+
}, [enabled, key])
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!enabled) {
|
|
28
|
+
setState((current) => (current.data === null && !current.loading && current.error === null ? current : { data: null, error: null, loading: false }))
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
31
|
+
const cached = refreshToken === 0 && reloadSignal === 0 ? cacheGet(key) : undefined
|
|
32
|
+
if (cached !== undefined) {
|
|
33
|
+
setState({ data: cached, error: null, loading: false })
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
const controller = new AbortController()
|
|
37
|
+
let active = true
|
|
38
|
+
setState((current) => ({ data: current.data, error: null, loading: current.data === null }))
|
|
39
|
+
api.call('list', { provider, resource, context: context ?? undefined, all }, controller.signal).then(
|
|
40
|
+
(value) => {
|
|
41
|
+
if (!active) return
|
|
42
|
+
cacheSet(key, value, LIST_TTL)
|
|
43
|
+
setState({ data: value, error: null, loading: false })
|
|
44
|
+
},
|
|
45
|
+
(error) => {
|
|
46
|
+
if (!active || isAbortError(error)) return
|
|
47
|
+
setState((current) => ({ data: current.data, error, loading: false }))
|
|
48
|
+
},
|
|
49
|
+
)
|
|
50
|
+
return () => {
|
|
51
|
+
active = false
|
|
52
|
+
controller.abort()
|
|
53
|
+
}
|
|
54
|
+
}, [api, provider, key, resource, context, all, enabled, refreshToken, reloadSignal, localVersion])
|
|
55
|
+
|
|
56
|
+
const reload = useCallback(() => {
|
|
57
|
+
cacheClear(key)
|
|
58
|
+
setReloadSignal((value) => value + 1)
|
|
59
|
+
}, [key])
|
|
60
|
+
|
|
61
|
+
return { ...state, reload }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Poll Docker status while the panel is visible; keep the last good snapshot
|
|
66
|
+
* when a poll fails so the panel does not flicker during a daemon restart.
|
|
67
|
+
*/
|
|
68
|
+
export function useDockerStatus(api, provider, context, refreshToken = 0) {
|
|
69
|
+
const [state, setState] = useState({ data: null, error: null, loading: true, updatedAt: null })
|
|
70
|
+
const [localVersion, setLocalVersion] = useState(0)
|
|
71
|
+
const mounted = useRef(true)
|
|
72
|
+
|
|
73
|
+
const load = useCallback(
|
|
74
|
+
async (signal) => {
|
|
75
|
+
try {
|
|
76
|
+
const value = await api.call('status', { provider, context: context ?? undefined }, signal)
|
|
77
|
+
if (!mounted.current || signal?.aborted === true) return
|
|
78
|
+
setState({ data: value, error: null, loading: false, updatedAt: Date.now() })
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (!mounted.current || isAbortError(error)) return
|
|
81
|
+
setState((current) => ({ ...current, error, loading: false }))
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
[api, provider, context],
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
mounted.current = true
|
|
89
|
+
return () => {
|
|
90
|
+
mounted.current = false
|
|
91
|
+
}
|
|
92
|
+
}, [])
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
setState({ data: null, error: null, loading: true, updatedAt: null })
|
|
96
|
+
}, [api, provider, context])
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
let controller = new AbortController()
|
|
100
|
+
let timer
|
|
101
|
+
const tick = () => {
|
|
102
|
+
if (document.visibilityState !== 'visible') {
|
|
103
|
+
timer = setTimeout(tick, STATUS_INTERVAL)
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
controller.abort()
|
|
107
|
+
controller = new AbortController()
|
|
108
|
+
load(controller.signal)
|
|
109
|
+
timer = setTimeout(tick, STATUS_INTERVAL)
|
|
110
|
+
}
|
|
111
|
+
setState((current) => ({ ...current, loading: current.data === null }))
|
|
112
|
+
tick()
|
|
113
|
+
const onVisibility = () => {
|
|
114
|
+
if (document.visibilityState === 'visible') {
|
|
115
|
+
clearTimeout(timer)
|
|
116
|
+
tick()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
document.addEventListener('visibilitychange', onVisibility)
|
|
120
|
+
return () => {
|
|
121
|
+
controller.abort()
|
|
122
|
+
clearTimeout(timer)
|
|
123
|
+
document.removeEventListener('visibilitychange', onVisibility)
|
|
124
|
+
}
|
|
125
|
+
}, [load, refreshToken, localVersion])
|
|
126
|
+
|
|
127
|
+
const refresh = useCallback(() => {
|
|
128
|
+
cacheClear('list:')
|
|
129
|
+
setLocalVersion((value) => value + 1)
|
|
130
|
+
}, [])
|
|
131
|
+
|
|
132
|
+
return { ...state, refresh }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
/** Load the available providers and their CLI/system availability. */
|
|
137
|
+
export function useProviders(api, refreshToken = 0) {
|
|
138
|
+
const [state, setState] = useState({ data: null, error: null, loading: true })
|
|
139
|
+
const [localVersion, setLocalVersion] = useState(0)
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
const controller = new AbortController()
|
|
142
|
+
let active = true
|
|
143
|
+
setState((current) => ({ ...current, loading: current.data === null, error: null }))
|
|
144
|
+
api.call('providers', {}, controller.signal).then(
|
|
145
|
+
(value) => {
|
|
146
|
+
if (!active) return
|
|
147
|
+
setState({ data: value, error: null, loading: false })
|
|
148
|
+
},
|
|
149
|
+
(error) => {
|
|
150
|
+
if (!active || isAbortError(error)) return
|
|
151
|
+
setState({ data: null, error, loading: false })
|
|
152
|
+
},
|
|
153
|
+
)
|
|
154
|
+
return () => {
|
|
155
|
+
active = false
|
|
156
|
+
controller.abort()
|
|
157
|
+
}
|
|
158
|
+
}, [api, refreshToken, localVersion])
|
|
159
|
+
const reload = useCallback(() => setLocalVersion((value) => value + 1), [])
|
|
160
|
+
return { ...state, reload }
|
|
161
|
+
}
|