diff --git a/components/host/HostTreeGroupInlineRenameInput.tsx b/components/host/HostTreeGroupInlineRenameInput.tsx index 0230f0a42..1f29e5ec6 100644 --- a/components/host/HostTreeGroupInlineRenameInput.tsx +++ b/components/host/HostTreeGroupInlineRenameInput.tsx @@ -1,6 +1,6 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React from 'react'; -import { cn } from '../../lib/utils'; +import { InlineRenameInput } from '../ui/inline-rename-input'; type HostTreeGroupInlineRenameInputProps = { initialName: string; @@ -10,77 +10,11 @@ type HostTreeGroupInlineRenameInputProps = { style?: React.CSSProperties; }; -export const HostTreeGroupInlineRenameInput: React.FC = ({ - initialName, - onCommit, - onCancel, - className, - style, -}) => { - const inputRef = useRef(null); - const [value, setValue] = useState(initialName); - const submittingRef = useRef(false); - - useEffect(() => { - const input = inputRef.current; - if (!input) return; - input.focus(); - input.select(); - }, []); - - const commit = async () => { - if (submittingRef.current) return; - submittingRef.current = true; - try { - const committed = await onCommit(value); - if (committed === false) submittingRef.current = false; - } catch { - submittingRef.current = false; - } - }; - - const cancel = () => { - if (submittingRef.current) return; - submittingRef.current = true; - onCancel(); - }; - - return ( - setValue(event.target.value)} - onBlur={() => { - queueMicrotask(() => { - void commit(); - }); - }} - onClick={(event) => event.stopPropagation()} - onDoubleClick={(event) => event.stopPropagation()} - onMouseDown={(event) => event.stopPropagation()} - onPointerDown={(event) => event.stopPropagation()} - onDragStart={(event) => { - event.preventDefault(); - event.stopPropagation(); - }} - onKeyDown={(event) => { - event.stopPropagation(); - if (event.key === 'Enter') { - event.preventDefault(); - void commit(); - } - if (event.key === 'Escape') { - event.preventDefault(); - cancel(); - } - }} - className={cn( - 'min-w-0 flex-1 truncate select-text rounded-sm border border-primary/50 bg-background/80 px-1 py-0 text-sm font-medium outline-none ring-1 ring-primary/30', - className, - )} - style={style} - /> - ); -}; +export const HostTreeGroupInlineRenameInput: React.FC = ( + props, +) => ( + +); diff --git a/components/terminal/SessionInlineRenameInput.tsx b/components/terminal/SessionInlineRenameInput.tsx index 5c910328a..b2a9882f8 100644 --- a/components/terminal/SessionInlineRenameInput.tsx +++ b/components/terminal/SessionInlineRenameInput.tsx @@ -1,6 +1,6 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React from 'react'; -import { cn } from '../../lib/utils'; +import { InlineRenameInput } from '../ui/inline-rename-input'; type SessionInlineRenameInputProps = { initialName: string; @@ -10,72 +10,11 @@ type SessionInlineRenameInputProps = { style?: React.CSSProperties; }; -export const SessionInlineRenameInput: React.FC = ({ - initialName, - onCommit, - onCancel, - className, - style, -}) => { - const inputRef = useRef(null); - const [value, setValue] = useState(initialName); - const committedRef = useRef(false); - - useEffect(() => { - const input = inputRef.current; - if (!input) return; - input.focus(); - input.select(); - }, []); - - const commit = () => { - if (committedRef.current) return; - committedRef.current = true; - onCommit(value); - }; - - const cancel = () => { - if (committedRef.current) return; - committedRef.current = true; - onCancel(); - }; - - return ( - setValue(event.target.value)} - onBlur={() => { - queueMicrotask(() => { - commit(); - }); - }} - onClick={(event) => event.stopPropagation()} - onDoubleClick={(event) => event.stopPropagation()} - onMouseDown={(event) => event.stopPropagation()} - onPointerDown={(event) => event.stopPropagation()} - onDragStart={(event) => { - event.preventDefault(); - event.stopPropagation(); - }} - onKeyDown={(event) => { - event.stopPropagation(); - if (event.key === 'Enter') { - event.preventDefault(); - commit(); - } - if (event.key === 'Escape') { - event.preventDefault(); - cancel(); - } - }} - className={cn( - 'min-w-0 flex-1 truncate select-text rounded-sm border border-primary/50 bg-background/80 px-1 py-0 text-sm font-medium outline-none ring-1 ring-primary/30', - className, - )} - style={style} - /> - ); -}; +export const SessionInlineRenameInput: React.FC = ( + props, +) => ( + +); diff --git a/components/ui/inline-rename-input.test.tsx b/components/ui/inline-rename-input.test.tsx new file mode 100644 index 000000000..ed1d0efc9 --- /dev/null +++ b/components/ui/inline-rename-input.test.tsx @@ -0,0 +1,114 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { act, create, type ReactTestRenderer } from "react-test-renderer"; + +import { HostTreeGroupInlineRenameInput } from "../host/HostTreeGroupInlineRenameInput"; +import { SessionInlineRenameInput } from "../terminal/SessionInlineRenameInput"; +import { VaultTreeInlineRenameInput } from "../vault/VaultTreeRow"; +import { InlineRenameInput } from "./inline-rename-input"; + +test("inline rename wrappers preserve exact data attributes", () => { + const groupMarkup = renderToStaticMarkup( + {}} onCancel={() => {}} />, + ); + assert.match(groupMarkup, /data-inline-group-edit="true"/); + assert.doesNotMatch(groupMarkup, /data-vault-tree-inline-edit/); + + const vaultTreeMarkup = renderToStaticMarkup( + {}} onCancel={() => {}} />, + ); + assert.match(vaultTreeMarkup, /data-vault-tree-inline-edit="true"/); + assert.match(vaultTreeMarkup, /data-inline-group-edit="true"/); + + const sessionMarkup = renderToStaticMarkup( + {}} onCancel={() => {}} />, + ); + assert.match(sessionMarkup, /data-session-inline-rename="true"/); +}); + +test("InlineRenameInput handles Enter, Escape, blur queueMicrotask, stopPropagation, and async retry", async () => { + const actEnv = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }; + const prevAct = actEnv.IS_REACT_ACT_ENVIRONMENT; + actEnv.IS_REACT_ACT_ENVIRONMENT = true; + let renderer: ReactTestRenderer | null = null; + + try { + // 1. Escape cancels once and suppresses subsequent blur commit. + let commits = 0; + let cancels = 0; + await act(async () => { + renderer = create( + { + commits += 1; + }} + onCancel={() => { + cancels += 1; + }} + />, + ); + }); + const input = renderer!.root.findByType("input"); + let stopped = 0; + let prevented = 0; + const stopPropagation = () => { stopped += 1; }; + const preventDefault = () => { prevented += 1; }; + input.props.onClick({ stopPropagation }); + input.props.onDoubleClick({ stopPropagation }); + input.props.onMouseDown({ stopPropagation }); + input.props.onPointerDown({ stopPropagation }); + input.props.onDragStart({ stopPropagation, preventDefault }); + assert.equal(stopped, 5); + assert.equal(prevented, 1); + + await act(async () => { + input.props.onKeyDown({ key: "Escape", stopPropagation, preventDefault }); + input.props.onBlur(); + await Promise.resolve(); + }); + assert.equal(cancels, 1); + assert.equal(commits, 0); + + // 2. Blur commits via queueMicrotask and resets submittingRef when onCommit rejects or returns false. + let blurAttempts = 0; + let committedValue = ""; + await act(async () => { + renderer!.unmount(); + renderer = create( + { + blurAttempts += 1; + committedValue = name; + if (blurAttempts === 1) return false; + if (blurAttempts === 2) throw new Error("transient"); + return true; + }} + onCancel={() => {}} + />, + ); + }); + const input2 = renderer!.root.findByType("input"); + await act(async () => { + input2.props.onChange({ target: { value: "gamma" } }); + }); + for (let i = 0; i < 4; i += 1) { + await act(async () => { + input2.props.onBlur(); + await Promise.resolve(); + await Promise.resolve(); + }); + } + // Attempts 1 (false) and 2 (throw) unlock; attempt 3 (true) locks so attempt 4 is skipped. + assert.equal(blurAttempts, 3); + assert.equal(committedValue, "gamma"); + } finally { + await act(async () => { + renderer?.unmount(); + }); + actEnv.IS_REACT_ACT_ENVIRONMENT = prevAct; + } +}); diff --git a/components/ui/inline-rename-input.tsx b/components/ui/inline-rename-input.tsx new file mode 100644 index 000000000..da7319586 --- /dev/null +++ b/components/ui/inline-rename-input.tsx @@ -0,0 +1,91 @@ +import React, { useEffect, useRef, useState } from "react"; + +import { cn } from "../../lib/utils"; + +type DataAttributes = { + [K in `data-${string}`]?: string | boolean | undefined; +}; + +export type InlineRenameInputProps = DataAttributes & { + initialName: string; + onCommit: (name: string) => boolean | void | Promise; + onCancel: () => void; + className?: string; + style?: React.CSSProperties; +}; + +export const InlineRenameInput: React.FC = ({ + initialName, + onCommit, + onCancel, + className, + style, + ...dataProps +}) => { + const inputRef = useRef(null); + const [value, setValue] = useState(initialName); + const submittingRef = useRef(false); + + useEffect(() => { + const input = inputRef.current; + if (!input) return; + input.focus(); + input.select(); + }, []); + + const commit = async () => { + if (submittingRef.current) return; + submittingRef.current = true; + try { + const committed = await onCommit(value); + if (committed === false) submittingRef.current = false; + } catch { + submittingRef.current = false; + } + }; + + const cancel = () => { + if (submittingRef.current) return; + submittingRef.current = true; + onCancel(); + }; + + return ( + setValue(event.target.value)} + onBlur={() => { + queueMicrotask(() => { + void commit(); + }); + }} + onClick={(event) => event.stopPropagation()} + onDoubleClick={(event) => event.stopPropagation()} + onMouseDown={(event) => event.stopPropagation()} + onPointerDown={(event) => event.stopPropagation()} + onDragStart={(event) => { + event.preventDefault(); + event.stopPropagation(); + }} + onKeyDown={(event) => { + event.stopPropagation(); + if (event.key === "Enter") { + event.preventDefault(); + void commit(); + } + if (event.key === "Escape") { + event.preventDefault(); + cancel(); + } + }} + className={cn( + "min-w-0 flex-1 truncate select-text rounded-sm border border-primary/50 bg-background/80 px-1 py-0 text-sm font-medium outline-none ring-1 ring-primary/30", + className, + )} + style={style} + /> + ); +}; diff --git a/components/vault/VaultHostCard.tsx b/components/vault/VaultHostCard.tsx new file mode 100644 index 000000000..9adbc5142 --- /dev/null +++ b/components/vault/VaultHostCard.tsx @@ -0,0 +1,287 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import React from "react"; + +import { hostCardFocusClassName } from "../../domain/hostClickBehavior"; +import { getVaultHostRowSubtitle } from "../../domain/localShellHost"; +import type { Host } from "../../domain/models"; +import { isPluginHostProtocol } from "../../domain/pluginConnection"; +import { isVaultHostDraggable } from "../../domain/vaultHostDrag"; +import { HostNotesIndicator } from "../host/HostNotesIndicator"; +import { OpenDualPaneSftpMenuItem, OpenHostBrowserMenuItem } from "../host/HostTreeContextMenus"; + +export type VaultHostCardSection = "pinned" | "recent" | "grouped" | "main"; + +export interface VaultHostCardPrimitives { + Badge: React.ComponentType; + CheckSquare: React.ComponentType; + ClipboardCopy: React.ComponentType; + cn: (...inputs: any[]) => string; + ContextMenu: React.ComponentType; + ContextMenuContent: React.ComponentType; + ContextMenuItem: React.ComponentType; + ContextMenuTrigger: React.ComponentType; + Copy: React.ComponentType; + DistroAvatar: React.ComponentType; + Edit2: React.ComponentType; + Pin: React.ComponentType; + Plug: React.ComponentType; + Square: React.ComponentType; + Star: React.ComponentType; + Trash2: React.ComponentType; +} + +export interface VaultHostContextMenuProps { + host: Host; + section: VaultHostCardSection; + primitives: Pick< + VaultHostCardPrimitives, + | "ClipboardCopy" + | "ContextMenuContent" + | "ContextMenuItem" + | "Copy" + | "Edit2" + | "Pin" + | "Plug" + | "Trash2" + >; + t: (key: string, params?: Record) => string; + onConnect: (host: Host) => void; + onOpenBrowser?: (host: Host) => void; + onEdit: (host: Host) => void; + onDuplicate: (host: Host) => void; + onCopyHostname: (host: Host) => void; + onCopyCredentials: (host: Host) => void; + onTogglePinned: (hostId: string) => void; + onDelete: (hostId: string) => void; +} + +export const VaultHostContextMenu: React.FC = ({ + host, + section, + primitives, + t, + onConnect, + onOpenBrowser, + onEdit, + onDuplicate, + onCopyHostname, + onCopyCredentials, + onTogglePinned, + onDelete, +}) => { + const { + ClipboardCopy, + ContextMenuContent, + ContextMenuItem, + Copy, + Edit2, + Pin, + Plug, + Trash2, + } = primitives; + const pinLabel = section === "pinned" || host.pinned + ? t("vault.hosts.unpin") + : t("vault.hosts.pinToTop"); + + return ( + + onConnect(host)}> + {t("vault.hosts.connect")} + + + + onEdit(host)}> + {t("action.edit")} + + onDuplicate(host)}> + {t("action.duplicate")} + + {!isPluginHostProtocol(host.protocol) ? ( + onCopyHostname(host)}> + {t("terminal.statusbar.copyHostname.label")} + + ) : null} + onCopyCredentials(host)}> + {t("vault.hosts.copyCredentials")} + + onTogglePinned(host.id)}> + {pinLabel} + + onDelete(host.id)}> + {t("action.delete")} + + + ); +}; + +export interface VaultHostCardProps { + host: Host; + section: VaultHostCardSection; + gridItemKey: string; + viewMode: "list" | "grid"; + isMultiSelectMode: boolean; + selectedHostIds: ReadonlySet; + draggingHostIds: ReadonlySet | null; + isFocusSelected: boolean; + tabIndex: number; + isPopInAnimated?: boolean; + onPopInAnimationEnd?: () => void; + sanitizeHost: (host: Host) => Host; + getEffectiveHostDistro: (host: Host) => string | undefined; + renderEditButton: (host: Host, compact?: boolean) => React.ReactNode; + onDragStart: (e: React.DragEvent, hostId: string) => void; + onActivate: (safeHost: Host) => void; + onConnect: (host: Host) => void; + onOpenBrowser?: (host: Host) => void; + onEdit: (host: Host) => void; + onDuplicate: (host: Host) => void; + onCopyHostname: (host: Host) => void; + onCopyCredentials: (host: Host) => void; + onTogglePinned: (hostId: string) => void; + onDelete: (hostId: string) => void; + t: (key: string, params?: Record) => string; + primitives: VaultHostCardPrimitives; +} + +export const VaultHostCard: React.FC = ({ + host, + section, + gridItemKey, + viewMode, + isMultiSelectMode, + selectedHostIds, + draggingHostIds, + isFocusSelected, + tabIndex, + isPopInAnimated = false, + onPopInAnimationEnd, + sanitizeHost, + getEffectiveHostDistro, + renderEditButton, + onDragStart, + onActivate, + onConnect, + onOpenBrowser, + onEdit, + onDuplicate, + onCopyHostname, + onCopyCredentials, + onTogglePinned, + onDelete, + t, + primitives, +}) => { + const { + Badge, + CheckSquare, + cn, + ContextMenu, + ContextMenuTrigger, + DistroAvatar, + Square, + Star, + } = primitives; + + const safeHost = sanitizeHost(host); + const effectiveDistro = getEffectiveHostDistro(safeHost); + const distroBadge = { + text: (safeHost.os || "L")[0].toUpperCase(), + label: effectiveDistro || safeHost.os || "Linux", + }; + const showGridStar = viewMode === "grid" + && (section === "pinned" || (section !== "recent" && Boolean(host.pinned))); + const showManagedBadge = (section === "grouped" || section === "main") + && Boolean(safeHost.managedSourceId); + const checkboxAriaHidden = section === "grouped" || section === "main" ? true : undefined; + + return ( + + +
onDragStart(e, host.id)} + onClick={() => { + onActivate(safeHost); + }} + onKeyDown={(event) => { + if (event.key !== "Enter" && event.key !== " ") return; + event.preventDefault(); + onActivate(safeHost); + }} + > + {showGridStar && ( + + )} +
+ {isMultiSelectMode && ( +
+ {selectedHostIds.has(host.id) ? ( + + ) : ( + + )} +
+ )} + +
+
+ + {safeHost.label} + + {viewMode !== "grid" && renderEditButton(host, true)} + {showManagedBadge && ( + + managed + + )} + +
+
+ {getVaultHostRowSubtitle(safeHost, { localFallback: t("localShell.subtitle") })} +
+
+ {viewMode === "grid" && renderEditButton(host)} +
+
+
+ +
+ ); +}; diff --git a/components/vault/VaultHostListSection.test.tsx b/components/vault/VaultHostListSection.test.tsx index 6201922e5..91cdd3a17 100644 --- a/components/vault/VaultHostListSection.test.tsx +++ b/components/vault/VaultHostListSection.test.tsx @@ -517,15 +517,62 @@ test("VaultHostListSection preserves grouped totals while virtualizing rendered }); test("VaultHostListSection exposes hostname copy in host context menus, not as a hover button", () => { - const source = readFileSync(new URL("./VaultHostListSection.tsx", import.meta.url), "utf8"); - assert.doesNotMatch(source, /data-vault-host-copy-hostname-button/); - assert.doesNotMatch(source, /renderHostCopyHostnameButton/); - assert.match(source, /terminal\.statusbar\.copyHostname\.label/); - assert.match(source, /handleCopyHostname\(host\)/); - assert.match(source, /!isPluginHostProtocol\(host\.protocol\)/); + const sectionSource = readFileSync(new URL("./VaultHostListSection.tsx", import.meta.url), "utf8"); + const cardSource = readFileSync(new URL("./VaultHostCard.tsx", import.meta.url), "utf8"); + const combinedSource = `${sectionSource}\n${cardSource}`; + assert.doesNotMatch(combinedSource, /data-vault-host-copy-hostname-button/); + assert.doesNotMatch(combinedSource, /renderHostCopyHostnameButton/); + assert.match(cardSource, /terminal\.statusbar\.copyHostname\.label/); + assert.match(cardSource, /onCopyHostname\(host\)/); + assert.match(cardSource, /!isPluginHostProtocol\(host\.protocol\)/); }); test("VaultHostListSection opens dual-pane SFTP from host context menus", () => { - const source = readFileSync(new URL("./VaultHostListSection.tsx", import.meta.url), "utf8"); - assert.match(source, /OpenDualPaneSftpMenuItem/); + const cardSource = readFileSync(new URL("./VaultHostCard.tsx", import.meta.url), "utf8"); + assert.match(cardSource, /OpenDualPaneSftpMenuItem/); +}); + +test("VaultHostListSection preserves section-specific card differences across pinned, recent, grouped, and main", () => { + const managedMain = { ...makeHost("managed-main", "Managed Main"), managedSourceId: "src-1", pinned: true }; + const ungroupedHost = { ...makeHost("ungrouped-host", "Ungrouped Host"), managedSourceId: "src-2" }; + + const gridMarkup = renderHostList({ + viewMode: "grid", + displayedGroups: [], + displayedHosts: [managedMain, pinnedHost, recentHost], + pinnedHosts: [pinnedHost], + recentHosts: [{ ...recentHost, pinned: true }], + showRecentHosts: true, + visibleDisplayedHosts: [managedMain], + isMultiSelectMode: true, + selectedHostIds: new Set([managedMain.id]), + }); + + // Pinned card shows star even when host.pinned is undefined, no managed badge, checkbox wrapper has no aria-hidden. + const pinnedIdx = gridMarkup.indexOf('data-vault-grid-item="pinned:pinned-host"'); + const recentIdx = gridMarkup.indexOf('data-vault-grid-item="recent:recent-host"'); + const mainIdx = gridMarkup.indexOf('data-vault-grid-item="main:managed-main"'); + assert.ok(pinnedIdx >= 0 && recentIdx > pinnedIdx && mainIdx > recentIdx); + const pinnedSlice = gridMarkup.slice(pinnedIdx, recentIdx); + const recentSlice = gridMarkup.slice(recentIdx, mainIdx); + const mainSlice = gridMarkup.slice(mainIdx); + + assert.match(pinnedSlice, /text-amber-400 fill-amber-400/); + assert.match(pinnedSlice, /
]*lucide-square/); + assert.doesNotMatch(pinnedSlice, /