dsh-remote-vs 0.12.0 → 0.13.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 +2 -1
- package/README.md +2 -1
- package/lib/client.js +199 -14
- package/lib/index.js +299 -11
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/listing-requests.d.ts +45 -0
- package/lib/types/pool.d.ts +106 -0
- package/lib/types/sftp.d.ts +35 -0
- package/lib/{worlds-oj65Hic-.js → worlds-C6Xha1mQ.js} +413 -90
- package/package.json +1 -1
- package/src/client/DownloadDock.tsx +6 -2
- package/src/client/RemoteExplorer.tsx +137 -12
- package/src/client/downloads.ts +5 -1
- package/src/client/i18n.ts +6 -1
- package/src/client/listing-requests.ts +84 -0
- package/src/client/vsc/kit.tsx +30 -3
- package/src/pool.ts +377 -17
- package/src/sftp.ts +502 -108
|
@@ -16,6 +16,7 @@ import { MAX_SELECTION_DOWNLOAD, type RemoteFileEntry } from '../protocol.ts'
|
|
|
16
16
|
import { api, uploadFile } from './api.ts'
|
|
17
17
|
import { startDownload, startSelectionDownload } from './downloads.ts'
|
|
18
18
|
import { insertRemoteReference, sameLocalDir } from './composer.ts'
|
|
19
|
+
import { ListingRequests } from './listing-requests.ts'
|
|
19
20
|
import { repoRelative, useExplorer } from './store.tsx'
|
|
20
21
|
import { planUpload, withDroppedDirs, type DroppedFile } from './upload-plan.ts'
|
|
21
22
|
import { collectDroppedFiles, type EntryLike } from './upload-source.ts'
|
|
@@ -147,6 +148,13 @@ function formatUploadLabel(
|
|
|
147
148
|
/** How many directory listings stay cached; each one is a whole directory. */
|
|
148
149
|
const LISTING_CACHE_KEEP = 50
|
|
149
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Hover intent before a directory's listing is prefetched: long enough that
|
|
153
|
+
* sweeping the pointer across the tree does not fire a request per row, short
|
|
154
|
+
* enough that arriving at a folder and clicking it finds the listing warm.
|
|
155
|
+
*/
|
|
156
|
+
const PREFETCH_HOVER_MS = 150
|
|
157
|
+
|
|
150
158
|
/**
|
|
151
159
|
* Insert one listing as the newest, so "the first key is the oldest" stays
|
|
152
160
|
* true and `trimListings` can evict from the front.
|
|
@@ -196,6 +204,16 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
196
204
|
const { t, alias, root, repoRoot, marks } = store
|
|
197
205
|
const ctx = store.ctx
|
|
198
206
|
const [listings, setListings] = useState<Record<string, RemoteFileEntry[]>>({})
|
|
207
|
+
/**
|
|
208
|
+
* The same listings, readable synchronously. `prefetchDir` has to answer "is
|
|
209
|
+
* this already cached?" at pointer time, and a state value would be one render
|
|
210
|
+
* behind (asking again for a directory that was just listed).
|
|
211
|
+
*/
|
|
212
|
+
const listingsRef = useRef<Record<string, RemoteFileEntry[]>>({})
|
|
213
|
+
/** Set false on unmount so a listing that lands late does not touch state. */
|
|
214
|
+
const alive = useRef(true)
|
|
215
|
+
/** Hover-intent timer for prefetch; one pointer means one candidate. */
|
|
216
|
+
const prefetchTimer = useRef<number | undefined>(undefined)
|
|
199
217
|
const [loading, setLoading] = useState<Set<string>>(new Set())
|
|
200
218
|
const [expanded, setExpanded] = useState<Set<string>>(new Set())
|
|
201
219
|
const [selected, setSelected] = useState<Set<string>>(new Set())
|
|
@@ -238,23 +256,56 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
238
256
|
&& viewedAliasPath !== undefined
|
|
239
257
|
&& sameLocalDir(chatTarget.sessionWorkspacePath, viewedAliasPath)
|
|
240
258
|
|
|
259
|
+
/**
|
|
260
|
+
* The revision this component's listing traffic is labelled with.
|
|
261
|
+
*
|
|
262
|
+
* A ref, not a hook dependency: a revision bump must not re-create `loadDir`
|
|
263
|
+
* (the reveal/refresh effects list it, and their identity churn would re-run
|
|
264
|
+
* both). Only the request-time check in `ListingRequests` reads it. It is
|
|
265
|
+
* written during render on purpose (an idempotent latest-value ref): an
|
|
266
|
+
* effect-synced ref is one commit stale, so a read triggered from the same
|
|
267
|
+
* commit as the write could still be labelled with the previous revision.
|
|
268
|
+
*/
|
|
269
|
+
const revisionRef = useRef(store.revision)
|
|
270
|
+
revisionRef.current = store.revision
|
|
271
|
+
/** Current binding, for the late-answer guard below (the memo is per alias). */
|
|
272
|
+
const aliasRef = useRef(alias)
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The in-flight listing reads (see `ListingRequests`).
|
|
276
|
+
*
|
|
277
|
+
* A hover prefetch and the click that follows it are the same request, and so
|
|
278
|
+
* are a double click and the reveal chain's level: without the shared map each
|
|
279
|
+
* one opened its own SFTP listing for the same path. A request is only shared
|
|
280
|
+
* when it started at the same revision, so a read AFTER a write never gets an
|
|
281
|
+
* answer issued before it.
|
|
282
|
+
*/
|
|
283
|
+
const listingsApi = useMemo(() => new ListingRequests(
|
|
284
|
+
dir => dirKey(alias, dir),
|
|
285
|
+
dir => api.fileOp.list(alias, dir),
|
|
286
|
+
(key, entries) => {
|
|
287
|
+
// A late answer must not paint after unmount, nor write the PREVIOUS
|
|
288
|
+
// workspace's cache key after a binding change.
|
|
289
|
+
if (alive.current && aliasRef.current === alias) setListings(prev => withListing(prev, key, entries))
|
|
290
|
+
},
|
|
291
|
+
), [alias])
|
|
292
|
+
|
|
241
293
|
const loadDir = useCallback(async (dir: string, force = false): Promise<void> => {
|
|
242
294
|
if (alias === '') return
|
|
243
295
|
const key = dirKey(alias, dir)
|
|
244
|
-
// NOTE: a cached listing is deliberately NOT reused
|
|
245
|
-
//
|
|
246
|
-
//
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
|
|
296
|
+
// NOTE: a cached listing is deliberately NOT reused as the only source. The
|
|
297
|
+
// tree relies on every caller re-listing after a write (`force` = start a
|
|
298
|
+
// fresh request instead of joining one that may predate the write); a
|
|
299
|
+
// PREFETCHED listing is still on screen while the revalidation runs, which
|
|
300
|
+
// is what makes the open feel instant without serving stale content as
|
|
301
|
+
// final.
|
|
250
302
|
setLoading(prev => {
|
|
251
303
|
const next = new Set(prev)
|
|
252
304
|
next.add(key)
|
|
253
305
|
return next
|
|
254
306
|
})
|
|
255
307
|
try {
|
|
256
|
-
|
|
257
|
-
setListings(prev => withListing(prev, key, entries))
|
|
308
|
+
await listingsApi.request(dir, revisionRef.current, force)
|
|
258
309
|
} catch (error) {
|
|
259
310
|
toast(error instanceof Error ? error.message : String(error), true)
|
|
260
311
|
} finally {
|
|
@@ -264,7 +315,37 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
264
315
|
return next
|
|
265
316
|
})
|
|
266
317
|
}
|
|
267
|
-
}, [alias])
|
|
318
|
+
}, [alias, listingsApi])
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Warm the listing cache for a directory the pointer is resting on.
|
|
322
|
+
*
|
|
323
|
+
* The open itself still revalidates through `loadDir` (the explorer must show
|
|
324
|
+
* writes made elsewhere), but with the rows already in memory the expand
|
|
325
|
+
* paints immediately instead of showing a loading line for one round trip.
|
|
326
|
+
* Speculative by nature, so a failure is not a toast — the real listing, when
|
|
327
|
+
* the user opens it, still explains itself.
|
|
328
|
+
*/
|
|
329
|
+
const prefetchDir = useCallback((dir: string): void => {
|
|
330
|
+
if (alias === '') return
|
|
331
|
+
const key = dirKey(alias, dir)
|
|
332
|
+
if (listingsRef.current[key] !== undefined) return
|
|
333
|
+
if (listingsApi.busy(dir)) return
|
|
334
|
+
listingsApi.request(dir, revisionRef.current).catch(() => undefined)
|
|
335
|
+
}, [alias, listingsApi])
|
|
336
|
+
|
|
337
|
+
/** Hover intent: prefetch the row under the pointer after a short rest. */
|
|
338
|
+
const hoverDir = (path: string | undefined): void => {
|
|
339
|
+
if (prefetchTimer.current !== undefined) {
|
|
340
|
+
window.clearTimeout(prefetchTimer.current)
|
|
341
|
+
prefetchTimer.current = undefined
|
|
342
|
+
}
|
|
343
|
+
if (path === undefined) return
|
|
344
|
+
prefetchTimer.current = window.setTimeout(() => {
|
|
345
|
+
prefetchTimer.current = undefined
|
|
346
|
+
prefetchDir(path)
|
|
347
|
+
}, PREFETCH_HOVER_MS)
|
|
348
|
+
}
|
|
268
349
|
|
|
269
350
|
/**
|
|
270
351
|
* "Show this folder in the tree" — raised by a search result's menu.
|
|
@@ -313,6 +394,10 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
313
394
|
|
|
314
395
|
// Root listing + refresh on the shared revision bump (git writes, saves...).
|
|
315
396
|
const revision = store.revision
|
|
397
|
+
/** Keep the late-answer guard current for the NEXT commit's callbacks. */
|
|
398
|
+
useEffect(() => {
|
|
399
|
+
aliasRef.current = alias
|
|
400
|
+
}, [alias])
|
|
316
401
|
/**
|
|
317
402
|
* The binding this tree was listed from and the revision it was listed at.
|
|
318
403
|
* A parked panel stays MOUNTED behind `display:none`, so a write made off
|
|
@@ -337,7 +422,8 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
337
422
|
if (rebind) setExpanded(new Set([root]))
|
|
338
423
|
// Root plus every open directory, once each: `expanded` always holds the
|
|
339
424
|
// root, and listing it separately on top of the loop listed it twice. They
|
|
340
|
-
// are fired concurrently
|
|
425
|
+
// are fired concurrently; Host side they share the alias's browse SFTP
|
|
426
|
+
// session, so a refresh of a deep tree no longer opens one channel per level.
|
|
341
427
|
for (const dir of new Set(rebind ? [root] : [root, ...expanded])) void loadDir(dir, true)
|
|
342
428
|
// `expanded` is deliberately not a dependency: opening a directory lists it
|
|
343
429
|
// itself, so re-listing the whole tree per expand would put the bug back.
|
|
@@ -358,6 +444,23 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
358
444
|
setListings(prev => trimListings(prev, keep))
|
|
359
445
|
}, [expanded, alias, listings])
|
|
360
446
|
|
|
447
|
+
/** Keep the synchronous cache view in step (prefetch reads it at pointer time). */
|
|
448
|
+
useEffect(() => {
|
|
449
|
+
listingsRef.current = listings
|
|
450
|
+
}, [listings])
|
|
451
|
+
|
|
452
|
+
/** A hover prefetch / late listing must not fire or paint after the panel is gone. */
|
|
453
|
+
useEffect(() => {
|
|
454
|
+
// Re-armed on every effect run: a StrictMode/replay remount runs
|
|
455
|
+
// setup → cleanup → setup, and an `alive` that only ever got set to false
|
|
456
|
+
// would freeze every listing on the second pass.
|
|
457
|
+
alive.current = true
|
|
458
|
+
return () => {
|
|
459
|
+
alive.current = false
|
|
460
|
+
if (prefetchTimer.current !== undefined) window.clearTimeout(prefetchTimer.current)
|
|
461
|
+
}
|
|
462
|
+
}, [])
|
|
463
|
+
|
|
361
464
|
const rowsOf = useCallback((dir: string): RemoteFileEntry[] | undefined => {
|
|
362
465
|
const rows = listings[dirKey(alias, dir)]
|
|
363
466
|
if (rows === undefined) return undefined
|
|
@@ -1183,7 +1286,22 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
1183
1286
|
draggable
|
|
1184
1287
|
onClick={(e) => selectRow(entry, e)}
|
|
1185
1288
|
onDoubleClick={() => { if (!entry.isDir) store.openFile(entry.path) }}
|
|
1186
|
-
|
|
1289
|
+
// Hover-intent prefetch: a directory the pointer rests on gets its
|
|
1290
|
+
// listing warmed, so the click that follows paints instead of
|
|
1291
|
+
// waiting one round trip. Leave/collapse cancels the pending one.
|
|
1292
|
+
onMouseEnter={() => { if (entry.isDir) hoverDir(entry.path) }}
|
|
1293
|
+
onMouseLeave={() => hoverDir(undefined)}
|
|
1294
|
+
// Right-click opens on POINTERDOWN — the first event of the
|
|
1295
|
+
// gesture — so the menu is already mounted before any later
|
|
1296
|
+
// event of the same click (a platform may replay mousedown, and
|
|
1297
|
+
// a background git refresh may scroll) can close it. The
|
|
1298
|
+
// contextmenu fallback covers surfaces that only send that
|
|
1299
|
+
// (touch long-press).
|
|
1300
|
+
onPointerDown={(e) => { if (e.button === 2) openMenu(e, entry) }}
|
|
1301
|
+
onContextMenu={(e) => {
|
|
1302
|
+
e.preventDefault()
|
|
1303
|
+
if (menu.menu === undefined) openMenu(e, entry)
|
|
1304
|
+
}}
|
|
1187
1305
|
onDragStart={(e) => {
|
|
1188
1306
|
dragPaths.current = selected.has(entry.path) ? [...selected] : [entry.path]
|
|
1189
1307
|
e.dataTransfer.effectAllowed = 'move'
|
|
@@ -1389,7 +1507,14 @@ export function RemoteExplorer(props: { showHidden: boolean; onToggleHidden: ()
|
|
|
1389
1507
|
}}
|
|
1390
1508
|
onDrop={(e) => handleDrop(root, e)}
|
|
1391
1509
|
>
|
|
1392
|
-
<div
|
|
1510
|
+
<div
|
|
1511
|
+
className="rvs-tree"
|
|
1512
|
+
onPointerDown={(e) => { if (e.button === 2) openMenu(e, undefined) }}
|
|
1513
|
+
onContextMenu={(e) => {
|
|
1514
|
+
e.preventDefault()
|
|
1515
|
+
if (menu.menu === undefined) openMenu(e, undefined)
|
|
1516
|
+
}}
|
|
1517
|
+
>
|
|
1393
1518
|
<div className="rvs-empty" style={{ textAlign: 'left', padding: '2px 8px', direction: 'rtl', unicodeBidi: 'plaintext' }} title={rootLabel}>
|
|
1394
1519
|
<code style={{ fontSize: 11 }}>{rootLabel}</code>
|
|
1395
1520
|
</div>
|
package/src/client/downloads.ts
CHANGED
|
@@ -141,7 +141,11 @@ export function startDownload(alias: string, path: string, hint: { name: string;
|
|
|
141
141
|
const { id, controller } = newTask({ name: hint.name, directory: hint.directory })
|
|
142
142
|
launch(id, controller, async (options) => {
|
|
143
143
|
const entry = await api.fileOp.stat(alias, path)
|
|
144
|
-
|
|
144
|
+
// Seed the size from the stat, not from the HTTP response alone: the route
|
|
145
|
+
// sends `content-length`, but a shell or proxy in front of the page may
|
|
146
|
+
// strip it (DSH Desktop's forwarder deletes it), and then a plain file
|
|
147
|
+
// download would fall back to the archive wording ("packaging…").
|
|
148
|
+
patch(id, entry.isDir ? { directory: true } : { directory: false, total: entry.size })
|
|
145
149
|
return entry.isDir ? await saveRemoteDir(alias, path, options) : await saveRemoteFile(alias, path, options)
|
|
146
150
|
})
|
|
147
151
|
return id
|
package/src/client/i18n.ts
CHANGED
|
@@ -28,8 +28,11 @@ export const DYNAMIC_KEYS = [
|
|
|
28
28
|
'history.content',
|
|
29
29
|
'history.diff',
|
|
30
30
|
// Reached through the `STATUS_KEY` lookup in `DownloadDock.tsx` (running is
|
|
31
|
-
// the only status the detail line never prints as a literal `t()` call)
|
|
31
|
+
// the only status the detail line never prints as a literal `t()` call), and
|
|
32
|
+
// through the archive-vs-file ternary for the unknown-size note.
|
|
32
33
|
'downloads.running',
|
|
34
|
+
'downloads.indeterminate',
|
|
35
|
+
'downloads.sizeUnknown',
|
|
33
36
|
] as const
|
|
34
37
|
|
|
35
38
|
const zh: Record<string, string> = {
|
|
@@ -203,6 +206,7 @@ const zh: Record<string, string> = {
|
|
|
203
206
|
'downloads.close': '关闭',
|
|
204
207
|
'downloads.browserDefault': '已保存到浏览器默认下载目录',
|
|
205
208
|
'downloads.indeterminate': '正在打包…',
|
|
209
|
+
'downloads.sizeUnknown': '大小未知',
|
|
206
210
|
'downloads.tooMany': '选择项过多(最多 100 项),请改为下载所在目录',
|
|
207
211
|
'files.copied': '已复制',
|
|
208
212
|
'files.moved': '已移动',
|
|
@@ -630,6 +634,7 @@ const en: Record<string, string> = {
|
|
|
630
634
|
'downloads.close': 'Dismiss',
|
|
631
635
|
'downloads.browserDefault': 'Saved to the browser download folder',
|
|
632
636
|
'downloads.indeterminate': 'Packaging…',
|
|
637
|
+
'downloads.sizeUnknown': 'size unknown',
|
|
633
638
|
'downloads.tooMany': 'Too many entries (max 100); download the containing folder instead',
|
|
634
639
|
'files.copied': 'Copied',
|
|
635
640
|
'files.moved': 'Moved',
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-flight directory-listing requests, shared by the explorer's hover prefetch
|
|
3
|
+
* and the reads that follow it.
|
|
4
|
+
*
|
|
5
|
+
* Two rules the tree depends on:
|
|
6
|
+
* - JOIN only a request that started at the SAME revision. A listing read after
|
|
7
|
+
* a write must never be answered by a request issued before it, or pre-write
|
|
8
|
+
* rows paint as if they were final (a hover prefetch starts, a write lands,
|
|
9
|
+
* the click follows and joins the request that predates the write).
|
|
10
|
+
* - PAINT in request order. If a newer request supersedes an older one for the
|
|
11
|
+
* same directory, the older answer may still arrive; it must not overwrite the
|
|
12
|
+
* one the caller asked for.
|
|
13
|
+
*
|
|
14
|
+
* The cache itself stays in the component: this module only owns "which listing
|
|
15
|
+
* requests are on the wire and which answer may be stored".
|
|
16
|
+
*/
|
|
17
|
+
import type { RemoteFileEntry } from '../protocol.ts'
|
|
18
|
+
|
|
19
|
+
/** One in-flight listing request and the revision it was issued at. */
|
|
20
|
+
interface ListingRequest {
|
|
21
|
+
promise: Promise<RemoteFileEntry[]>
|
|
22
|
+
revision: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ListingRequests {
|
|
26
|
+
private readonly inFlight = new Map<string, ListingRequest>()
|
|
27
|
+
/** Newest request per key: only its answer may be stored. */
|
|
28
|
+
private readonly newest = new Map<string, number>()
|
|
29
|
+
private sequence = 0
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param keyOf - cache key for one directory (host alias + absolute path).
|
|
33
|
+
* @param load - performs the listing.
|
|
34
|
+
* @param store - applies a listing whose request is still the newest for its
|
|
35
|
+
* key; the component writes it into the listing cache.
|
|
36
|
+
*/
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly keyOf: (dir: string) => string,
|
|
39
|
+
private readonly load: (dir: string) => Promise<RemoteFileEntry[]>,
|
|
40
|
+
private readonly store: (key: string, entries: RemoteFileEntry[]) => void,
|
|
41
|
+
) {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* List one directory, or join the request a hover prefetch already started.
|
|
45
|
+
*
|
|
46
|
+
* @param dir - absolute remote directory.
|
|
47
|
+
* @param revision - the explorer revision this read is meant to reflect. A
|
|
48
|
+
* request issued at another revision is never joined.
|
|
49
|
+
* @param fresh - start a new request even when one is in flight (a
|
|
50
|
+
* revalidation after a write, where joining the older read would be wrong).
|
|
51
|
+
*/
|
|
52
|
+
request(dir: string, revision: number, fresh = false): Promise<RemoteFileEntry[]> {
|
|
53
|
+
const key = this.keyOf(dir)
|
|
54
|
+
const running = this.inFlight.get(key)
|
|
55
|
+
if (!fresh && running !== undefined && running.revision === revision) return running.promise
|
|
56
|
+
const sequence = (this.sequence += 1)
|
|
57
|
+
const promise = this.load(dir)
|
|
58
|
+
const entry: ListingRequest = { promise, revision }
|
|
59
|
+
this.inFlight.set(key, entry)
|
|
60
|
+
this.newest.set(key, sequence)
|
|
61
|
+
const settle = (): void => {
|
|
62
|
+
if (this.inFlight.get(key) === entry) this.inFlight.delete(key)
|
|
63
|
+
}
|
|
64
|
+
promise.then(
|
|
65
|
+
(entries) => {
|
|
66
|
+
if (this.newest.get(key) === sequence) {
|
|
67
|
+
this.newest.delete(key)
|
|
68
|
+
this.store(key, entries)
|
|
69
|
+
}
|
|
70
|
+
settle()
|
|
71
|
+
},
|
|
72
|
+
() => {
|
|
73
|
+
if (this.newest.get(key) === sequence) this.newest.delete(key)
|
|
74
|
+
settle()
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
return promise
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Whether a listing for `dir` is on the wire. */
|
|
81
|
+
busy(dir: string): boolean {
|
|
82
|
+
return this.inFlight.has(this.keyOf(dir))
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/client/vsc/kit.tsx
CHANGED
|
@@ -675,6 +675,14 @@ export interface MenuEntry {
|
|
|
675
675
|
separated?: boolean
|
|
676
676
|
}
|
|
677
677
|
|
|
678
|
+
/**
|
|
679
|
+
* Grace period after a menu appears during which a LEFT mousedown cannot close
|
|
680
|
+
* it: the opening gesture itself may still deliver one (platform-dependent, and
|
|
681
|
+
* a trackpad two-finger tap more often), and a panel re-render around the click
|
|
682
|
+
* can shift where that event lands.
|
|
683
|
+
*/
|
|
684
|
+
const MENU_OPEN_GRACE_MS = 250
|
|
685
|
+
|
|
678
686
|
/** Portal-rendered context menu, clamped to the viewport before paint. */
|
|
679
687
|
export function ContextMenu(props: {
|
|
680
688
|
x: number
|
|
@@ -684,6 +692,8 @@ export function ContextMenu(props: {
|
|
|
684
692
|
}): ReactNode {
|
|
685
693
|
const boxRef = useRef<HTMLDivElement>(null)
|
|
686
694
|
const [pos, setPos] = useState({ left: props.x, top: props.y })
|
|
695
|
+
/** When this menu appeared (monotonic): see MENU_OPEN_GRACE_MS. */
|
|
696
|
+
const openedAt = useRef(typeof performance === 'undefined' ? 0 : performance.now())
|
|
687
697
|
|
|
688
698
|
useLayoutEffect(() => {
|
|
689
699
|
const box = boxRef.current
|
|
@@ -709,22 +719,39 @@ export function ContextMenu(props: {
|
|
|
709
719
|
|
|
710
720
|
useEffect(() => {
|
|
711
721
|
const onDown = (e: MouseEvent): void => {
|
|
722
|
+
// The right-click that opened this menu can deliver one more mousedown
|
|
723
|
+
// (platform-dependent; a trackpad two-finger tap does it more often), and
|
|
724
|
+
// the closing job belongs to LEFT presses anyway: a fresh right-click
|
|
725
|
+
// opens a NEW menu, which replaces this one without needing a close.
|
|
726
|
+
if (e.button === 2) return
|
|
727
|
+
if (performance.now() - openedAt.current < MENU_OPEN_GRACE_MS) return
|
|
712
728
|
if (boxRef.current?.contains(e.target as Node) === true) return
|
|
713
729
|
onCloseRef.current()
|
|
714
730
|
}
|
|
715
731
|
const onKey = (e: KeyboardEvent): void => {
|
|
716
732
|
if (e.key === 'Escape') onCloseRef.current()
|
|
717
733
|
}
|
|
718
|
-
const
|
|
734
|
+
const onResize = (): void => onCloseRef.current()
|
|
735
|
+
const onScroll = (e: Event): void => {
|
|
736
|
+
// Only the PAGE scrolling moves the world under the menu. Element
|
|
737
|
+
// scrollers (the file tree, the chat log, the SCM list) fire scroll events
|
|
738
|
+
// while background work lands — a git status refresh was enough to close
|
|
739
|
+
// the menu the moment it opened, which read as "the menu flashes and never
|
|
740
|
+
// opens". The menu is portaled at fixed viewport coordinates, so those
|
|
741
|
+
// scrolls do not invalidate it.
|
|
742
|
+
const target = e.target
|
|
743
|
+
if (target instanceof Node && target !== document && target !== document.documentElement && target !== document.body) return
|
|
744
|
+
onCloseRef.current()
|
|
745
|
+
}
|
|
719
746
|
window.addEventListener('mousedown', onDown, true)
|
|
720
747
|
window.addEventListener('keydown', onKey)
|
|
721
748
|
window.addEventListener('scroll', onScroll, true)
|
|
722
|
-
window.addEventListener('resize',
|
|
749
|
+
window.addEventListener('resize', onResize)
|
|
723
750
|
return () => {
|
|
724
751
|
window.removeEventListener('mousedown', onDown, true)
|
|
725
752
|
window.removeEventListener('keydown', onKey)
|
|
726
753
|
window.removeEventListener('scroll', onScroll, true)
|
|
727
|
-
window.removeEventListener('resize',
|
|
754
|
+
window.removeEventListener('resize', onResize)
|
|
728
755
|
}
|
|
729
756
|
}, [])
|
|
730
757
|
|