mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 07:32:54 +00:00
fix(sftp): keep bulk queues and large-file resume responsive (#3226)
* fix(sftp): keep bulk queues and large-file resume responsive * fix(sftp): preserve resume compatibility and timeout cleanup * test(sftp): verify fixture cleanup in fresh environments * fix: address Codex review on PR #3226 * fix(sftp): remove blocking periodic history persistence --------- Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
This commit is contained in:
1 parent
39d7c38a6a
commit
fb034f08bb
8 files changed
+714
-31
No files matched your search
@@ -17,6 +17,7 @@ import {
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import React, { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
||||
import { useVirtualizer } from "@tanstack/react-virtual";
|
||||
|
||||
import { useI18n } from "../application/i18n/I18nProvider";
|
||||
import {
|
||||
@@ -341,12 +342,17 @@ function formatTransferPathLine(task: Pick<TransferTask, "sourcePath" | "targetP
|
||||
function TransferRow({
|
||||
task,
|
||||
childTasks = [],
|
||||
expanded,
|
||||
onToggleExpanded,
|
||||
isLast,
|
||||
}: {
|
||||
task: TransferTask;
|
||||
childTasks?: readonly TransferTask[];
|
||||
expanded: boolean;
|
||||
onToggleExpanded: () => void;
|
||||
isLast: boolean;
|
||||
}) {
|
||||
const { t } = useI18n();
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const folderReplaceWarningId = React.useId();
|
||||
// Optimistic spinner from click until store status moves off paused/interrupted.
|
||||
const [resumeClicked, setResumeClicked] = useState(false);
|
||||
@@ -478,7 +484,7 @@ function TransferRow({
|
||||
|
||||
return (
|
||||
<div
|
||||
className="border-b border-border/40 px-3 py-2.5 last:border-b-0 hover:bg-muted/30"
|
||||
className={cn("border-border/40 px-3 py-2.5 hover:bg-muted/30", isLast ? "border-b-0" : "border-b")}
|
||||
data-section="global-sftp-transfer-row"
|
||||
data-transfer-status={task.status}
|
||||
data-directory-parent={isDirParent ? "true" : undefined}
|
||||
@@ -506,7 +512,7 @@ function TransferRow({
|
||||
{canToggleChildren && (
|
||||
<TransferAction
|
||||
label={expanded ? t("sftp.transfers.collapseChildren") : t("sftp.transfers.expandChildren")}
|
||||
onClick={() => setExpanded((value) => !value)}
|
||||
onClick={onToggleExpanded}
|
||||
>
|
||||
{expanded ? <ChevronUp size={13} /> : <ChevronDown size={13} />}
|
||||
</TransferAction>
|
||||
@@ -755,6 +761,59 @@ function TransferRow({
|
||||
);
|
||||
}
|
||||
|
||||
function TransferList({ tasks, childrenByParent, empty }: {
|
||||
tasks: readonly TransferTask[];
|
||||
childrenByParent: ReadonlyMap<string, TransferTask[]>;
|
||||
empty: React.ReactNode;
|
||||
}) {
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
// Keep folder expansion when a row scrolls out of the mounted viewport.
|
||||
const [expandedIds, setExpandedIds] = useState<ReadonlySet<string>>(() => new Set());
|
||||
const virtual = tasks.length > 20;
|
||||
const virtualizer = useVirtualizer({
|
||||
count: tasks.length,
|
||||
getScrollElement: () => scrollRef.current,
|
||||
estimateSize: () => 112,
|
||||
getItemKey: (index) => tasks[index].id,
|
||||
overscan: 4,
|
||||
enabled: virtual,
|
||||
});
|
||||
const renderRow = (task: TransferTask, index: number) => (
|
||||
<TransferRow
|
||||
key={task.id}
|
||||
task={task}
|
||||
childTasks={childrenByParent.get(task.id) ?? []}
|
||||
expanded={expandedIds.has(task.id)}
|
||||
isLast={index === tasks.length - 1}
|
||||
onToggleExpanded={() => setExpandedIds((previous) => {
|
||||
const next = new Set(previous);
|
||||
if (next.has(task.id)) next.delete(task.id);
|
||||
else next.add(task.id);
|
||||
return next;
|
||||
})}
|
||||
/>
|
||||
);
|
||||
return (
|
||||
<div ref={scrollRef} className="max-h-[460px] overflow-auto" data-section="global-sftp-transfer-list">
|
||||
{tasks.length === 0 ? empty : !virtual ? tasks.map(renderRow) : (
|
||||
<div className="relative w-full" style={{ height: virtualizer.getTotalSize() }}>
|
||||
{virtualizer.getVirtualItems().map((row) => (
|
||||
<div
|
||||
key={row.key}
|
||||
ref={virtualizer.measureElement}
|
||||
data-index={row.index}
|
||||
className="absolute left-0 top-0 w-full"
|
||||
style={{ transform: `translateY(${row.start}px)` }}
|
||||
>
|
||||
{renderRow(tasks[row.index], row.index)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function GlobalSftpTransferCenter() {
|
||||
const { t } = useI18n();
|
||||
// Badge is a stable store snapshot (identity unchanged on pure progress).
|
||||
@@ -866,20 +925,17 @@ export function GlobalSftpTransferCenter() {
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="max-h-[460px] overflow-auto">
|
||||
{displayed.length === 0 ? (
|
||||
<TransferList
|
||||
key={`${bucket}-${showBackground}`}
|
||||
tasks={displayed}
|
||||
childrenByParent={childrenByParent}
|
||||
empty={(
|
||||
<div className="flex h-40 flex-col items-center justify-center text-muted-foreground">
|
||||
{badge.hasAttention && bucket !== "attention" && bucket !== "all" ? <AlertCircle size={22} /> : <ArrowDownUp size={22} />}
|
||||
<span className="mt-2 text-xs">{t("sftp.transferCenter.empty")}</span>
|
||||
</div>
|
||||
) : displayed.map((task) => (
|
||||
<TransferRow
|
||||
key={task.id}
|
||||
task={task}
|
||||
childTasks={childrenByParent.get(task.id) ?? []}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
|
||||
{(() => {
|
||||
const showBackgroundToggle = collapsed.length > 0;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import React from "react";
|
||||
import { createDomRenderer, dispatchDomEvent, flushEffects, installDomEnvironment } from "./test-support/renderReactDom.tsx";
|
||||
|
||||
test("large transfer histories mount only visible rows and can scroll to the last file", async (t) => {
|
||||
const env = installDomEnvironment();
|
||||
const previous = new Map<string, PropertyDescriptor | undefined>();
|
||||
const globals = {
|
||||
MutationObserver: env.window.MutationObserver,
|
||||
NodeFilter: env.window.NodeFilter,
|
||||
HTMLInputElement: env.window.HTMLInputElement,
|
||||
Element: env.window.Element,
|
||||
requestAnimationFrame: (callback: FrameRequestCallback) => setTimeout(() => callback(Date.now()), 0),
|
||||
cancelAnimationFrame: clearTimeout,
|
||||
};
|
||||
for (const [key, value] of Object.entries(globals)) {
|
||||
previous.set(key, Object.getOwnPropertyDescriptor(globalThis, key));
|
||||
Object.defineProperty(globalThis, key, { configurable: true, writable: true, value });
|
||||
}
|
||||
Object.defineProperties(env.window.HTMLElement.prototype, {
|
||||
offsetWidth: { configurable: true, get: () => 460 },
|
||||
offsetHeight: { configurable: true, get() { return this.dataset.section === "global-sftp-transfer-list" ? 460 : 112; } },
|
||||
});
|
||||
const { I18nProvider } = await import("../application/i18n/I18nProvider.tsx");
|
||||
const { sftpTransferCenterStore: store } = await import("../application/state/sftpTransferCenterStore.ts");
|
||||
const { GlobalSftpTransferCenter } = await import("./GlobalSftpTransferCenter.tsx");
|
||||
const { TooltipProvider } = await import("./ui/tooltip.tsx");
|
||||
const renderer = await createDomRenderer(env.document);
|
||||
t.after(async () => {
|
||||
await renderer.unmount();
|
||||
for (const [key, descriptor] of previous) {
|
||||
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
|
||||
else Reflect.deleteProperty(globalThis, key);
|
||||
}
|
||||
env.cleanup();
|
||||
});
|
||||
store.publishOwner("virtual-list-test", Array.from({ length: 1_000 }, (_, index) => ({
|
||||
id: `virtual-file-${index}`, fileName: `file-${index}.bin`,
|
||||
sourcePath: `/source/${index}`, targetPath: `/target/${index}`,
|
||||
sourceConnectionId: "source", targetConnectionId: "local", direction: "download" as const,
|
||||
status: "queued" as const, totalBytes: 1_000, transferredBytes: 0, speed: 0,
|
||||
startTime: index + 1, isDirectory: false,
|
||||
})));
|
||||
await renderer.render(<I18nProvider locale="en"><TooltipProvider><GlobalSftpTransferCenter /></TooltipProvider></I18nProvider>);
|
||||
const toggle = env.document.querySelector("[data-section=global-sftp-transfer-toggle]");
|
||||
assert.ok(toggle);
|
||||
await dispatchDomEvent(toggle, new env.window.MouseEvent("click", { bubbles: true }));
|
||||
await flushEffects();
|
||||
const rows = () => env.document.querySelectorAll("[role=progressbar]");
|
||||
assert.ok(rows().length > 0 && rows().length < 40, `rendered ${rows().length} rows`);
|
||||
assert.ok(env.document.querySelector('[role=progressbar][aria-label="file-999.bin"]'));
|
||||
const firstRow = env.document.querySelector('[data-transfer-status="queued"]');
|
||||
assert.ok(firstRow);
|
||||
assert.equal(firstRow.matches('.last\\:border-b-0:last-child'), false, "a virtual row must not lose its divider just because it has a wrapper");
|
||||
|
||||
const scroll = env.document.querySelector<HTMLElement>("[data-section=global-sftp-transfer-list]");
|
||||
assert.ok(scroll);
|
||||
scroll.scrollTop = 112 * 1_000 - 460;
|
||||
await dispatchDomEvent(scroll, new env.window.Event("scroll"));
|
||||
await flushEffects();
|
||||
assert.ok(rows().length > 0 && rows().length < 40);
|
||||
assert.ok(env.document.querySelector('[role=progressbar][aria-label="file-0.bin"]'));
|
||||
const lastRow = env.document.querySelector('[role=progressbar][aria-label="file-0.bin"]')?.closest('[data-transfer-status]');
|
||||
assert.ok(lastRow?.classList.contains("border-b-0"), "only the actual final row omits its divider");
|
||||
assert.equal(store.getSnapshot().tasks.length, 1_000, "offscreen files must remain queued");
|
||||
});
|
||||
Reference in new issue
Block a user