@wangyf397/session_del 1.1.0 → 1.1.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/client.js +53 -49
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
* 删除动作通过 ctx.remote.commands.execute(sessionId, "/session-del delete <id>")
|
|
8
8
|
* 交给 Host 的 /session-del 命令完成——UI 与 Agent 工具共用同一套操作。
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* 本组件只依赖 React 和运行时服务的惰性读取(与本 profile 里可用的
|
|
11
|
+
* thinking-dial 插件同一套写法),不依赖 slot 提供的 hook、不写全局 DOM/样式,
|
|
12
|
+
* 把出错面压到最小。任何缺服务/缺会话都会在行内显示原因,而不是静默失败。
|
|
12
13
|
*
|
|
13
14
|
* @module @wangyf397/session_del/client
|
|
14
15
|
*/
|
|
@@ -24,7 +25,7 @@ window.__ModuleLoader__.load({
|
|
|
24
25
|
|
|
25
26
|
/** 中英文案。 */
|
|
26
27
|
const zh = {
|
|
27
|
-
'menu.delete': '
|
|
28
|
+
'menu.delete': '彻底删除会话(不可恢复)',
|
|
28
29
|
'menu.confirm': '再点一次,彻底删除',
|
|
29
30
|
'menu.busy': '正在删除…',
|
|
30
31
|
'menu.done': '已彻底删除',
|
|
@@ -45,9 +46,34 @@ window.__ModuleLoader__.load({
|
|
|
45
46
|
'error.unknownCommand': 'The Host has no /session-del command (is the plugin active?)',
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/** 当前 client 上下文(惰性读取服务,与 provider 顺序无关)。 */
|
|
50
|
+
let pluginCtx
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 从一个 Cordis 上下文里读服务,服务尚未提供时返回 undefined 而不抛错。
|
|
54
|
+
* @param {object|undefined} host 上下文,apply 之前为 undefined。
|
|
55
|
+
* @param {string} name 服务名。
|
|
56
|
+
* @returns {*} 服务实例或 undefined。
|
|
57
|
+
*/
|
|
58
|
+
function readService(host, name) {
|
|
59
|
+
if (host === undefined || host === null) return undefined
|
|
60
|
+
try {
|
|
61
|
+
const viaGet = host.get(name)
|
|
62
|
+
if (viaGet !== undefined) return viaGet
|
|
63
|
+
} catch {
|
|
64
|
+
/* 走属性形式 */
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
return host[name]
|
|
68
|
+
} catch {
|
|
69
|
+
return undefined
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
48
73
|
/** 当前主视图里的会话 id:命令需要一个 agent 作为执行者。 */
|
|
49
|
-
function mainViewSessionId(
|
|
50
|
-
const byId =
|
|
74
|
+
function mainViewSessionId(sessions, excludeId) {
|
|
75
|
+
const byId = sessions?.list?.getSnapshot?.()?.byId
|
|
76
|
+
if (!byId) return undefined
|
|
51
77
|
for (const row of Object.values(byId)) {
|
|
52
78
|
if (!row || String(row.id) === String(excludeId)) continue
|
|
53
79
|
if ((row.retainedBy?.mainView ?? 0) > 0) return String(row.id)
|
|
@@ -95,52 +121,40 @@ window.__ModuleLoader__.load({
|
|
|
95
121
|
|
|
96
122
|
/**
|
|
97
123
|
* 侧栏会话菜单里的一行:idle → confirm → busy → done/failed。
|
|
98
|
-
* @param {object} props sessionId / displayTitle /
|
|
124
|
+
* @param {object} props sessionId / displayTitle / t
|
|
99
125
|
*/
|
|
100
126
|
function DeleteMenuItem(props) {
|
|
101
|
-
const { sessionId, displayTitle,
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const sessionState = useSessions((state) => state)
|
|
127
|
+
const { sessionId, displayTitle, t } = props
|
|
128
|
+
const id = String(sessionId ?? '')
|
|
129
|
+
const tt = typeof t === 'function' ? t : (key, vars) => (vars ? `${key} ${JSON.stringify(vars)}` : key)
|
|
105
130
|
|
|
106
131
|
const [phase, setPhase] = React.useState('idle')
|
|
107
132
|
const [reason, setReason] = React.useState('')
|
|
108
133
|
const [hover, setHover] = React.useState(false)
|
|
109
134
|
const [focused, setFocused] = React.useState(false)
|
|
110
|
-
const timerRef = React.useRef(null)
|
|
111
135
|
|
|
112
|
-
|
|
113
|
-
() => () => {
|
|
114
|
-
if (timerRef.current !== null) clearTimeout(timerRef.current)
|
|
115
|
-
},
|
|
116
|
-
[]
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
/** 执行删除。 */
|
|
136
|
+
/** 执行删除(直接读已注入的 remote.commands 命名空间与 sessions 服务)。 */
|
|
120
137
|
const run = async () => {
|
|
121
|
-
const
|
|
122
|
-
if (!
|
|
138
|
+
const commands = readService(pluginCtx, 'remote.commands')
|
|
139
|
+
if (!commands || typeof commands.execute !== 'function') {
|
|
123
140
|
setPhase('failed')
|
|
124
|
-
setReason(
|
|
141
|
+
setReason(tt('error.noRemote'))
|
|
125
142
|
return
|
|
126
143
|
}
|
|
127
|
-
const
|
|
144
|
+
const sessions = readService(pluginCtx, 'sessions')
|
|
145
|
+
const runner = mainViewSessionId(sessions, id)
|
|
128
146
|
if (!runner) {
|
|
129
147
|
setPhase('failed')
|
|
130
|
-
setReason(
|
|
148
|
+
setReason(tt('error.noRunner'))
|
|
131
149
|
return
|
|
132
150
|
}
|
|
133
151
|
setPhase('busy')
|
|
134
152
|
try {
|
|
135
|
-
const result = await
|
|
136
|
-
const outcome = readResult(result,
|
|
153
|
+
const result = await commands.execute(runner, `/session-del delete ${id}`, [])
|
|
154
|
+
const outcome = readResult(result, tt)
|
|
137
155
|
if (outcome.ok) {
|
|
138
156
|
setPhase('done')
|
|
139
157
|
setReason('')
|
|
140
|
-
timerRef.current = setTimeout(() => {
|
|
141
|
-
timerRef.current = null
|
|
142
|
-
setMenuOpen(false)
|
|
143
|
-
}, 1200)
|
|
144
158
|
} else {
|
|
145
159
|
setPhase('failed')
|
|
146
160
|
setReason(outcome.text || 'error')
|
|
@@ -153,14 +167,14 @@ window.__ModuleLoader__.load({
|
|
|
153
167
|
|
|
154
168
|
const label =
|
|
155
169
|
phase === 'confirm'
|
|
156
|
-
?
|
|
170
|
+
? tt('menu.confirm')
|
|
157
171
|
: phase === 'busy'
|
|
158
|
-
?
|
|
172
|
+
? tt('menu.busy')
|
|
159
173
|
: phase === 'done'
|
|
160
|
-
?
|
|
174
|
+
? tt('menu.done')
|
|
161
175
|
: phase === 'failed'
|
|
162
|
-
?
|
|
163
|
-
:
|
|
176
|
+
? tt('menu.failed', { reason })
|
|
177
|
+
: tt('menu.delete')
|
|
164
178
|
|
|
165
179
|
const danger = phase === 'confirm' || phase === 'failed'
|
|
166
180
|
const busy = phase === 'busy'
|
|
@@ -190,13 +204,6 @@ window.__ModuleLoader__.load({
|
|
|
190
204
|
outlineOffset: focused ? -2 : 0,
|
|
191
205
|
}
|
|
192
206
|
|
|
193
|
-
const labelStyle = {
|
|
194
|
-
minWidth: 0,
|
|
195
|
-
overflow: 'hidden',
|
|
196
|
-
whiteSpace: 'nowrap',
|
|
197
|
-
textOverflow: 'ellipsis',
|
|
198
|
-
}
|
|
199
|
-
|
|
200
207
|
return h(
|
|
201
208
|
'button',
|
|
202
209
|
{
|
|
@@ -205,7 +212,7 @@ window.__ModuleLoader__.load({
|
|
|
205
212
|
style,
|
|
206
213
|
title: [displayTitle, phase === 'failed' ? reason : undefined].filter(Boolean).join(' — ') || undefined,
|
|
207
214
|
disabled: busy,
|
|
208
|
-
'aria-label':
|
|
215
|
+
'aria-label': tt('menu.delete'),
|
|
209
216
|
onMouseEnter: () => setHover(true),
|
|
210
217
|
onMouseLeave: () => setHover(false),
|
|
211
218
|
onFocus: () => setFocused(true),
|
|
@@ -221,17 +228,14 @@ window.__ModuleLoader__.load({
|
|
|
221
228
|
},
|
|
222
229
|
},
|
|
223
230
|
h('span', { key: 'icon', style: { display: 'inline-flex', flex: 'none', width: 14, height: 14 } }, TrashIcon()),
|
|
224
|
-
h('span', { key: 'label', style:
|
|
231
|
+
h('span', { key: 'label', style: { minWidth: 0, overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' } }, label)
|
|
225
232
|
)
|
|
226
233
|
}
|
|
227
234
|
|
|
228
|
-
/** 当前 client 上下文(供点击时按需取用 ctx.remote)。 */
|
|
229
|
-
let ctxRef = null
|
|
230
|
-
|
|
231
235
|
return {
|
|
232
|
-
inject: ['slots', 'locale'],
|
|
236
|
+
inject: ['slots', 'locale', 'remote', 'remote.commands', 'sessions'],
|
|
233
237
|
apply(ctx) {
|
|
234
|
-
|
|
238
|
+
pluginCtx = ctx
|
|
235
239
|
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'session_del: dictionaries')
|
|
236
240
|
|
|
237
241
|
ctx.slots.inject('sidebar.workspaces.session.menu.item', () =>
|
package/package.json
CHANGED