Adds per-log theme and font size controls to log replay

Enables users to customize terminal theme and font size for each log replay,
improving readability and personalization of the log view experience. Updates
persist in the log model, and theme/font changes apply instantly without
recreating the terminal. Also improves UI for these controls and tidies minor
imports.
This commit is contained in:
bincxz committed 2025-12-14 23:16:35 +08:00
1 parent 5693bc530b
commit 2c53b1d997
5 files changed
+144 -46

No files matched your search

+23 -16
View File
@@ -6,7 +6,7 @@ import { useVaultState } from './application/state/useVaultState';
import { useWindowControls } from './application/state/useWindowControls';
import { matchesKeyBinding } from './domain/models';
import { netcattyBridge } from './infrastructure/services/netcattyBridge';
import LogView from './components/LogView';
import LogView from './components/LogView.tsx';
import ProtocolSelectDialog from './components/ProtocolSelectDialog';
import { QuickSwitcher } from './components/QuickSwitcher';
import SettingsDialog from './components/SettingsDialog';
@@ -20,7 +20,7 @@ import { Label } from './components/ui/label';
import { ToastProvider } from './components/ui/toast';
import { VaultView, VaultSection } from './components/VaultView';
import { cn } from './lib/utils';
import { Host, HostProtocol, TerminalTheme } from './types';
import { ConnectionLog, Host, HostProtocol, TerminalTheme } from './types';
import { LogView as LogViewType } from './application/state/useSessionState';
// Visibility container for VaultView - isolates isActive subscription
@@ -40,12 +40,13 @@ const VaultViewContainer: React.FC<{ children: React.ReactNode }> = ({ children
// LogView wrapper - manages visibility based on active tab
interface LogViewWrapperProps {
logView: LogViewType;
terminalTheme: TerminalTheme;
fontSize: number;
defaultTerminalTheme: TerminalTheme;
defaultFontSize: number;
onClose: () => void;
onUpdateLog: (logId: string, updates: Partial<ConnectionLog>) => void;
}
const LogViewWrapper: React.FC<LogViewWrapperProps> = ({ logView, terminalTheme, fontSize, onClose }) => {
const LogViewWrapper: React.FC<LogViewWrapperProps> = ({ logView, defaultTerminalTheme, defaultFontSize, onClose, onUpdateLog }) => {
const activeTabId = useActiveTabId();
const isVisible = activeTabId === logView.id;
@@ -58,10 +59,11 @@ const LogViewWrapper: React.FC<LogViewWrapperProps> = ({ logView, terminalTheme,
<div className={cn("absolute inset-0", isVisible ? "z-20" : "")} style={containerStyle}>
<LogView
log={logView.log}
terminalTheme={terminalTheme}
fontSize={fontSize}
defaultTerminalTheme={defaultTerminalTheme}
defaultFontSize={defaultFontSize}
isVisible={isVisible}
onClose={onClose}
onUpdateLog={onUpdateLog}
/>
</div>
);
@@ -672,15 +674,20 @@ function App() {
/>
{/* Log Views - readonly terminal replays */}
{logViews.map(logView => (
<LogViewWrapper
key={logView.id}
logView={logView}
terminalTheme={currentTerminalTheme}
fontSize={terminalFontSize}
onClose={() => closeLogView(logView.id)}
/>
))}
{logViews.map(logView => {
// Get the latest log data from connectionLogs to reflect updates
const latestLog = connectionLogs.find(l => l.id === logView.connectionLogId) || logView.log;
return (
<LogViewWrapper
key={logView.id}
logView={{ ...logView, log: latestLog }}
defaultTerminalTheme={currentTerminalTheme}
defaultFontSize={terminalFontSize}
onClose={() => closeLogView(logView.id)}
onUpdateLog={updateConnectionLog}
/>
);
})}
</div>
<QuickSwitcher
-2
View File
@@ -419,8 +419,6 @@ echo $3 >> "$FILE"`);
}
}, [
draftKey,
isMac,
hasBrowserWebAuthn,
createCredentialInBrowser,
onSave,
closePanel,
+2 -4
View File
@@ -1,10 +1,8 @@
import {
ArrowRight,
ChevronDown,
Clock,
FolderOpen,
Import,
Key,
LayoutGrid,
List as ListIcon,
RefreshCw,
@@ -51,7 +49,7 @@ interface KnownHostsManagerProps {
type ViewMode = "grid" | "list";
// Helper functions outside component for stable references
const formatDateFn = (timestamp: number) => {
const _formatDateFn = (timestamp: number) => {
return new Date(timestamp).toLocaleDateString("en-US", {
year: "numeric",
month: "short",
@@ -59,7 +57,7 @@ const formatDateFn = (timestamp: number) => {
});
};
const getKeyTypeColorFn = (keyType: string) => {
const _getKeyTypeColorFn = (keyType: string) => {
switch (keyType.toLowerCase()) {
case "ssh-ed25519":
return "text-emerald-500";
+117 -24
View File
@@ -2,34 +2,46 @@ import { Terminal as XTerm } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import { WebglAddon } from "@xterm/addon-webgl";
import "@xterm/xterm/css/xterm.css";
import { FileText, X } from "lucide-react";
import React, { memo, useEffect, useMemo, useRef, useState } from "react";
import { ChevronDown, FileText, Minus, Plus, X } from "lucide-react";
import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { cn } from "../lib/utils";
import { ConnectionLog, TerminalTheme } from "../types";
import { TERMINAL_THEMES } from "../infrastructure/config/terminalThemes";
import { Button } from "./ui/button";
import { Dropdown, DropdownContent, DropdownTrigger } from "./ui/dropdown";
interface LogViewProps {
log: ConnectionLog;
terminalTheme: TerminalTheme;
fontSize: number;
defaultTerminalTheme: TerminalTheme;
defaultFontSize: number;
isVisible: boolean;
onClose: () => void;
onUpdateLog: (logId: string, updates: Partial<ConnectionLog>) => void;
}
const LogViewComponent: React.FC<LogViewProps> = ({
log,
terminalTheme,
fontSize,
defaultTerminalTheme,
defaultFontSize,
isVisible,
onClose,
onUpdateLog,
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const termRef = useRef<XTerm | null>(null);
const fitAddonRef = useRef<FitAddon | null>(null);
const [isReady, setIsReady] = useState(false);
const [themeDropdownOpen, setThemeDropdownOpen] = useState(false);
console.log('[LogView] Render', { logId: log.id, isVisible, hasTerminalData: !!log.terminalData });
// Use log's saved theme/fontSize or fall back to defaults
const currentTheme = useMemo(() => {
if (log.themeId) {
return TERMINAL_THEMES.find(t => t.id === log.themeId) || defaultTerminalTheme;
}
return defaultTerminalTheme;
}, [log.themeId, defaultTerminalTheme]);
const currentFontSize = log.fontSize ?? defaultFontSize;
// Format date for display
const formattedDate = useMemo(() => {
@@ -43,22 +55,31 @@ const LogViewComponent: React.FC<LogViewProps> = ({
});
}, [log.startTime]);
// Handle theme change
const handleThemeChange = useCallback((themeId: string) => {
onUpdateLog(log.id, { themeId });
setThemeDropdownOpen(false);
}, [log.id, onUpdateLog]);
// Handle font size change
const handleFontSizeChange = useCallback((delta: number) => {
const newSize = Math.max(8, Math.min(24, currentFontSize + delta));
onUpdateLog(log.id, { fontSize: newSize });
}, [log.id, currentFontSize, onUpdateLog]);
// Initialize terminal
useEffect(() => {
console.log('[LogView] useEffect', { isVisible, hasContainer: !!containerRef.current });
if (!containerRef.current || !isVisible) return;
console.log('[LogView] Creating terminal');
// Create terminal
const term = new XTerm({
fontFamily: '"JetBrains Mono", "SF Mono", Monaco, Menlo, monospace',
fontSize: fontSize,
fontSize: currentFontSize,
cursorBlink: false,
cursorStyle: "underline",
allowProposedApi: true,
disableStdin: true, // Read-only mode
theme: terminalTheme.theme,
theme: currentTheme.colors,
scrollback: 10000,
});
@@ -71,13 +92,12 @@ const LogViewComponent: React.FC<LogViewProps> = ({
// Open terminal
term.open(containerRef.current);
console.log('[LogView] Terminal opened');
// Try to load WebGL addon for better performance
try {
const webglAddon = new WebglAddon();
term.loadAddon(webglAddon);
} catch (e) {
} catch {
// WebGL not available, canvas renderer will be used
}
@@ -85,14 +105,12 @@ const LogViewComponent: React.FC<LogViewProps> = ({
setTimeout(() => {
try {
fitAddon.fit();
console.log('[LogView] Terminal fitted');
} catch {
// Ignore fit errors
}
}, 50);
// Write terminal data if available
console.log('[LogView] Writing data, hasTerminalData:', !!log.terminalData, 'length:', log.terminalData?.length);
if (log.terminalData) {
term.write(log.terminalData);
} else {
@@ -120,14 +138,32 @@ const LogViewComponent: React.FC<LogViewProps> = ({
fitAddonRef.current = null;
setIsReady(false);
};
}, [isVisible, log.terminalData, log.hostname, log.username, log.protocol, formattedDate, log.endTime, log.startTime, fontSize, terminalTheme.theme]);
// Only re-create terminal when visibility or terminalData changes
// Theme and font size updates are handled separately
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isVisible, log.id, log.terminalData]);
// Update theme
// Update theme instantly without recreating terminal
useEffect(() => {
if (termRef.current && isReady) {
termRef.current.options.theme = terminalTheme.theme;
termRef.current.options.theme = currentTheme.colors;
}
}, [terminalTheme, isReady]);
}, [currentTheme, isReady]);
// Update font size instantly without recreating terminal
useEffect(() => {
if (termRef.current && isReady) {
termRef.current.options.fontSize = currentFontSize;
// Refit after font size change
setTimeout(() => {
try {
fitAddonRef.current?.fit();
} catch {
// Ignore fit errors
}
}, 10);
}
}, [currentFontSize, isReady]);
// Handle resize
useEffect(() => {
@@ -180,8 +216,63 @@ const LogViewComponent: React.FC<LogViewProps> = ({
</div>
</div>
<div className="flex items-center gap-2">
{/* Theme selector */}
<Dropdown open={themeDropdownOpen} onOpenChange={setThemeDropdownOpen}>
<DropdownTrigger asChild>
<Button variant="ghost" size="sm" className="gap-1 h-8 px-2">
<div
className="w-4 h-4 rounded border border-border/50"
style={{ backgroundColor: currentTheme.colors.background }}
/>
<span className="text-xs max-w-20 truncate">{currentTheme.name}</span>
<ChevronDown size={12} />
</Button>
</DropdownTrigger>
<DropdownContent className="w-48 max-h-64 overflow-y-auto" align="end">
{TERMINAL_THEMES.map(theme => (
<button
key={theme.id}
className={cn(
"w-full flex items-center gap-2 px-2 py-1.5 text-left text-sm rounded hover:bg-secondary/80 transition-colors",
currentTheme.id === theme.id && "bg-secondary"
)}
onClick={() => handleThemeChange(theme.id)}
>
<div
className="w-5 h-5 rounded border border-border/50 flex-shrink-0"
style={{ backgroundColor: theme.colors.background }}
/>
<span className="truncate">{theme.name}</span>
</button>
))}
</DropdownContent>
</Dropdown>
{/* Font size controls */}
<div className="flex items-center gap-0.5 bg-secondary/50 rounded px-1">
<Button
variant="ghost"
size="sm"
className="h-7 w-7 p-0"
onClick={() => handleFontSizeChange(-1)}
disabled={currentFontSize <= 8}
>
<Minus size={12} />
</Button>
<span className="text-xs w-6 text-center">{currentFontSize}</span>
<Button
variant="ghost"
size="sm"
className="h-7 w-7 p-0"
onClick={() => handleFontSizeChange(1)}
disabled={currentFontSize >= 24}
>
<Plus size={12} />
</Button>
</div>
<span className="text-xs text-muted-foreground bg-secondary px-2 py-1 rounded">
Read-only Log Replay
Read-only
</span>
<Button variant="ghost" size="sm" onClick={onClose}>
<X size={16} />
@@ -192,7 +283,7 @@ const LogViewComponent: React.FC<LogViewProps> = ({
{/* Terminal container */}
<div
className="flex-1 overflow-hidden p-2"
style={{ backgroundColor: terminalTheme?.theme?.background || '#000000' }}
style={{ backgroundColor: currentTheme?.colors?.background || '#000000' }}
>
<div ref={containerRef} className="h-full w-full" />
</div>
@@ -204,9 +295,11 @@ const LogViewComponent: React.FC<LogViewProps> = ({
const logViewAreEqual = (prev: LogViewProps, next: LogViewProps): boolean => {
return (
prev.log.id === next.log.id &&
prev.log.themeId === next.log.themeId &&
prev.log.fontSize === next.log.fontSize &&
prev.isVisible === next.isVisible &&
prev.fontSize === next.fontSize &&
prev.terminalTheme.id === next.terminalTheme.id
prev.defaultFontSize === next.defaultFontSize &&
prev.defaultTerminalTheme.id === next.defaultTerminalTheme.id
);
};
+2
View File
@@ -583,4 +583,6 @@ export interface ConnectionLog {
localHostname: string; // Local machine hostname
saved: boolean; // Whether this log is bookmarked/saved
terminalData?: string; // Captured terminal output data for replay
themeId?: string; // Terminal theme ID for this log view
fontSize?: number; // Terminal font size for this log view
}