dsh-plugin 0.1.1 → 1.0.0
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/README.en.md +49 -26
- package/README.md +49 -26
- package/client/client.js +2148 -229
- package/client/client.js.map +1 -1
- package/lib/http/routes.js +396 -0
- package/lib/index.js +17 -8
- package/lib/services/install-types.js +11 -0
- package/lib/services/install.js +4 -0
- package/lib/services/installed-versions.js +51 -0
- package/lib/services/loader.js +98 -0
- package/lib/services/pending-restart.js +34 -0
- package/lib/services/preflight.js +94 -0
- package/lib/services/profile.js +88 -0
- package/lib/services/progress.js +50 -0
- package/lib/services/task-queue.js +417 -0
- package/lib/types/http/routes.d.ts +23 -0
- package/lib/types/index.d.ts +27 -0
- package/lib/types/services/install-types.d.ts +63 -0
- package/lib/types/services/install.d.ts +18 -0
- package/lib/types/services/installed-versions.d.ts +14 -0
- package/lib/types/services/loader.d.ts +43 -0
- package/lib/types/services/pending-restart.d.ts +13 -0
- package/lib/types/services/preflight.d.ts +6 -0
- package/lib/types/services/profile.d.ts +24 -0
- package/lib/types/services/progress.d.ts +17 -0
- package/lib/types/services/task-queue.d.ts +76 -0
- package/package.json +27 -6
- package/src/client/components/CatalogControls.tsx +104 -0
- package/src/client/components/CatalogHeader.tsx +78 -0
- package/src/client/components/CatalogList.tsx +75 -0
- package/src/client/components/CategoryTabs.tsx +35 -0
- package/src/client/components/Dropdown.tsx +80 -0
- package/src/client/components/NotificationsModal.tsx +230 -0
- package/src/client/components/PluginCard.tsx +104 -0
- package/src/client/components/PluginHubSection.tsx +339 -0
- package/src/client/components/ProgressView.tsx +23 -0
- package/src/client/components/icons.tsx +125 -0
- package/src/client/components/modals.tsx +365 -0
- package/src/client/hooks/useCatalog.ts +192 -0
- package/src/client/hooks/useTaskQueue.ts +405 -0
- package/src/client/index.tsx +8 -422
- package/src/client/lib/catalog.ts +204 -0
- package/src/client/lib/env.ts +16 -0
- package/src/client/lib/failures.ts +153 -0
- package/src/client/lib/format.ts +22 -0
- package/src/client/locales.ts +186 -18
- package/src/client/styles/Dropdown.module.css +123 -0
- package/src/client/styles/Header.module.css +432 -0
- package/src/client/styles/List.module.css +324 -0
- package/src/client/styles/Modal.module.css +965 -0
- package/src/client/styles/tokens.module.css +86 -0
- package/src/client/types.ts +80 -0
- package/src/server/http/routes.ts +397 -0
- package/src/server/index.ts +35 -0
- package/src/server/services/install-types.ts +63 -0
- package/src/server/services/install.ts +18 -0
- package/src/server/services/installed-versions.ts +65 -0
- package/src/server/services/loader.ts +110 -0
- package/src/server/services/pending-restart.ts +36 -0
- package/src/server/services/preflight.ts +97 -0
- package/src/server/services/profile.ts +89 -0
- package/src/server/services/progress.ts +48 -0
- package/src/server/services/task-queue.ts +450 -0
- package/src/client/Section.module.css +0 -589
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background install/remove task queue for the Plugin Hub.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the server-side FIFO active queue by polling /active, settles
|
|
5
|
+
* vanished tasks against /status, and exposes the queue actions (install,
|
|
6
|
+
* uninstall, cancel) plus the modal task lookups used by the dialogs.
|
|
7
|
+
*/
|
|
8
|
+
import { useEffect, useRef, useState } from 'react'
|
|
9
|
+
import type { HubPlugin, Translate } from '../types.ts'
|
|
10
|
+
|
|
11
|
+
/** 客户端任务队列项:镜像服务端 active 任务(running 在前、pending 在后)。 */
|
|
12
|
+
export interface QueueTask {
|
|
13
|
+
id: number
|
|
14
|
+
kind: 'install' | 'uninstall'
|
|
15
|
+
/** 展示目标:owner/repo(安装)或 npm 包名(卸载) */
|
|
16
|
+
target: string
|
|
17
|
+
/** 插件中文简介(入队时从插件数据带过来,轮询合并时保留):展示在仓库名下方,让人知道排队的到底是什么 */
|
|
18
|
+
desc?: string
|
|
19
|
+
/** 所属插件仓库(owner/repo):失败时错误弹窗据此提供「去仓库反馈」入口;卸载为 null */
|
|
20
|
+
repo: string | null
|
|
21
|
+
/** 安装时的目录信号快照(版本 + 仓库更新时间):任务成功后在本地记录,供「有更新」比对 */
|
|
22
|
+
version?: string
|
|
23
|
+
updatedAt?: string
|
|
24
|
+
/** pending 排队中 / running 执行中 / cancelling 取消中(客户端乐观过渡态) */
|
|
25
|
+
status: 'pending' | 'running' | 'cancelling'
|
|
26
|
+
progress: number
|
|
27
|
+
lines: string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 客户端待重启项:镜像服务端内存列表 + 本地补齐的展示信息(简介/版本)。 */
|
|
31
|
+
export interface PendingRestart {
|
|
32
|
+
/** owner/repo(展示用,已去 github: 前缀) */
|
|
33
|
+
target: string
|
|
34
|
+
/** 待重启语义:install 装完等挂载 / uninstall 卸完等清理 */
|
|
35
|
+
kind: 'install' | 'uninstall'
|
|
36
|
+
at: number
|
|
37
|
+
desc?: string
|
|
38
|
+
version?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TaskQueueOptions {
|
|
42
|
+
t: Translate
|
|
43
|
+
refreshInstalled: () => void
|
|
44
|
+
/** 任务成功完成:viaModal 表示任务对应当前打开弹窗(弹窗切结果视图),否则走 Toast;repo 供成功通知记录。 */
|
|
45
|
+
onInstallDone: (viaModal: boolean, repo: string | null) => void
|
|
46
|
+
onUninstallDone: (viaModal: boolean, repo: string | null) => void
|
|
47
|
+
/** 任务失败:完整输出或兜底文案 + 所属插件仓库 + 操作类型(安装/卸载)。 */
|
|
48
|
+
onError: (message: string, repo: string | null, kind: 'install' | 'uninstall') => void
|
|
49
|
+
/** 当前打开的安装/卸载弹窗插件:用于在弹窗内匹配进行中的任务。 */
|
|
50
|
+
installPlugin: HubPlugin | null
|
|
51
|
+
uninstallPlugin: HubPlugin | null
|
|
52
|
+
installedName: (p: HubPlugin) => string | null
|
|
53
|
+
/** 待重启项展示信息补齐:按 owner/repo 从插件目录解析简介/版本;找不到返回 null。 */
|
|
54
|
+
resolvePending: (repo: string) => { desc?: string; version?: string } | null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function useTaskQueue(opts: TaskQueueOptions) {
|
|
58
|
+
const {
|
|
59
|
+
t, refreshInstalled, onInstallDone, onUninstallDone, onError,
|
|
60
|
+
installPlugin, uninstallPlugin, installedName, resolvePending,
|
|
61
|
+
} = opts
|
|
62
|
+
const [queue, setQueue] = useState<QueueTask[]>([])
|
|
63
|
+
const queueRef = useRef<QueueTask[]>([])
|
|
64
|
+
/** 待重启列表(服务端内存态 + 本地展示信息补齐);与 queue 一样以 ref 为唯一事实来源。 */
|
|
65
|
+
const [pendingRestarts, setPendingRestarts] = useState<PendingRestart[]>([])
|
|
66
|
+
const pendingRef = useRef<PendingRestart[]>([])
|
|
67
|
+
/** 本次会话内安装成功插件的展示信息(目标 → 简介/版本):轮询合并待重启列表时补齐,刷新后由 resolvePending 兜底。 */
|
|
68
|
+
const pendingInfoRef = useRef<Map<string, { desc?: string; version?: string }>>(new Map())
|
|
69
|
+
/** 保持最新的 resolvePending:轮询闭包捕获的是调用那一刻的目录状态,目录晚加载时也能补齐。 */
|
|
70
|
+
const resolvePendingRef = useRef(resolvePending)
|
|
71
|
+
resolvePendingRef.current = resolvePending
|
|
72
|
+
/** 当前打开弹窗所对应的任务 id:该任务完成时弹窗切换为结果视图 */
|
|
73
|
+
const modalTaskRef = useRef<number | null>(null)
|
|
74
|
+
/** 轮询定时器句柄(队列清空/组件卸载时清理) */
|
|
75
|
+
const pollRef = useRef<number | null>(null)
|
|
76
|
+
/** 请求在途的目标集合(同步防重):双击安装/卸载时第二击直接忽略。
|
|
77
|
+
* 本地队列任务要等 fetch 返回后才入队,仅靠 queueRef 检查拦不住请求窗口内的重复点击。 */
|
|
78
|
+
const submittingRef = useRef<Set<string>>(new Set())
|
|
79
|
+
/** 请求在途标记:弹窗据此禁用确认按钮,避免等待响应期间被再次点击。 */
|
|
80
|
+
const [submitting, setSubmitting] = useState(false)
|
|
81
|
+
|
|
82
|
+
/** 停止后台任务轮询(任务结束或组件卸载时清理)。 */
|
|
83
|
+
const stopPoll = () => {
|
|
84
|
+
if (pollRef.current !== null) {
|
|
85
|
+
window.clearInterval(pollRef.current)
|
|
86
|
+
pollRef.current = null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 队列与待重启都清空才停轮询:待重启期间持续轮询,宿主一重启(内存列表清空)立刻感知并消失。 */
|
|
91
|
+
const maybeStopPoll = () => {
|
|
92
|
+
if (queueRef.current.length === 0 && pendingRef.current.length === 0) stopPoll()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 组件卸载时兜底清理轮询定时器
|
|
96
|
+
useEffect(() => stopPoll, [])
|
|
97
|
+
|
|
98
|
+
/** 更新队列(同步镜像到 ref:queueRef 是唯一事实来源,state 只负责触发渲染,
|
|
99
|
+
* 避免依赖 React updater 的异步调度时序导致 gone/stopPoll/防重复入队读到旧值)。 */
|
|
100
|
+
const applyQueue = (updater: (prev: QueueTask[]) => QueueTask[]) => {
|
|
101
|
+
const next = updater(queueRef.current)
|
|
102
|
+
queueRef.current = next
|
|
103
|
+
setQueue(next)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 解析服务端待重启列表(`github:` 前缀已去掉,与安装任务的 target 对齐)。 */
|
|
107
|
+
const parsePendingRestarts = (value: unknown): PendingRestart[] => {
|
|
108
|
+
if (!Array.isArray(value)) return []
|
|
109
|
+
const out: PendingRestart[] = []
|
|
110
|
+
for (const x of value) {
|
|
111
|
+
if (typeof x !== 'object' || x === null) continue
|
|
112
|
+
const target = (x as { target?: unknown }).target
|
|
113
|
+
if (typeof target !== 'string' || target === '') continue
|
|
114
|
+
const at = (x as { at?: unknown }).at
|
|
115
|
+
out.push({
|
|
116
|
+
target: target.replace(/^github:/, ''),
|
|
117
|
+
kind: (x as { kind?: unknown }).kind === 'uninstall' ? 'uninstall' : 'install',
|
|
118
|
+
at: typeof at === 'number' ? at : 0,
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
return out
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 合并服务端待重启列表:保留已有展示信息,缺失时用会话内记录 / 目录解析补齐。 */
|
|
125
|
+
const applyPending = (raw: PendingRestart[]) => {
|
|
126
|
+
const prev = pendingRef.current
|
|
127
|
+
const next = raw.map((p) => {
|
|
128
|
+
const old = prev.find((q) => q.target === p.target)
|
|
129
|
+
if (old?.desc !== undefined) return { ...p, desc: old.desc, version: old.version }
|
|
130
|
+
const info = pendingInfoRef.current.get(p.target) ?? resolvePendingRef.current?.(p.target) ?? null
|
|
131
|
+
return info ? { ...p, desc: info.desc, version: info.version } : p
|
|
132
|
+
})
|
|
133
|
+
pendingRef.current = next
|
|
134
|
+
setPendingRestarts(next)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 向宿主记录某插件的安装时目录信号(version + updatedAt);version 为空时清除记录(卸载)。 */
|
|
138
|
+
const syncInstalledVersion = async (repo: string | null, version: string | undefined, updatedAt: string | undefined) => {
|
|
139
|
+
if (!repo) return
|
|
140
|
+
try {
|
|
141
|
+
await fetch('/dsh-plugin-hub/installed-version', {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: { 'content-type': 'application/json' },
|
|
144
|
+
body: JSON.stringify({ repo, version: version ?? null, updatedAt }),
|
|
145
|
+
})
|
|
146
|
+
} catch {
|
|
147
|
+
// 宿主未挂本地路由:静默忽略,仅本机展示受影响
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 后台任务结束(active 中消失后查终态确认):完成 → 刷新安装表 + 结果视图/toast;失败 → 完整错误弹窗 */
|
|
152
|
+
const finishQueueTask = (ok: boolean, q: QueueTask, lines: string[]) => {
|
|
153
|
+
applyQueue((prev) => prev.filter((x) => x.id !== q.id))
|
|
154
|
+
if (ok) {
|
|
155
|
+
refreshInstalled()
|
|
156
|
+
if (q.kind === 'uninstall') {
|
|
157
|
+
// 卸载成功:清掉本地版本记录
|
|
158
|
+
void syncInstalledVersion(q.repo, undefined, undefined)
|
|
159
|
+
onUninstallDone(modalTaskRef.current === q.id, q.repo)
|
|
160
|
+
} else {
|
|
161
|
+
// 安装成功:记录安装时的目录信号(版本 + 仓库更新时间),供「有更新」比对;
|
|
162
|
+
// 同时缓存展示信息,待重启列表(服务端已登记)合并时直接补齐简介/版本
|
|
163
|
+
void syncInstalledVersion(q.repo, q.version, q.updatedAt)
|
|
164
|
+
pendingInfoRef.current.set(q.target, { desc: q.desc, version: q.version })
|
|
165
|
+
onInstallDone(modalTaskRef.current === q.id, q.repo)
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
// 失败:完整展示全部输出行(最新在前,逆序为日志阅读顺序),不裁剪
|
|
169
|
+
const detail = lines.length > 0
|
|
170
|
+
? [...lines].reverse().join('\n')
|
|
171
|
+
: q.kind === 'uninstall' ? t('uninstallFail') : t('installFail')
|
|
172
|
+
onError(detail, q.repo, q.kind)
|
|
173
|
+
}
|
|
174
|
+
maybeStopPoll()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 任务在 /active 中消失 = 已结束:查 /status 拿终态并收尾(cancelled 静默移除)。 */
|
|
178
|
+
const settleTask = async (q: QueueTask) => {
|
|
179
|
+
let status = 'failed'
|
|
180
|
+
let lines: string[] = []
|
|
181
|
+
try {
|
|
182
|
+
const res = await fetch(`/dsh-plugin-hub/status?task=${q.id}`, { cache: 'no-store' })
|
|
183
|
+
if (res.ok) {
|
|
184
|
+
const data = await res.json() as { task?: { status?: string; lines?: string[] } }
|
|
185
|
+
status = data.task?.status ?? 'failed'
|
|
186
|
+
lines = data.task?.lines ?? []
|
|
187
|
+
}
|
|
188
|
+
} catch { /* 服务端不可达:按失败处理 */ }
|
|
189
|
+
if (status === 'done') finishQueueTask(true, q, lines)
|
|
190
|
+
else if (status === 'failed') finishQueueTask(false, q, lines)
|
|
191
|
+
else {
|
|
192
|
+
applyQueue((prev) => prev.filter((x) => x.id !== q.id)) // cancelled / 未知
|
|
193
|
+
maybeStopPoll()
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 轮询整个队列:合并服务端 /active(running 在前、pending 在后),
|
|
198
|
+
* 消失的任务查终态收尾;队列清空即停止轮询。 */
|
|
199
|
+
const pollQueue = () => {
|
|
200
|
+
stopPoll()
|
|
201
|
+
pollRef.current = window.setInterval(async () => {
|
|
202
|
+
try {
|
|
203
|
+
const res = await fetch('/dsh-plugin-hub/active', { cache: 'no-store' })
|
|
204
|
+
if (!res.ok) throw new Error(`active ${res.status}`)
|
|
205
|
+
const data = await res.json() as {
|
|
206
|
+
tasks?: { id?: unknown; action?: unknown; target?: unknown; status?: unknown; progress?: unknown; lines?: unknown }[]
|
|
207
|
+
pendingRestarts?: unknown
|
|
208
|
+
}
|
|
209
|
+
const active = (data.tasks ?? []).filter((a) => typeof a.id === 'number')
|
|
210
|
+
const byId = new Map(active.map((a) => [a.id as number, a]))
|
|
211
|
+
// 先捕获旧队列:gone 是「旧队列里服务端已消失的任务」的差集,必须在 applyQueue 更新前取
|
|
212
|
+
const prevQueue = queueRef.current
|
|
213
|
+
// 待重启列表同步(服务端内存态:宿主重启后自然变空,这里随之清空 UI)
|
|
214
|
+
applyPending(parsePendingRestarts(data.pendingRestarts))
|
|
215
|
+
// 合并:running 在前、pending 在后(与服务端一致),新出现任务补入队列
|
|
216
|
+
applyQueue((prev) => {
|
|
217
|
+
const next: QueueTask[] = []
|
|
218
|
+
for (const a of active) {
|
|
219
|
+
const id = a.id as number
|
|
220
|
+
const prevTask = prev.find((q) => q.id === id)
|
|
221
|
+
const isRemove = a.action === 'remove'
|
|
222
|
+
next.push({
|
|
223
|
+
id,
|
|
224
|
+
kind: isRemove ? 'uninstall' : 'install',
|
|
225
|
+
target: typeof a.target === 'string' ? a.target.replace(/^github:/, '') : (prevTask?.target ?? ''),
|
|
226
|
+
desc: prevTask?.desc,
|
|
227
|
+
repo: prevTask?.repo ?? (isRemove ? null : (typeof a.target === 'string' ? a.target.replace(/^github:/, '') : null)),
|
|
228
|
+
// 安装时快照的目录信号:轮询合并时保留,任务成功后记录到本地
|
|
229
|
+
version: prevTask?.version,
|
|
230
|
+
updatedAt: prevTask?.updatedAt,
|
|
231
|
+
status: a.status === 'running' ? 'running' : 'pending',
|
|
232
|
+
progress: typeof a.progress === 'number' ? a.progress : prevTask?.progress ?? 0,
|
|
233
|
+
lines: Array.isArray(a.lines) ? (a.lines as string[]) : (prevTask?.lines ?? []),
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
return next
|
|
237
|
+
})
|
|
238
|
+
// 本地有、服务端已消失 → 已结束:逐个查终态收尾
|
|
239
|
+
// (cancelling 是客户端乐观过渡态,交给 cancelTask 的定时移除,不在轮询里提前收敛)
|
|
240
|
+
const gone = prevQueue.filter((q) => !byId.has(q.id) && q.status !== 'cancelling')
|
|
241
|
+
for (const q of gone) await settleTask(q)
|
|
242
|
+
} catch {
|
|
243
|
+
// 服务端暂不可达:静默等待下一轮
|
|
244
|
+
}
|
|
245
|
+
}, 600)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 刷新后恢复任务队列与待重启列表:任务注册表在服务端内存里,只要宿主进程没重启就还在;
|
|
249
|
+
// 重新拉取 /active(数组)继续轮询,界面上显示「进行中」状态条,可展开查看实时输出。
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
let cancelled = false
|
|
252
|
+
const restore = async () => {
|
|
253
|
+
try {
|
|
254
|
+
const res = await fetch('/dsh-plugin-hub/active', { cache: 'no-store' })
|
|
255
|
+
if (!res.ok) return
|
|
256
|
+
const data = await res.json() as {
|
|
257
|
+
tasks?: { id?: unknown; action?: unknown; target?: unknown; status?: unknown; progress?: unknown; lines?: unknown }[]
|
|
258
|
+
pendingRestarts?: unknown
|
|
259
|
+
}
|
|
260
|
+
if (cancelled) return
|
|
261
|
+
// 待重启:装完没重启的持久提醒,刷新页面也要恢复
|
|
262
|
+
applyPending(parsePendingRestarts(data.pendingRestarts))
|
|
263
|
+
const items: QueueTask[] = (data.tasks ?? [])
|
|
264
|
+
.filter((a) => typeof a.id === 'number')
|
|
265
|
+
.map((a) => {
|
|
266
|
+
const isRemove = a.action === 'remove'
|
|
267
|
+
return {
|
|
268
|
+
id: a.id as number,
|
|
269
|
+
kind: (isRemove ? 'uninstall' : 'install') as 'install' | 'uninstall',
|
|
270
|
+
target: typeof a.target === 'string' ? a.target.replace(/^github:/, '') : '',
|
|
271
|
+
repo: isRemove ? null : (typeof a.target === 'string' ? a.target.replace(/^github:/, '') : null),
|
|
272
|
+
status: a.status === 'running' ? 'running' : 'pending',
|
|
273
|
+
progress: typeof a.progress === 'number' ? a.progress : 0,
|
|
274
|
+
lines: Array.isArray(a.lines) ? (a.lines as string[]) : [],
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
if (items.length > 0 || pendingRef.current.length > 0) {
|
|
278
|
+
applyQueue((prev) => [...prev, ...items.filter((n) => !prev.some((p) => p.id === n.id))])
|
|
279
|
+
// 刷新后恢复的任务同样保持折叠:摘要条提示进行中,点开才看明细/日志
|
|
280
|
+
pollQueue()
|
|
281
|
+
}
|
|
282
|
+
} catch { /* host without the plugin's server routes */ }
|
|
283
|
+
}
|
|
284
|
+
void restore()
|
|
285
|
+
return () => { cancelled = true }
|
|
286
|
+
}, [])
|
|
287
|
+
|
|
288
|
+
/** 弹窗动作:直接安装。请求宿主本地路由,任务进入服务端队列(FIFO),弹窗内实时显示进度。 */
|
|
289
|
+
const installNow = async (p: HubPlugin, opts?: { update?: boolean }) => {
|
|
290
|
+
const repo = p.source?.repo ?? ''
|
|
291
|
+
if (!repo) return
|
|
292
|
+
// 防重复入队:同一目标已在排队/执行中则忽略
|
|
293
|
+
if (queueRef.current.some((q) => q.kind === 'install' && q.target === repo)) return
|
|
294
|
+
// 请求在途防重:任务要等响应回来才进本地队列,这之前再次点击(双击)直接忽略
|
|
295
|
+
if (submittingRef.current.has(repo)) return
|
|
296
|
+
submittingRef.current.add(repo)
|
|
297
|
+
setSubmitting(true)
|
|
298
|
+
try {
|
|
299
|
+
const res = await fetch('/dsh-plugin-hub/install', {
|
|
300
|
+
method: 'POST',
|
|
301
|
+
headers: { 'content-type': 'application/json' },
|
|
302
|
+
body: JSON.stringify({ repo, mode: opts?.update ? 'update' : undefined }),
|
|
303
|
+
})
|
|
304
|
+
const data = await res.json() as { ok?: boolean; task?: number; error?: string }
|
|
305
|
+
if (!data.ok || typeof data.task !== 'number') {
|
|
306
|
+
// 请求层失败(重复安装 409 / 参数错误等):完整错误弹窗
|
|
307
|
+
onError(data.error ?? `HTTP ${res.status}`, repo, 'install')
|
|
308
|
+
return
|
|
309
|
+
}
|
|
310
|
+
const taskId = data.task
|
|
311
|
+
modalTaskRef.current = taskId
|
|
312
|
+
applyQueue((prev) => [...prev, { id: taskId, kind: 'install', target: repo, repo, desc: p.description, version: p.version, updatedAt: p.dates?.repoUpdatedAt, status: 'pending', progress: 0, lines: [] }])
|
|
313
|
+
// 提前缓存展示信息:安装成功后服务端登记待重启,轮询合并时不用等目录解析
|
|
314
|
+
pendingInfoRef.current.set(repo, { desc: p.description, version: p.version })
|
|
315
|
+
// 摘要条常驻顶部(含实时进度),明细面板保持折叠,想看时再点开
|
|
316
|
+
pollQueue()
|
|
317
|
+
} catch {
|
|
318
|
+
onError(t('installFail'), repo, 'install')
|
|
319
|
+
} finally {
|
|
320
|
+
submittingRef.current.delete(repo)
|
|
321
|
+
setSubmitting(false)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** 弹窗动作:直接卸载。与安装同一队列机制,弹窗内实时显示进度。 */
|
|
326
|
+
const uninstallNow = async (p: HubPlugin) => {
|
|
327
|
+
const name = installedName(p)
|
|
328
|
+
if (!name) return
|
|
329
|
+
const repo = p.source?.repo ?? null
|
|
330
|
+
if (queueRef.current.some((q) => q.kind === 'uninstall' && q.target === name)) return
|
|
331
|
+
// 请求在途防重:与安装一致,响应回来前再次点击直接忽略
|
|
332
|
+
if (submittingRef.current.has(name)) return
|
|
333
|
+
submittingRef.current.add(name)
|
|
334
|
+
setSubmitting(true)
|
|
335
|
+
try {
|
|
336
|
+
const res = await fetch('/dsh-plugin-hub/uninstall', {
|
|
337
|
+
method: 'POST',
|
|
338
|
+
headers: { 'content-type': 'application/json' },
|
|
339
|
+
body: JSON.stringify({ name, repo: repo ?? undefined }),
|
|
340
|
+
})
|
|
341
|
+
const data = await res.json() as { ok?: boolean; task?: number; error?: string }
|
|
342
|
+
if (!data.ok || typeof data.task !== 'number') {
|
|
343
|
+
onError(data.error ?? `HTTP ${res.status}`, repo, 'uninstall')
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
const taskId = data.task
|
|
347
|
+
modalTaskRef.current = taskId
|
|
348
|
+
applyQueue((prev) => [...prev, { id: taskId, kind: 'uninstall', target: name, desc: p.description, repo, status: 'pending', progress: 0, lines: [] }])
|
|
349
|
+
// 摘要条常驻顶部(含实时进度),明细面板保持折叠,想看时再点开
|
|
350
|
+
pollQueue()
|
|
351
|
+
} catch {
|
|
352
|
+
onError(t('uninstallFail'), repo, 'uninstall')
|
|
353
|
+
} finally {
|
|
354
|
+
submittingRef.current.delete(name)
|
|
355
|
+
setSubmitting(false)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** 取消任务:排队中立即出队,执行中终止子进程;先标记「正在取消」短暂过渡后再移除。 */
|
|
360
|
+
const cancelTask = async (id: number) => {
|
|
361
|
+
if (modalTaskRef.current === id) modalTaskRef.current = null
|
|
362
|
+
// 乐观标记「正在取消」,给用户一个可见反馈,而不是瞬间消失
|
|
363
|
+
applyQueue((prev) => prev.map((q) => (q.id === id ? { ...q, status: 'cancelling' as const } : q)))
|
|
364
|
+
try {
|
|
365
|
+
await fetch('/dsh-plugin-hub/cancel', {
|
|
366
|
+
method: 'POST',
|
|
367
|
+
headers: { 'content-type': 'application/json' },
|
|
368
|
+
body: JSON.stringify({ id }),
|
|
369
|
+
})
|
|
370
|
+
} catch { /* 服务端不可达:仍按取消处理,稍后移除 */ }
|
|
371
|
+
// 短暂展示「正在取消」后移除;队列清空则停止轮询
|
|
372
|
+
window.setTimeout(() => {
|
|
373
|
+
applyQueue((prev) => prev.filter((x) => x.id !== id))
|
|
374
|
+
maybeStopPoll()
|
|
375
|
+
}, 700)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** 打开新弹窗前清空任务匹配:避免旧任务 id 误匹配到新弹窗。 */
|
|
379
|
+
const clearModalTask = () => { modalTaskRef.current = null }
|
|
380
|
+
|
|
381
|
+
// 弹窗对应任务:优先当前弹窗发起任务,其次按目标匹配(重新打开弹窗 / 目标已在队列时也能展示实时进度)
|
|
382
|
+
// 排除 cancelling:取消中的任务不再关联到弹窗,弹窗回到「确认」态可重新操作
|
|
383
|
+
const installModalTask = installPlugin
|
|
384
|
+
? (queue.find((q) => q.id === modalTaskRef.current && q.status !== 'cancelling')
|
|
385
|
+
?? queue.find((q) => q.kind === 'install' && q.target === (installPlugin.source?.repo ?? '') && q.status !== 'cancelling')
|
|
386
|
+
?? null)
|
|
387
|
+
: null
|
|
388
|
+
const uninstallModalTask = uninstallPlugin
|
|
389
|
+
? (queue.find((q) => q.id === modalTaskRef.current && q.status !== 'cancelling')
|
|
390
|
+
?? queue.find((q) => q.kind === 'uninstall' && q.target === (installedName(uninstallPlugin) ?? '') && q.status !== 'cancelling')
|
|
391
|
+
?? null)
|
|
392
|
+
: null
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
queue,
|
|
396
|
+
pendingRestarts,
|
|
397
|
+
installModalTask,
|
|
398
|
+
uninstallModalTask,
|
|
399
|
+
submitting,
|
|
400
|
+
installNow,
|
|
401
|
+
uninstallNow,
|
|
402
|
+
cancelTask,
|
|
403
|
+
clearModalTask,
|
|
404
|
+
}
|
|
405
|
+
}
|