dsh-remote-vs 0.26.0 → 0.28.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 +112 -28
- package/README.md +84 -25
- package/THIRD-PARTY-NOTICES.md +7 -2
- package/lib/client.js +1237 -254
- package/lib/index.js +821 -17
- package/lib/types/changes/recorder.d.ts +119 -2
- package/lib/types/client/CodeEditor.d.ts +29 -0
- package/lib/types/client/OutlinePanel.d.ts +22 -0
- package/lib/types/client/ProcessesSection.d.ts +19 -0
- package/lib/types/client/api.d.ts +22 -2
- package/lib/types/client/blame-model.d.ts +77 -0
- package/lib/types/client/changes/apply-failure.d.ts +41 -0
- package/lib/types/client/changes/block-confirm.d.ts +97 -0
- package/lib/types/client/changes/diff-outcome.d.ts +56 -0
- package/lib/types/client/changes/freshness.d.ts +51 -0
- package/lib/types/client/changes/model.d.ts +19 -1
- package/lib/types/client/changes/session-diff.d.ts +100 -0
- package/lib/types/client/changes/staleness.d.ts +98 -0
- package/lib/types/client/changes/turn-reverted.d.ts +82 -0
- package/lib/types/client/changes/turn-view.d.ts +131 -0
- package/lib/types/client/changes/undo-affordance.d.ts +44 -10
- package/lib/types/client/changes/undo-target.d.ts +12 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/outline-model.d.ts +59 -0
- package/lib/types/client/scm/actions.d.ts +12 -1
- package/lib/types/client/store.d.ts +109 -1
- package/lib/types/client/vsc/blame.d.ts +19 -0
- package/lib/types/client/vsc/change-marks.d.ts +21 -0
- package/lib/types/client/vsc/kit.d.ts +0 -2
- package/lib/types/client/vsc/preview-document.d.ts +46 -0
- package/lib/types/client/vsc/preview-model.d.ts +31 -0
- package/lib/types/client/vsc/preview-services.d.ts +24 -0
- package/lib/types/git-ops.d.ts +18 -0
- package/lib/types/processes.d.ts +43 -0
- package/lib/types/protocol.d.ts +133 -1
- package/package.json +2 -1
- package/src/changes/recorder.ts +851 -8
- package/src/client/CodeEditor.tsx +86 -4
- package/src/client/EditorPane.tsx +581 -41
- package/src/client/MonitorView.tsx +16 -5
- package/src/client/OutlinePanel.tsx +287 -0
- package/src/client/ProcessesSection.tsx +292 -0
- package/src/client/api.ts +38 -3
- package/src/client/blame-model.ts +242 -0
- package/src/client/changes/ChangesDock.tsx +539 -54
- package/src/client/changes/apply-failure.ts +73 -0
- package/src/client/changes/block-confirm.ts +180 -0
- package/src/client/changes/changes.css +45 -0
- package/src/client/changes/diff-outcome.ts +97 -0
- package/src/client/changes/freshness.ts +68 -0
- package/src/client/changes/model.ts +56 -1
- package/src/client/changes/session-diff.ts +187 -0
- package/src/client/changes/staleness.ts +148 -0
- package/src/client/changes/turn-reverted.ts +254 -0
- package/src/client/changes/turn-view.ts +195 -0
- package/src/client/changes/undo-affordance.ts +295 -63
- package/src/client/changes/undo-target.ts +12 -0
- package/src/client/i18n.ts +294 -2
- package/src/client/index.tsx +4 -0
- package/src/client/outline-model.ts +775 -0
- package/src/client/scm/DiffView.tsx +32 -13
- package/src/client/scm/HistoryPanel.tsx +38 -1
- package/src/client/scm/actions.ts +55 -2
- package/src/client/store.tsx +446 -21
- package/src/client/vsc/blame.ts +147 -0
- package/src/client/vsc/change-marks.ts +21 -0
- package/src/client/vsc/kit.tsx +3 -11
- package/src/client/vsc/preview-document.tsx +326 -0
- package/src/client/vsc/preview-model.ts +216 -0
- package/src/client/vsc/preview-services.ts +118 -0
- package/src/client/vsc/vsc.css +965 -39
- package/src/git-ops.ts +34 -0
- package/src/processes.ts +177 -0
- package/src/protocol.ts +138 -1
- package/src/routes.ts +138 -9
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
import { useExplorer } from './store.tsx'
|
|
31
31
|
import { optionStyle } from './ui.tsx'
|
|
32
32
|
import { IconButton, IconRefresh, MiniSpinner, Segmented } from './vsc/kit.tsx'
|
|
33
|
+
import { ProcessesSection } from './ProcessesSection.tsx'
|
|
33
34
|
|
|
34
35
|
/** Visible-panel poll cadence. The Host's own reuse window is shorter (3 s). */
|
|
35
36
|
export const MONITOR_POLL_MS = 5_000
|
|
@@ -625,11 +626,21 @@ export function MonitorView(props: {
|
|
|
625
626
|
</div>
|
|
626
627
|
)}
|
|
627
628
|
<div className="rvs-monitor-body">
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
629
|
+
<div className="rvs-monitor-content rvs-scroll">
|
|
630
|
+
{props.hosts.length === 0 && <div className="rvs-banner warn">{t('monitor.noHosts')}</div>}
|
|
631
|
+
{props.hosts.length > 0 && aliases.length === 0 && <div className="rvs-empty">{t('monitor.noHost')}</div>}
|
|
632
|
+
{paused && <div className="rvs-banner info">{t('monitor.paused')}</div>}
|
|
633
|
+
{single !== undefined && renderDetail(single)}
|
|
634
|
+
{single === undefined && aliases.length > 1 && renderAll(aliases)}
|
|
635
|
+
</div>
|
|
636
|
+
<ProcessesSection
|
|
637
|
+
t={t}
|
|
638
|
+
hosts={props.hosts}
|
|
639
|
+
aliases={aliases}
|
|
640
|
+
visible={props.visible}
|
|
641
|
+
paused={paused}
|
|
642
|
+
intervalMs={intervalMs}
|
|
643
|
+
/>
|
|
633
644
|
</div>
|
|
634
645
|
</div>
|
|
635
646
|
)
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 符号大纲浮层:可筛选的符号列表 + ↑↓/Enter/点击跳转。
|
|
3
|
+
*
|
|
4
|
+
* 只在打开时挂载(父组件用 `undefined` 表示关闭),所以「关闭」就是卸载 —— 不会
|
|
5
|
+
* 留下第二个 `isOpen` 状态要同步,也不会在关闭后留一个偷键盘的监听器。条目由
|
|
6
|
+
* `outline-model.ts` 的纯函数产出;这里只负责像素、焦点和按键。
|
|
7
|
+
*
|
|
8
|
+
* PORTAL 到 `<body>`:编辑器有三种座位(侧栏 / 中间 / 浮窗),浮层若留在原 DOM
|
|
9
|
+
* 里会被祖先的 `overflow: hidden` 裁掉。`.rvs-portal` 负责声明 `--rvs-*` token
|
|
10
|
+
* 子集 —— portal 之后已经不在 `.rvs-root` 作用域内,缺了它颜色/边框全是空值。
|
|
11
|
+
*/
|
|
12
|
+
import { useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react'
|
|
13
|
+
import { createPortal } from 'react-dom'
|
|
14
|
+
import { matchPositions } from './quickopen-model.ts'
|
|
15
|
+
import {
|
|
16
|
+
OUTLINE_RESULT_LIMIT,
|
|
17
|
+
buildOutline,
|
|
18
|
+
rankOutline,
|
|
19
|
+
type OutlineEntry,
|
|
20
|
+
} from './outline-model.ts'
|
|
21
|
+
|
|
22
|
+
/** 一行:kind 徽标 + 缩进过的名字 + 行号。 */
|
|
23
|
+
function Row(props: {
|
|
24
|
+
entry: OutlineEntry
|
|
25
|
+
query: string
|
|
26
|
+
active: boolean
|
|
27
|
+
index: number
|
|
28
|
+
id: string
|
|
29
|
+
t: (key: string) => string
|
|
30
|
+
onPick: () => void
|
|
31
|
+
onHover: () => void
|
|
32
|
+
}): ReactNode {
|
|
33
|
+
const { entry } = props
|
|
34
|
+
// 高亮的位置来自 quickopen 的 `matchPositions`:与过滤用的是同一套贪心匹配,
|
|
35
|
+
// 所以「为什么这行算命中」和「高亮哪几个字符」永远一致。
|
|
36
|
+
const positions = useMemo(() => new Set(matchPositions(props.query, entry.name)), [props.query, entry.name])
|
|
37
|
+
return (
|
|
38
|
+
<div
|
|
39
|
+
className={props.active ? 'rvs-outline-row active' : 'rvs-outline-row'}
|
|
40
|
+
role="option"
|
|
41
|
+
aria-selected={props.active}
|
|
42
|
+
id={props.id}
|
|
43
|
+
data-outline-index={props.index}
|
|
44
|
+
data-outline-depth={entry.depth}
|
|
45
|
+
data-outline-kind={entry.kind}
|
|
46
|
+
// mousedown 而不是 click:列表不能在跳转前先丢焦点,而重排后的 click 可能
|
|
47
|
+
// 落到用户没指的那一行上。
|
|
48
|
+
onMouseDown={(event) => {
|
|
49
|
+
event.preventDefault()
|
|
50
|
+
props.onPick()
|
|
51
|
+
}}
|
|
52
|
+
onMouseEnter={props.onHover}
|
|
53
|
+
>
|
|
54
|
+
<span className="rvs-outline-kind" data-kind={entry.kind}>{props.t(`outline.kind.${entry.kind}`)}</span>
|
|
55
|
+
<span
|
|
56
|
+
className="rvs-outline-name"
|
|
57
|
+
style={{ paddingLeft: `${entry.depth * 14}px` }}
|
|
58
|
+
title={entry.container === undefined ? entry.name : `${entry.container} › ${entry.name}`}
|
|
59
|
+
>
|
|
60
|
+
{entry.name.split('').map((character, at) => (
|
|
61
|
+
positions.has(at)
|
|
62
|
+
? <mark key={at} className="rvs-outline-hit">{character}</mark>
|
|
63
|
+
: <span key={at}>{character}</span>
|
|
64
|
+
))}
|
|
65
|
+
</span>
|
|
66
|
+
<span className="rvs-outline-line">{entry.line}</span>
|
|
67
|
+
</div>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function OutlinePanel(props: {
|
|
72
|
+
t: (key: string) => string
|
|
73
|
+
/** 远端绝对路径(判断语言 + 标题)。 */
|
|
74
|
+
path: string
|
|
75
|
+
/** 实时缓冲区全文:dirty 时必须来自编辑器,不是 `tab.text`。 */
|
|
76
|
+
text: string
|
|
77
|
+
onPick: (entry: OutlineEntry) => void
|
|
78
|
+
onClose: () => void
|
|
79
|
+
}): ReactNode {
|
|
80
|
+
// Two editor seats can keep an outline open at once; DOM ids must not collide.
|
|
81
|
+
const listId = useId()
|
|
82
|
+
const optionId = (index: number): string => `${listId}-option-${index}`
|
|
83
|
+
const t = props.t
|
|
84
|
+
const outline = useMemo(() => buildOutline(props.text, props.path), [props.text, props.path])
|
|
85
|
+
const [query, setQuery] = useState('')
|
|
86
|
+
const [active, setActive] = useState(0)
|
|
87
|
+
const inputRef = useRef<HTMLInputElement>(null)
|
|
88
|
+
const listRef = useRef<HTMLDivElement>(null)
|
|
89
|
+
const boxRef = useRef<HTMLDivElement>(null)
|
|
90
|
+
|
|
91
|
+
/** 最新的关闭回调:window 监听只装一次,不能跟着每次渲染重装。 */
|
|
92
|
+
const closeRef = useRef(props.onClose)
|
|
93
|
+
closeRef.current = props.onClose
|
|
94
|
+
|
|
95
|
+
/** 关闭时把焦点还给打开它的那个元素(portal 自身不会恢复)。 */
|
|
96
|
+
const openerRef = useRef<HTMLElement | null>(
|
|
97
|
+
typeof document === 'undefined' ? null : (document.activeElement as HTMLElement | null),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
inputRef.current?.focus()
|
|
102
|
+
return () => {
|
|
103
|
+
const opener = openerRef.current
|
|
104
|
+
if (opener !== null && document.body.contains(opener)) opener.focus()
|
|
105
|
+
}
|
|
106
|
+
}, [])
|
|
107
|
+
|
|
108
|
+
const ranked = useMemo(() => rankOutline(query, outline.entries, OUTLINE_RESULT_LIMIT), [query, outline])
|
|
109
|
+
const count = ranked.hits.length
|
|
110
|
+
const current = count === 0 ? -1 : Math.min(active, count - 1)
|
|
111
|
+
|
|
112
|
+
// 新查询从第一行开始,绝不停在上一轮的旧行上。
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
setActive(0)
|
|
115
|
+
}, [query])
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (current < 0) return
|
|
119
|
+
listRef.current?.querySelector<HTMLElement>(`[data-outline-index="${current}"]`)
|
|
120
|
+
?.scrollIntoView?.({ block: 'nearest' })
|
|
121
|
+
}, [current])
|
|
122
|
+
|
|
123
|
+
const pick = (entry: OutlineEntry): void => {
|
|
124
|
+
props.onPick(entry)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {
|
|
128
|
+
// 输入法候选窗用 Enter/方向键选字:此时按 Enter 就跳走等于打断中文输入。
|
|
129
|
+
if (event.nativeEvent.isComposing === true || event.keyCode === 229) return
|
|
130
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
131
|
+
event.preventDefault()
|
|
132
|
+
if (count === 0) return
|
|
133
|
+
setActive((previous) => {
|
|
134
|
+
const at = Math.min(previous, count - 1)
|
|
135
|
+
if (event.key === 'ArrowDown') return (at + 1) % count
|
|
136
|
+
return at <= 0 ? count - 1 : at - 1
|
|
137
|
+
})
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
if (event.key === 'Enter') {
|
|
141
|
+
event.preventDefault()
|
|
142
|
+
const target = current >= 0 ? ranked.hits[current] : undefined
|
|
143
|
+
if (target !== undefined) pick(target)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Tab 不许离开浮层:列表行不是焦点元素,Tab 会落到后面的页面元素上。 */
|
|
148
|
+
const onDialogKeyDown = (event: React.KeyboardEvent<HTMLDivElement>): void => {
|
|
149
|
+
if (event.key !== 'Tab') return
|
|
150
|
+
const box = boxRef.current
|
|
151
|
+
if (box === null) return
|
|
152
|
+
const items = Array.from(box.querySelectorAll<HTMLElement>(
|
|
153
|
+
'input:not([disabled]), button:not([disabled]), [tabindex]:not([tabindex="-1"])',
|
|
154
|
+
))
|
|
155
|
+
if (items.length === 0) return
|
|
156
|
+
const first = items[0]
|
|
157
|
+
const last = items[items.length - 1]
|
|
158
|
+
const at = document.activeElement
|
|
159
|
+
const inside = at !== null && box.contains(at)
|
|
160
|
+
if (!inside) {
|
|
161
|
+
event.preventDefault()
|
|
162
|
+
first?.focus()
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
if (event.shiftKey && at === first) {
|
|
166
|
+
event.preventDefault()
|
|
167
|
+
last?.focus()
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
if (!event.shiftKey && at === last) {
|
|
171
|
+
event.preventDefault()
|
|
172
|
+
first?.focus()
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
const onKey = (event: KeyboardEvent): void => {
|
|
178
|
+
if (event.key !== 'Escape') return
|
|
179
|
+
event.preventDefault()
|
|
180
|
+
event.stopPropagation()
|
|
181
|
+
closeRef.current()
|
|
182
|
+
}
|
|
183
|
+
window.addEventListener('keydown', onKey, true)
|
|
184
|
+
return () => window.removeEventListener('keydown', onKey, true)
|
|
185
|
+
}, [])
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 状态行。
|
|
189
|
+
*
|
|
190
|
+
* 「猜的大纲」与「被截断」是两条独立的常驻警告:它们说的是「这份列表不完整」,
|
|
191
|
+
* 不能被「没有匹配」替换掉 —— 那个是查询的结果,不是数据的性质。
|
|
192
|
+
*/
|
|
193
|
+
const foot = (): ReactNode => {
|
|
194
|
+
if (count === 0) {
|
|
195
|
+
return (
|
|
196
|
+
<span className="rvs-outline-note" role="status">
|
|
197
|
+
{outline.entries.length === 0 ? t('outline.empty') : t('outline.noResults')}
|
|
198
|
+
</span>
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
return (
|
|
202
|
+
<span className="rvs-outline-note">
|
|
203
|
+
{ranked.truncated
|
|
204
|
+
? t('outline.more').replace('{count}', String(count)).replace('{total}', String(outline.total))
|
|
205
|
+
: `${count} / ${outline.total}`}
|
|
206
|
+
</span>
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (typeof document === 'undefined') return null
|
|
211
|
+
return createPortal(
|
|
212
|
+
<div className="rvs-portal">
|
|
213
|
+
<div
|
|
214
|
+
className="rvs-outline-mask"
|
|
215
|
+
onMouseDown={(event) => { if (event.target === event.currentTarget) props.onClose() }}
|
|
216
|
+
>
|
|
217
|
+
<div
|
|
218
|
+
className="rvs-outline"
|
|
219
|
+
role="dialog"
|
|
220
|
+
aria-modal="true"
|
|
221
|
+
aria-label={t('outline.title')}
|
|
222
|
+
ref={boxRef}
|
|
223
|
+
onKeyDown={onDialogKeyDown}
|
|
224
|
+
>
|
|
225
|
+
<div className="rvs-outline-head">
|
|
226
|
+
<span className="rvs-outline-title">{t('outline.title')}</span>
|
|
227
|
+
<span className="rvs-outline-path" title={props.path}>{props.path}</span>
|
|
228
|
+
<button
|
|
229
|
+
type="button"
|
|
230
|
+
className="rvs-outline-close"
|
|
231
|
+
title={t('common.close')}
|
|
232
|
+
aria-label={t('common.close')}
|
|
233
|
+
onClick={props.onClose}
|
|
234
|
+
>
|
|
235
|
+
×
|
|
236
|
+
</button>
|
|
237
|
+
</div>
|
|
238
|
+
<div className="rvs-outline-bar">
|
|
239
|
+
<input
|
|
240
|
+
ref={inputRef}
|
|
241
|
+
className="rvs-outline-input"
|
|
242
|
+
type="text"
|
|
243
|
+
value={query}
|
|
244
|
+
placeholder={t('outline.placeholder')}
|
|
245
|
+
spellCheck={false}
|
|
246
|
+
autoComplete="off"
|
|
247
|
+
role="combobox"
|
|
248
|
+
aria-expanded
|
|
249
|
+
aria-controls={listId}
|
|
250
|
+
aria-activedescendant={current >= 0 ? optionId(current) : undefined}
|
|
251
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
252
|
+
onKeyDown={onInputKeyDown}
|
|
253
|
+
/>
|
|
254
|
+
<span className="rvs-outline-hint">{t('outline.hint')}</span>
|
|
255
|
+
</div>
|
|
256
|
+
<div className="rvs-outline-list" id={listId} role="listbox" ref={listRef}>
|
|
257
|
+
{ranked.hits.map((entry, index) => (
|
|
258
|
+
<Row
|
|
259
|
+
key={`${entry.line}:${entry.name}:${index}`}
|
|
260
|
+
entry={entry}
|
|
261
|
+
query={query}
|
|
262
|
+
active={index === current}
|
|
263
|
+
index={index}
|
|
264
|
+
id={optionId(index)}
|
|
265
|
+
t={t}
|
|
266
|
+
onPick={() => pick(entry)}
|
|
267
|
+
onHover={() => setActive(index)}
|
|
268
|
+
/>
|
|
269
|
+
))}
|
|
270
|
+
</div>
|
|
271
|
+
<div className="rvs-outline-foot">
|
|
272
|
+
{foot()}
|
|
273
|
+
{outline.guessed && (
|
|
274
|
+
<span className="rvs-outline-note warn" role="status">{t('outline.guessed')}</span>
|
|
275
|
+
)}
|
|
276
|
+
{outline.truncated && (
|
|
277
|
+
<span className="rvs-outline-note warn" role="status">
|
|
278
|
+
{t('outline.truncated').replace('{count}', String(outline.entries.length))}
|
|
279
|
+
</span>
|
|
280
|
+
)}
|
|
281
|
+
</div>
|
|
282
|
+
</div>
|
|
283
|
+
</div>
|
|
284
|
+
</div>,
|
|
285
|
+
document.body,
|
|
286
|
+
)
|
|
287
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote process table shown on the monitor page.
|
|
3
|
+
*
|
|
4
|
+
* One bounded snapshot per selected host, polled only while the monitor page and
|
|
5
|
+
* the browser tab are both visible. Killing is always confirmed and always goes
|
|
6
|
+
* through the Host route (which validates the PID against its own snapshot), so
|
|
7
|
+
* the browser never builds a shell command from a process row.
|
|
8
|
+
*/
|
|
9
|
+
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
|
|
10
|
+
import type { RemoteHostSummary, RemoteProcess, RemoteProcessesResult } from '../protocol.ts'
|
|
11
|
+
import { api, RemoteApiError } from './api.ts'
|
|
12
|
+
import { IconButton, IconRefresh, MiniSpinner, confirmDialog, toast } from './vsc/kit.tsx'
|
|
13
|
+
|
|
14
|
+
export const PROCESS_POLL_MS = 5_000
|
|
15
|
+
|
|
16
|
+
type SortKey = 'cpu' | 'mem' | 'pid'
|
|
17
|
+
|
|
18
|
+
function formatElapsed(seconds: number | null, t: (key: string) => string): string {
|
|
19
|
+
if (seconds === null || !Number.isFinite(seconds) || seconds < 0) return t('proc.unknown')
|
|
20
|
+
const days = Math.floor(seconds / 86_400)
|
|
21
|
+
const hours = Math.floor((seconds % 86_400) / 3_600)
|
|
22
|
+
const minutes = Math.floor((seconds % 3_600) / 60)
|
|
23
|
+
if (days > 0) return `${days}d ${hours}h`
|
|
24
|
+
if (hours > 0) return `${hours}h ${minutes}m`
|
|
25
|
+
if (minutes > 0) return `${minutes}m`
|
|
26
|
+
return `${Math.max(1, Math.floor(seconds))}s`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function percent(value: number): string {
|
|
30
|
+
return `${value.toFixed(1)}%`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sortRows(rows: readonly RemoteProcess[], key: SortKey): RemoteProcess[] {
|
|
34
|
+
const copy = [...rows]
|
|
35
|
+
if (key === 'pid') copy.sort((left, right) => left.pid - right.pid)
|
|
36
|
+
else {
|
|
37
|
+
const field = key === 'cpu' ? 'cpuPercent' : 'memPercent'
|
|
38
|
+
copy.sort((left, right) => right[field] - left[field] || left.pid - right.pid)
|
|
39
|
+
}
|
|
40
|
+
return copy
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function ProcessesSection(props: {
|
|
44
|
+
t: (key: string) => string
|
|
45
|
+
hosts: RemoteHostSummary[]
|
|
46
|
+
aliases: string[]
|
|
47
|
+
visible: boolean
|
|
48
|
+
paused: boolean
|
|
49
|
+
intervalMs: number
|
|
50
|
+
}): ReactNode {
|
|
51
|
+
const t = props.t
|
|
52
|
+
const [alias, setAlias] = useState(props.aliases[0] ?? '')
|
|
53
|
+
const [expanded, setExpanded] = useState(false)
|
|
54
|
+
const [result, setResult] = useState<RemoteProcessesResult | undefined>(undefined)
|
|
55
|
+
const [loading, setLoading] = useState(false)
|
|
56
|
+
const [error, setError] = useState<string | undefined>(undefined)
|
|
57
|
+
const [filter, setFilter] = useState('')
|
|
58
|
+
const [sortKey, setSortKey] = useState<SortKey>('cpu')
|
|
59
|
+
const [killing, setKilling] = useState<number | undefined>(undefined)
|
|
60
|
+
const [forcePid, setForcePid] = useState<number | undefined>(undefined)
|
|
61
|
+
const inFlight = useRef<Set<string>>(new Set())
|
|
62
|
+
const aliasRef = useRef(alias)
|
|
63
|
+
aliasRef.current = alias
|
|
64
|
+
const alive = useRef(true)
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
alive.current = true
|
|
68
|
+
return () => { alive.current = false }
|
|
69
|
+
}, [])
|
|
70
|
+
|
|
71
|
+
// The scope can change from "all hosts" to one host, or disappear entirely.
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (props.aliases.includes(alias)) return
|
|
74
|
+
setAlias(props.aliases[0] ?? '')
|
|
75
|
+
setResult(undefined)
|
|
76
|
+
setError(undefined)
|
|
77
|
+
}, [props.aliases, alias])
|
|
78
|
+
|
|
79
|
+
const load = useCallback(async (force = false, manual = false): Promise<void> => {
|
|
80
|
+
const requested = alias
|
|
81
|
+
if (requested === '' || inFlight.current.has(requested)) return
|
|
82
|
+
inFlight.current.add(requested)
|
|
83
|
+
if (manual) setLoading(true)
|
|
84
|
+
try {
|
|
85
|
+
const next = await api.processes(requested, force)
|
|
86
|
+
// The user may have switched hosts while this request was in flight: an
|
|
87
|
+
// old host's table must never paint under (or be killed from) the new one.
|
|
88
|
+
if (!alive.current || requested !== aliasRef.current) return
|
|
89
|
+
setResult(next)
|
|
90
|
+
setError(next.error)
|
|
91
|
+
} catch (caught) {
|
|
92
|
+
if (!alive.current) return
|
|
93
|
+
setError(
|
|
94
|
+
caught instanceof RemoteApiError && caught.status === 404
|
|
95
|
+
? t('proc.hostTooOld')
|
|
96
|
+
: caught instanceof RemoteApiError ? caught.message : String(caught),
|
|
97
|
+
)
|
|
98
|
+
} finally {
|
|
99
|
+
inFlight.current.delete(requested)
|
|
100
|
+
if (manual && alive.current && requested === aliasRef.current) setLoading(false)
|
|
101
|
+
}
|
|
102
|
+
}, [alias])
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (!expanded || !props.visible || props.paused || alias === '') return
|
|
106
|
+
const tick = (): void => {
|
|
107
|
+
if (document.hidden) return
|
|
108
|
+
void load(false, false)
|
|
109
|
+
}
|
|
110
|
+
tick()
|
|
111
|
+
const timer = window.setInterval(tick, Math.max(2_000, props.intervalMs || PROCESS_POLL_MS))
|
|
112
|
+
return () => window.clearInterval(timer)
|
|
113
|
+
}, [expanded, props.visible, props.paused, alias, load, props.intervalMs])
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
if (!expanded || !props.visible || props.paused) return
|
|
117
|
+
const onVisible = (): void => {
|
|
118
|
+
if (!document.hidden) void load(false, false)
|
|
119
|
+
}
|
|
120
|
+
document.addEventListener('visibilitychange', onVisible)
|
|
121
|
+
return () => document.removeEventListener('visibilitychange', onVisible)
|
|
122
|
+
}, [expanded, props.visible, props.paused, load])
|
|
123
|
+
|
|
124
|
+
const rows = useMemo(() => {
|
|
125
|
+
const needle = filter.trim().toLowerCase()
|
|
126
|
+
const source = result?.processes ?? []
|
|
127
|
+
const filtered = needle === ''
|
|
128
|
+
? source
|
|
129
|
+
: source.filter(row => row.command.toLowerCase().includes(needle)
|
|
130
|
+
|| row.user.toLowerCase().includes(needle)
|
|
131
|
+
|| String(row.pid).includes(needle))
|
|
132
|
+
return sortRows(filtered, sortKey)
|
|
133
|
+
}, [result, filter, sortKey])
|
|
134
|
+
|
|
135
|
+
const kill = async (row: RemoteProcess): Promise<void> => {
|
|
136
|
+
const ok = await confirmDialog({
|
|
137
|
+
title: t('proc.killTitle'),
|
|
138
|
+
text: t('proc.killText')
|
|
139
|
+
.replace('{host}', alias)
|
|
140
|
+
.replace('{pid}', String(row.pid))
|
|
141
|
+
.replace('{command}', row.command.slice(0, 120)),
|
|
142
|
+
danger: true,
|
|
143
|
+
okText: t('proc.kill'),
|
|
144
|
+
cancelText: t('common.cancel'),
|
|
145
|
+
})
|
|
146
|
+
if (!ok) return
|
|
147
|
+
setKilling(row.pid)
|
|
148
|
+
try {
|
|
149
|
+
await api.killProcess(alias, row.pid, 'TERM')
|
|
150
|
+
toast(t('proc.killSent').replace('{pid}', String(row.pid)))
|
|
151
|
+
await load(true, false)
|
|
152
|
+
} catch (caught) {
|
|
153
|
+
toast(caught instanceof RemoteApiError ? caught.message : String(caught), true)
|
|
154
|
+
// TERM may be ignored (or the file may still be flushing): offer KILL as
|
|
155
|
+
// a second, deliberately re-confirmed action instead of escalating
|
|
156
|
+
// silently.
|
|
157
|
+
setForcePid(row.pid)
|
|
158
|
+
} finally {
|
|
159
|
+
if (alive.current) setKilling(undefined)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const forceKill = async (row: RemoteProcess): Promise<void> => {
|
|
164
|
+
const ok = await confirmDialog({
|
|
165
|
+
title: t('proc.killForceTitle'),
|
|
166
|
+
text: t('proc.killForceText')
|
|
167
|
+
.replace('{host}', alias)
|
|
168
|
+
.replace('{pid}', String(row.pid))
|
|
169
|
+
.replace('{command}', row.command.slice(0, 120)),
|
|
170
|
+
danger: true,
|
|
171
|
+
okText: t('proc.killForce'),
|
|
172
|
+
cancelText: t('common.cancel'),
|
|
173
|
+
})
|
|
174
|
+
if (!ok) return
|
|
175
|
+
setKilling(row.pid)
|
|
176
|
+
try {
|
|
177
|
+
await api.killProcess(alias, row.pid, 'KILL')
|
|
178
|
+
toast(t('proc.killForceSent').replace('{pid}', String(row.pid)))
|
|
179
|
+
setForcePid(undefined)
|
|
180
|
+
await load(true, false)
|
|
181
|
+
} catch (caught) {
|
|
182
|
+
toast(caught instanceof RemoteApiError ? caught.message : String(caught), true)
|
|
183
|
+
} finally {
|
|
184
|
+
if (alive.current) setKilling(undefined)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const host = props.hosts.find(item => item.alias === alias)
|
|
189
|
+
const toolUnknown = result !== undefined && result.tool === 'unknown'
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<section className={expanded ? 'rvs-proc expanded' : 'rvs-proc'} aria-label={t('proc.title')}>
|
|
193
|
+
<div className="rvs-proc-head">
|
|
194
|
+
<button
|
|
195
|
+
type="button"
|
|
196
|
+
className="rvs-proc-toggle"
|
|
197
|
+
aria-expanded={expanded}
|
|
198
|
+
onClick={() => { setExpanded(value => !value) }}
|
|
199
|
+
title={expanded ? t('proc.collapse') : t('proc.expand')}
|
|
200
|
+
>
|
|
201
|
+
<span aria-hidden="true">{expanded ? '▾' : '▸'}</span>
|
|
202
|
+
</button>
|
|
203
|
+
<span className="rvs-proc-title">{t('proc.title')}</span>
|
|
204
|
+
{props.aliases.length > 1 && (
|
|
205
|
+
<select
|
|
206
|
+
className="rvs-select rvs-proc-host"
|
|
207
|
+
value={alias}
|
|
208
|
+
onChange={(event) => { setAlias(event.target.value); setResult(undefined); setError(undefined) }}
|
|
209
|
+
aria-label={t('proc.host')}
|
|
210
|
+
>
|
|
211
|
+
{props.aliases.map(item => <option key={item} value={item}>{item}</option>)}
|
|
212
|
+
</select>
|
|
213
|
+
)}
|
|
214
|
+
{props.aliases.length === 1 && host !== undefined && (
|
|
215
|
+
<span className="rvs-proc-host-label">{host.alias}</span>
|
|
216
|
+
)}
|
|
217
|
+
{expanded && (
|
|
218
|
+
<>
|
|
219
|
+
<input
|
|
220
|
+
className="rvs-input rvs-proc-filter"
|
|
221
|
+
value={filter}
|
|
222
|
+
onChange={(event) => { setFilter(event.target.value) }}
|
|
223
|
+
placeholder={t('proc.filter')}
|
|
224
|
+
aria-label={t('proc.filter')}
|
|
225
|
+
/>
|
|
226
|
+
<select
|
|
227
|
+
className="rvs-select rvs-proc-sort"
|
|
228
|
+
value={sortKey}
|
|
229
|
+
onChange={(event) => { setSortKey(event.target.value as SortKey) }}
|
|
230
|
+
aria-label={t('proc.sort')}
|
|
231
|
+
>
|
|
232
|
+
<option value="cpu">{t('proc.sortCpu')}</option>
|
|
233
|
+
<option value="mem">{t('proc.sortMem')}</option>
|
|
234
|
+
<option value="pid">{t('proc.sortPid')}</option>
|
|
235
|
+
</select>
|
|
236
|
+
<IconButton title={t('proc.refresh')} onClick={() => { void load(true, true) }} disabled={loading || alias === ''}>
|
|
237
|
+
{loading ? <MiniSpinner /> : <IconRefresh size={13} />}
|
|
238
|
+
</IconButton>
|
|
239
|
+
</>
|
|
240
|
+
)}
|
|
241
|
+
</div>
|
|
242
|
+
{!expanded && <div className="rvs-proc-collapsed">{t('proc.collapsedHint')}</div>}
|
|
243
|
+
{expanded && error !== undefined && <div className="rvs-banner warn rvs-proc-banner">{toolUnknown ? t('proc.cannotEnumerate') : error}</div>}
|
|
244
|
+
{expanded && result?.tool === 'none' && <div className="rvs-banner warn rvs-proc-banner">{t('proc.noPs')}</div>}
|
|
245
|
+
{expanded && result?.truncated === true && (
|
|
246
|
+
<div className="rvs-banner info rvs-proc-banner">{t('proc.truncated').replace('{count}', String(result.processes.length))}</div>
|
|
247
|
+
)}
|
|
248
|
+
{expanded && alias === '' && <div className="rvs-empty">{t('proc.noHost')}</div>}
|
|
249
|
+
{expanded && alias !== '' && rows.length === 0 && !loading && error === undefined
|
|
250
|
+
&& result !== undefined && result.tool !== 'none' && result.tool !== 'unknown' && (
|
|
251
|
+
<div className="rvs-empty">{t('proc.empty')}</div>
|
|
252
|
+
)}
|
|
253
|
+
{expanded && rows.length > 0 && (
|
|
254
|
+
<div className="rvs-proc-table rvs-scroll" role="table" aria-label={t('proc.title')} aria-live="polite">
|
|
255
|
+
<div className="rvs-proc-row rvs-proc-row-head" role="row">
|
|
256
|
+
<span role="columnheader">PID</span>
|
|
257
|
+
<span role="columnheader">PPID</span>
|
|
258
|
+
<span role="columnheader">USER</span>
|
|
259
|
+
<span role="columnheader" title={t('proc.cpuHint')}>CPU</span>
|
|
260
|
+
<span role="columnheader">MEM</span>
|
|
261
|
+
<span role="columnheader">{t('proc.elapsed')}</span>
|
|
262
|
+
<span role="columnheader">{t('proc.command')}</span>
|
|
263
|
+
<span aria-hidden="true" />
|
|
264
|
+
</div>
|
|
265
|
+
{rows.map(row => (
|
|
266
|
+
<div className="rvs-proc-row" role="row" key={row.pid}>
|
|
267
|
+
<span role="cell" className="rvs-proc-pid">{row.pid}</span>
|
|
268
|
+
<span role="cell" className="rvs-proc-pid">{row.ppid === 0 ? '—' : row.ppid}</span>
|
|
269
|
+
<span role="cell" className="rvs-proc-user" title={row.user}>{row.user}</span>
|
|
270
|
+
<span role="cell" className="rvs-proc-num">{percent(row.cpuPercent)}</span>
|
|
271
|
+
<span role="cell" className="rvs-proc-num">{percent(row.memPercent)}</span>
|
|
272
|
+
<span role="cell" className="rvs-proc-elapsed">{formatElapsed(row.elapsedSeconds, t)}</span>
|
|
273
|
+
<span role="cell" className="rvs-proc-command" title={row.command}>{row.command}</span>
|
|
274
|
+
<span role="cell">
|
|
275
|
+
<button
|
|
276
|
+
type="button"
|
|
277
|
+
className={forcePid === row.pid ? 'rvs-proc-kill danger' : 'rvs-proc-kill'}
|
|
278
|
+
title={forcePid === row.pid ? t('proc.killForceTitle') : t('proc.killTitle')}
|
|
279
|
+
aria-label={`${forcePid === row.pid ? t('proc.killForceTitle') : t('proc.killTitle')} PID ${row.pid}`}
|
|
280
|
+
disabled={killing === row.pid}
|
|
281
|
+
onClick={() => { void (forcePid === row.pid ? forceKill(row) : kill(row)) }}
|
|
282
|
+
>
|
|
283
|
+
{killing === row.pid ? <MiniSpinner /> : (forcePid === row.pid ? t('proc.killForce') : t('proc.kill'))}
|
|
284
|
+
</button>
|
|
285
|
+
</span>
|
|
286
|
+
</div>
|
|
287
|
+
))}
|
|
288
|
+
</div>
|
|
289
|
+
)}
|
|
290
|
+
</section>
|
|
291
|
+
)
|
|
292
|
+
}
|