Skip to content

Commit 8ef3e1c

Browse files
committed
Add configurable thread rename shortcut
1 parent 7d1abed commit 8ef3e1c

15 files changed

Lines changed: 483 additions & 109 deletions

File tree

‎apps/app/src/components/settings/KeyboardSettingsSection.test.tsx‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
within,
99
} from "@testing-library/react";
1010
import { afterEach, describe, expect, it, vi } from "vitest";
11-
import { defaultAppSettings, type AppKeybindings } from "@bb/domain";
11+
import { defaultAppSettings, type AppDefaultKeybindings } from "@bb/domain";
1212
import { KeyboardSettingsSection } from "./KeyboardSettingsSection";
1313

1414
const testState = vi.hoisted(() => ({
@@ -39,6 +39,12 @@ const testState = vi.hoisted(() => ({
3939
},
4040
when: { all: ["mainSurface"], none: ["modalOpen"] },
4141
},
42+
{
43+
command: "thread.rename",
44+
desktopOnly: false,
45+
shortcut: null,
46+
when: { all: ["mainSurface"], none: ["modalOpen"] },
47+
},
4248
{
4349
command: "thread.jump.1",
4450
desktopOnly: false,
@@ -100,7 +106,7 @@ const testState = vi.hoisted(() => ({
100106
none: ["modalOpen", "editableFocus"],
101107
},
102108
},
103-
] as AppKeybindings,
109+
] as AppDefaultKeybindings,
104110
generalMutate: vi.fn(),
105111
isDesktop: false,
106112
mutate: vi.fn(),
@@ -222,6 +228,39 @@ describe("KeyboardSettingsSection", () => {
222228
);
223229
});
224230

231+
it("lets users assign a command that has no default shortcut", () => {
232+
render(<KeyboardSettingsSection />);
233+
const recorder = screen.getByRole("button", {
234+
name: "Record shortcut for Rename thread, current shortcut unassigned",
235+
});
236+
expect(recorder.hasAttribute("disabled")).toBe(false);
237+
expect(within(recorder).getByText("Unassigned")).toBeDefined();
238+
239+
fireEvent.click(recorder);
240+
fireEvent.keyDown(recorder, {
241+
key: "R",
242+
ctrlKey: true,
243+
shiftKey: true,
244+
});
245+
246+
expect(testState.mutate).toHaveBeenLastCalledWith(
247+
[
248+
{
249+
command: "thread.rename",
250+
shortcut: {
251+
key: "r",
252+
mod: true,
253+
meta: false,
254+
control: false,
255+
alt: false,
256+
shift: true,
257+
},
258+
},
259+
],
260+
expect.objectContaining({ onError: expect.any(Function) }),
261+
);
262+
});
263+
225264
it("shows web and desktop defaults without OS-specific groups", () => {
226265
vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
227266
render(<KeyboardSettingsSection />);

‎apps/app/src/components/settings/KeyboardSettingsSection.tsx‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { useMemo, useState, type KeyboardEvent } from "react";
22
import {
33
defaultAppSettings,
44
type AppCommandId,
5+
type AppDefaultKeybindings,
56
type AppKeybindingOverrides,
6-
type AppKeybindings,
77
type AppShortcut,
88
} from "@bb/domain";
99
import { Button } from "@bb/shared-ui/button";
@@ -21,6 +21,8 @@ import {
2121
canAssignAppShortcut,
2222
getCommandShortcut,
2323
getShortcutConflicts,
24+
isAppCommandAvailableForClient,
25+
resetCommandShortcutOverride,
2426
setCommandShortcutOverride,
2527
} from "@/lib/keyboard-shortcut-settings";
2628
import {
@@ -41,7 +43,7 @@ import {
4143
import { AppCommandShortcutPill } from "@/components/commands/AppCommandShortcutHint";
4244
import { getBbDesktopInfo } from "@/lib/bb-desktop";
4345

44-
const EMPTY_KEYBINDINGS: AppKeybindings = [];
46+
const EMPTY_KEYBINDINGS: AppDefaultKeybindings = [];
4547
const EMPTY_OVERRIDES: AppKeybindingOverrides = [];
4648
const SETTINGS_SHORTCUT_PILL_CLASS =
4749
"rounded-none bg-transparent px-0 py-0 text-foreground opacity-100";
@@ -160,10 +162,11 @@ function ShortcutRecorder({
160162

161163
interface KeyboardCommandRowProps {
162164
command: AppCommandId;
163-
defaults: AppKeybindings;
165+
defaults: AppDefaultKeybindings;
164166
disabled: boolean;
165167
isDesktop: boolean;
166168
onChange(command: AppCommandId, shortcut: AppShortcut | null): void;
169+
onReset(command: AppCommandId): void;
167170
onRecordingChange(command: AppCommandId | null): void;
168171
overrides: AppKeybindingOverrides;
169172
recordingCommand: AppCommandId | null;
@@ -176,6 +179,7 @@ function KeyboardCommandRow({
176179
disabled,
177180
isDesktop,
178181
onChange,
182+
onReset,
179183
onRecordingChange,
180184
overrides,
181185
platform,
@@ -207,10 +211,12 @@ function KeyboardCommandRow({
207211
true,
208212
platform,
209213
);
210-
const activeDefaultShortcut = isDesktop
211-
? desktopDefaultShortcut
212-
: webDefaultShortcut;
213-
const availableOnClient = activeDefaultShortcut !== null;
214+
const availableOnClient = isAppCommandAvailableForClient(
215+
defaults,
216+
command,
217+
isDesktop,
218+
platform,
219+
);
214220
const splitDefaults =
215221
webDefaultShortcut !== null &&
216222
desktopDefaultShortcut !== null &&
@@ -309,16 +315,7 @@ function KeyboardCommandRow({
309315
aria-label={`Reset shortcut for ${metadata.label}`}
310316
className="size-7"
311317
disabled={disabled || !availableOnClient || !customized}
312-
onClick={() => {
313-
const defaultShortcut = getCommandShortcut(
314-
defaults,
315-
[],
316-
command,
317-
isDesktop,
318-
platform,
319-
);
320-
if (defaultShortcut !== null) onChange(command, defaultShortcut);
321-
}}
318+
onClick={() => onReset(command)}
322319
size="icon"
323320
type="button"
324321
variant="ghost"
@@ -384,6 +381,16 @@ export function KeyboardSettingsSection() {
384381
});
385382
}
386383

384+
function resetCommand(command: AppCommandId) {
385+
const previous = overrides;
386+
const next = resetCommandShortcutOverride(overrides, command);
387+
setDraft({ sourceKey: serverOverridesKey, value: next });
388+
updateKeyboardSettings.mutate(next, {
389+
onError: () =>
390+
setDraft({ sourceKey: serverOverridesKey, value: previous }),
391+
});
392+
}
393+
387394
const disabled =
388395
systemConfig.data === undefined || updateKeyboardSettings.isPending;
389396
const hasOverrides = overrides.length > 0;
@@ -451,6 +458,7 @@ export function KeyboardSettingsSection() {
451458
key={metadata.command}
452459
onChange={updateCommand}
453460
onRecordingChange={setRecordingCommand}
461+
onReset={resetCommand}
454462
overrides={overrides}
455463
platform={platform}
456464
recordingCommand={recordingCommand}

‎apps/app/src/lib/app-command-metadata.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const APP_COMMAND_GROUPS: readonly AppCommandGroup[] = [
3838
"Search threads",
3939
"Focus the sidebar thread search.",
4040
),
41+
command("thread.rename", "Rename thread", "Rename the focused thread."),
4142
command(
4243
"thread.previous",
4344
"Previous thread",

‎apps/app/src/lib/keyboard-shortcut-settings.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { describe, expect, it } from "vitest";
2-
import { APP_COMMAND_IDS, type AppKeybindings } from "@bb/domain";
2+
import {
3+
APP_COMMAND_IDS,
4+
type AppDefaultKeybindings,
5+
type AppKeybindings,
6+
} from "@bb/domain";
37
import { getAppCommandMetadata } from "./app-command-metadata";
48
import {
59
appShortcutFromInput,
610
canAssignAppShortcut,
711
getCommandShortcut,
12+
resetCommandShortcutOverride,
813
setCommandShortcutOverride,
914
} from "./keyboard-shortcut-settings";
1015

@@ -144,6 +149,38 @@ describe("keyboard shortcut settings", () => {
144149
).toEqual([]);
145150
});
146151

152+
it("assigns and resets a command without a default shortcut", () => {
153+
const unassignedDefaults: AppDefaultKeybindings = [
154+
{
155+
command: "thread.rename",
156+
desktopOnly: false,
157+
shortcut: null,
158+
when: { all: ["mainSurface"], none: ["modalOpen"] },
159+
},
160+
];
161+
const shortcut = defaults[0]!.shortcut;
162+
const assigned = setCommandShortcutOverride(
163+
unassignedDefaults,
164+
[],
165+
"thread.rename",
166+
shortcut,
167+
false,
168+
"Win32",
169+
);
170+
171+
expect(assigned).toEqual([{ command: "thread.rename", shortcut }]);
172+
expect(
173+
getCommandShortcut(
174+
unassignedDefaults,
175+
assigned,
176+
"thread.rename",
177+
false,
178+
"Win32",
179+
),
180+
).toEqual(shortcut);
181+
expect(resetCommandShortcutOverride(assigned, "thread.rename")).toEqual([]);
182+
});
183+
147184
it("selects the active default for the current app surface", () => {
148185
expect(
149186
getCommandShortcut(defaults, [], "thread.new", false, "Win32"),

‎apps/app/src/lib/keyboard-shortcut-settings.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
isMacKeyboardPlatform,
66
normalizeAppShortcutInputKey,
77
type AppCommandId,
8+
type AppDefaultKeybindings,
89
type AppKeybindingOverrides,
9-
type AppKeybindings,
1010
type AppShortcut,
1111
type AppShortcutInput,
1212
} from "@bb/domain";
@@ -73,31 +73,47 @@ export function canAssignAppShortcut(
7373
}
7474

7575
export function getCommandShortcut(
76-
defaults: AppKeybindings,
76+
defaults: AppDefaultKeybindings,
7777
overrides: AppKeybindingOverrides,
7878
command: AppCommandId,
7979
isDesktop: boolean,
8080
platform: string,
8181
): AppShortcut | null {
8282
const isMac = isMacKeyboardPlatform(platform);
8383
let defaultShortcut: AppShortcut | null = null;
84+
let available = false;
8485
for (let index = defaults.length - 1; index >= 0; index -= 1) {
8586
const binding = defaults[index];
8687
if (
8788
binding?.command === command &&
8889
isAppKeybindingAvailableForClient(binding, { isDesktop, isMac })
8990
) {
91+
available = true;
9092
defaultShortcut = binding.shortcut;
9193
break;
9294
}
9395
}
94-
if (defaultShortcut === null) return null;
96+
if (!available) return null;
9597
const override = overrides.find((candidate) => candidate.command === command);
9698
return override === undefined ? defaultShortcut : override.shortcut;
9799
}
98100

101+
export function isAppCommandAvailableForClient(
102+
defaults: AppDefaultKeybindings,
103+
command: AppCommandId,
104+
isDesktop: boolean,
105+
platform: string,
106+
): boolean {
107+
const isMac = isMacKeyboardPlatform(platform);
108+
return defaults.some(
109+
(binding) =>
110+
binding.command === command &&
111+
isAppKeybindingAvailableForClient(binding, { isDesktop, isMac }),
112+
);
113+
}
114+
99115
export function setCommandShortcutOverride(
100-
defaults: AppKeybindings,
116+
defaults: AppDefaultKeybindings,
101117
overrides: AppKeybindingOverrides,
102118
command: AppCommandId,
103119
shortcut: AppShortcut | null,
@@ -129,8 +145,15 @@ export function setCommandShortcutOverride(
129145
});
130146
}
131147

148+
export function resetCommandShortcutOverride(
149+
overrides: AppKeybindingOverrides,
150+
command: AppCommandId,
151+
): AppKeybindingOverrides {
152+
return overrides.filter((override) => override.command !== command);
153+
}
154+
132155
export function getShortcutConflicts(
133-
defaults: AppKeybindings,
156+
defaults: AppDefaultKeybindings,
134157
overrides: AppKeybindingOverrides,
135158
command: AppCommandId,
136159
isDesktop: boolean,

‎apps/app/src/views/thread-detail/ThreadDetailView.tsx‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ import {
230230
import { useRouteState } from "@/hooks/useRouteState";
231231
import { useAppCommandHandler } from "@/components/commands/AppCommandProvider";
232232
import { DefaultPaneContextProvider, usePaneContext } from "./PaneContext";
233+
import { ThreadRenameCommandHandler } from "./ThreadRenameCommandHandler";
233234

234235
const EMPTY_PARENT_THREADS: readonly ThreadListEntry[] = [];
235236
const EMPTY_PROJECT_THREAD_SUBSET_FILTERS =
@@ -2635,10 +2636,13 @@ function ThreadDetailViewInternal(props: ThreadDetailViewInternalProps) {
26352636
</MarkdownLocalFileContextMenuContext.Provider>
26362637
);
26372638
return (
2638-
<PluginThreadPanelNavigationProvider
2639-
openThreadPanel={handleOpenTimelinePluginPanel}
2640-
>
2641-
{threadDetailContent}
2642-
</PluginThreadPanelNavigationProvider>
2639+
<>
2640+
<ThreadRenameCommandHandler thread={thread} />
2641+
<PluginThreadPanelNavigationProvider
2642+
openThreadPanel={handleOpenTimelinePluginPanel}
2643+
>
2644+
{threadDetailContent}
2645+
</PluginThreadPanelNavigationProvider>
2646+
</>
26432647
);
26442648
}

0 commit comments

Comments
 (0)