Files
Netcatty/components/terminal/TerminalConnectionProgress.tsx
T
陈大猫andClaude Opus 5.5 fa554888a6 perf(terminal): stop per-second churn while connecting and while idle (#4118)
* perf(terminal): stop per-second churn while connecting and while idle

- Connecting sessions: the 200 ms progress animation and 1 s countdown were
  Terminal state threaded into the TerminalView ctx, so every connecting
  session (hidden tabs and startup restores included) re-rendered Terminal +
  TerminalView up to six times a second. Terminal now only publishes the
  timeout deadline and real progress events; the connection dialog animates
  and counts down locally (connectionTicker.ts). HostConnectionTestDialog
  keeps passing progress/timeLeft as before.
- Monitor runtime: polled every second unconditionally and woke every
  subscriber each time because takenAt always changes. It now polls at 1 s
  only while something is monitored (here or in another window), 10 s
  otherwise, and skips notifying for unchanged idle replies.
- Auto-reconnect progress log: capped at the newest 300 lines (it grew by a
  few lines every 5 s retry for as long as an outage lasted).
- App Lock: report activity at most once a second instead of an IPC per
  keystroke / wheel tick / pointer event.
- Tray/Dock: the renderer resends hosts only when the host list changed, and
  the main process rebuilds the macOS Dock menu (sort + one native item per
  host) only then, not on every session status change.

Real app, profiling build, main -> this change:
  3 SSH sessions connecting (3 s)   14 commits / 35.3 ms -> 8 / 1.7 ms
  idle (10 s)                       13 commits / 3.1 ms  -> 2 / 0.1 ms

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(terminal): update observe-popup reconnect deps assertion for capped progress logs

CI failed because #4118 wrapped setProgressLogs in useCallback to apply
capConnectionProgressLogs and added it to scheduleAutoReconnect deps.
The observe-popup source assertion still expected the old deps list.

The attachExistingSession early-return behavior is unchanged; only the
stale dependency regex needed updating.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 01:11:25 -08:00

139 lines
5.7 KiB
TypeScript

/**
* Terminal Connection Progress
* Displays connection progress with logs and timeout
*/
import { Loader2, Play } from 'lucide-react';
import React from 'react';
import { useI18n } from '../../application/i18n/I18nProvider';
import { useConnectionCountdownSeconds } from './connectionTicker';
import { Button } from '../ui/button';
import { ScrollArea } from '../ui/scroll-area';
export interface TerminalConnectionProgressProps {
status: 'connecting' | 'connected' | 'disconnected';
error: string | null;
/** Seconds shown before a deadline is known; also used as-is when `deadlineAt` is omitted. */
timeLeft: number;
/** Connection timeout deadline (epoch ms). When provided, counts down locally. */
deadlineAt?: number | null;
isAwaitingUserInput?: boolean;
showEnterReconnectHint?: boolean;
isCancelling: boolean;
showLogs: boolean;
progressLogs: string[];
onCancelConnect: () => void;
onCloseSession: () => void;
onRetry?: () => void;
reconnectLabel?: string;
closeLabel?: string;
}
export interface TerminalConnectionLogListProps {
progressLogs: string[];
error?: string | null;
}
export const TerminalConnectionLogList: React.FC<TerminalConnectionLogListProps> = ({
progressLogs,
error,
}) => (
<div className="rounded-md border border-border/35 bg-background/40">
<ScrollArea className="max-h-44">
<div className="space-y-1 p-2.5 pb-4 pr-4 text-xs text-foreground/90">
{progressLogs.map((line, idx) => (
<div key={idx} className="flex items-start gap-2">
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-emerald-500" />
<div className="min-w-0 break-words leading-5">{line}</div>
</div>
))}
{error && (
<div className="flex items-start gap-2 text-destructive">
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-destructive" />
<div className="min-w-0 break-words leading-5">{error}</div>
</div>
)}
</div>
</ScrollArea>
</div>
);
export const TerminalConnectionProgress: React.FC<TerminalConnectionProgressProps> = ({
status,
error,
timeLeft: timeLeftProp,
deadlineAt,
isAwaitingUserInput = false,
showEnterReconnectHint = false,
isCancelling: _isCancelling,
showLogs,
progressLogs,
onCancelConnect: _onCancelConnect,
onCloseSession,
onRetry,
reconnectLabel,
closeLabel,
}) => {
const { t } = useI18n();
const countdownSeconds = useConnectionCountdownSeconds(deadlineAt ?? null, timeLeftProp);
const timeLeft = deadlineAt === undefined ? timeLeftProp : countdownSeconds;
return (
<>
<div className="flex items-start justify-between gap-3 text-[11px] text-muted-foreground">
<div className="flex min-w-0 items-start gap-2">
{status === 'connecting' ? (
<>
<Loader2 className="h-3 w-3 mt-0.5 flex-shrink-0 animate-spin" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5">
{isAwaitingUserInput
? t('terminal.progress.waitingForUserInput')
: t('terminal.progress.timeoutIn', { seconds: timeLeft })}
</span>
</>
) : status === 'connected' ? (
<>
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-emerald-500" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5 text-emerald-500">
{t('terminal.progress.connected')}
</span>
</>
) : (
<>
<div className="mt-[0.4rem] h-1.5 w-1.5 flex-shrink-0 rounded-full bg-destructive" />
<span className="min-w-0 whitespace-pre-wrap break-words leading-5 text-destructive">
{error || t('terminal.progress.disconnected')}
</span>
</>
)}
</div>
</div>
{showLogs && (
<TerminalConnectionLogList progressLogs={progressLogs} error={error} />
)}
{status !== 'connecting' && (
<div className="flex flex-wrap items-center justify-between gap-2">
{showEnterReconnectHint && (
<div className="min-w-0 break-words text-[11px] leading-5 text-muted-foreground">
{t('terminal.progress.enterReconnectHint')}
</div>
)}
<div className="ml-auto flex shrink-0 justify-end gap-2">
<Button variant="ghost" size="sm" className="h-7 px-3 text-[11px]" onClick={onCloseSession}>
{closeLabel ?? t('terminal.toolbar.closeSession')}
</Button>
{onRetry && (
<Button size="sm" className="h-7 px-3 text-[11px]" onClick={onRetry}>
<Play className="h-3 w-3 mr-1.5" /> {reconnectLabel ?? t('terminal.progress.startOver')}
</Button>
)}
</div>
</div>
)}
</>
);
};
export default TerminalConnectionProgress;