Skip to content

Commit bd25b29

Browse files
committed
🤖 refactor: extract createEditKeyHandler for inline edit inputs
DRY helper for Enter-to-save, Escape-to-cancel pattern with stopPropagation.
1 parent e7bd15d commit bd25b29

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

src/browser/components/Settings/sections/ModelRow.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import { Check, Pencil, Star, Trash2, X } from "lucide-react";
3+
import { createEditKeyHandler } from "@/browser/utils/ui/keybinds";
34
import { GatewayIcon } from "@/browser/components/icons/GatewayIcon";
45
import { cn } from "@/common/lib/utils";
56
import { TooltipWrapper, Tooltip } from "@/browser/components/Tooltip";
@@ -45,13 +46,10 @@ export function ModelRow(props: ModelRowProps) {
4546
type="text"
4647
value={props.editValue ?? props.modelId}
4748
onChange={(e) => props.onEditChange?.(e.target.value)}
48-
onKeyDown={(e) => {
49-
if (e.key === "Enter") props.onSaveEdit?.();
50-
if (e.key === "Escape") {
51-
e.stopPropagation();
52-
props.onCancelEdit?.();
53-
}
54-
}}
49+
onKeyDown={createEditKeyHandler({
50+
onSave: () => props.onSaveEdit?.(),
51+
onCancel: () => props.onCancelEdit?.(),
52+
})}
5553
className="bg-modal-bg border-border-medium focus:border-accent min-w-0 flex-1 rounded border px-2 py-0.5 font-mono text-xs focus:outline-none"
5654
autoFocus
5755
/>

src/browser/components/Settings/sections/ProjectSettingsSection.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Check,
1515
X,
1616
} from "lucide-react";
17+
import { createEditKeyHandler } from "@/browser/utils/ui/keybinds";
1718

1819
type TestResult = { success: true; tools: string[] } | { success: false; error: string };
1920

@@ -296,14 +297,10 @@ export const ProjectSettingsSection: React.FC = () => {
296297
className="border-border-medium bg-secondary/30 text-foreground placeholder:text-muted-foreground focus:ring-accent mt-1 w-full rounded-md border px-2 py-1 text-xs focus:ring-1 focus:outline-none"
297298
autoFocus
298299
spellCheck={false}
299-
onKeyDown={(e) => {
300-
if (e.key === "Enter") {
301-
void handleSaveEdit();
302-
} else if (e.key === "Escape") {
303-
e.stopPropagation();
304-
handleCancelEdit();
305-
}
306-
}}
300+
onKeyDown={createEditKeyHandler({
301+
onSave: () => void handleSaveEdit(),
302+
onCancel: handleCancelEdit,
303+
})}
307304
/>
308305
) : (
309306
<p className="text-muted-foreground mt-0.5 text-xs break-all">{command}</p>

src/browser/components/Settings/sections/ProvidersSection.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState, useCallback } from "react";
22
import { ChevronDown, ChevronRight, Check, X } from "lucide-react";
3+
import { createEditKeyHandler } from "@/browser/utils/ui/keybinds";
34
import { SUPPORTED_PROVIDERS } from "@/common/constants/providers";
45
import type { ProviderName } from "@/common/constants/providers";
56
import { ProviderWithIcon } from "@/browser/components/ProviderIcon";
@@ -262,13 +263,10 @@ export function ProvidersSection() {
262263
placeholder={fieldConfig.placeholder}
263264
className="bg-modal-bg border-border-medium focus:border-accent flex-1 rounded border px-2 py-1.5 font-mono text-xs focus:outline-none"
264265
autoFocus
265-
onKeyDown={(e) => {
266-
if (e.key === "Enter") handleSaveEdit();
267-
if (e.key === "Escape") {
268-
e.stopPropagation();
269-
handleCancelEdit();
270-
}
271-
}}
266+
onKeyDown={createEditKeyHandler({
267+
onSave: handleSaveEdit,
268+
onCancel: handleCancelEdit,
269+
})}
272270
/>
273271
<Button
274272
variant="ghost"

src/browser/utils/ui/keybinds.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,21 @@ export const KEYBINDS = {
296296
// "D" for Dictate - intuitive and available
297297
TOGGLE_VOICE_INPUT: { key: "d", ctrl: true },
298298
} as const;
299+
300+
/**
301+
* Create a keyboard event handler for inline edit inputs.
302+
* Handles Enter to save and Escape to cancel (with stopPropagation to prevent modal close).
303+
*/
304+
export function createEditKeyHandler(options: {
305+
onSave: () => void;
306+
onCancel: () => void;
307+
}): (e: React.KeyboardEvent) => void {
308+
return (e) => {
309+
if (e.key === "Enter") {
310+
options.onSave();
311+
} else if (e.key === "Escape") {
312+
e.stopPropagation();
313+
options.onCancel();
314+
}
315+
};
316+
}

0 commit comments

Comments
 (0)