Skip to content

Commit 26c48bb

Browse files
t3dotggclaude
andauthored
fix(web): action shortcuts recorded in the browser now save (#19365)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 40d43a1 commit 26c48bb

3 files changed

Lines changed: 148 additions & 6 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,7 +5172,6 @@ export default function ChatView(props: ChatViewProps) {
51725172
(script) => commandForProjectScript(script.id) === input.keybindingCommand,
51735173
);
51745174
const changesKeybinding =
5175-
isElectron &&
51765175
input.keybinding !== undefined &&
51775176
(input.keybinding?.trim() || null) !== previousKeybinding &&
51785177
(!isDeletingScript || readEnvironmentScope(environmentId, AuthSettingsWriteScope));
@@ -5234,8 +5233,6 @@ export default function ChatView(props: ChatViewProps) {
52345233
);
52355234
}
52365235

5237-
if (!isElectron) return updateResult;
5238-
52395236
const scriptId = input.keybindingCommand
52405237
? projectScriptIdFromCommand(input.keybindingCommand)
52415238
: null;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import {
2+
DEFAULT_SERVER_SETTINGS,
3+
EnvironmentId,
4+
ProjectId,
5+
type ProjectScript,
6+
type ResolvedKeybindingsConfig,
7+
} from "@t3tools/contracts";
8+
import { AsyncResult } from "effect/reactivity";
9+
import { act, create, type ReactTestRenderer } from "react-test-renderer";
10+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
11+
12+
import type { NewProjectScriptInput } from "../projectScriptEditor";
13+
14+
const commands = vi.hoisted(() => ({
15+
updateSettings: vi.fn(),
16+
upsertKeybinding: vi.fn(),
17+
removeKeybinding: vi.fn(),
18+
}));
19+
20+
// A browser client: the shortcut must still reach the environment's keybindings.
21+
vi.mock("../../env", () => ({ isElectron: false }));
22+
vi.mock("../../state/session", () => ({ readEnvironmentScope: () => true }));
23+
vi.mock("../../state/entities", () => ({ useProjects: () => [] }));
24+
vi.mock("../../state/server", () => ({
25+
serverEnvironment: {
26+
updateSettings: "updateSettings",
27+
upsertKeybinding: "upsertKeybinding",
28+
removeKeybinding: "removeKeybinding",
29+
},
30+
}));
31+
vi.mock("../../state/use-atom-command", () => ({
32+
useAtomCommand: (command: keyof typeof commands) => commands[command],
33+
}));
34+
vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
35+
36+
import { useProjectScriptSettings } from "./useProjectScriptSettings";
37+
38+
const environmentId = EnvironmentId.make("remote");
39+
const lint: ProjectScript = {
40+
id: "lint",
41+
name: "Lint",
42+
command: "vp lint",
43+
icon: "lint",
44+
runOnWorktreeCreate: false,
45+
};
46+
const lintBinding: ResolvedKeybindingsConfig = [
47+
{
48+
command: "script.lint.run",
49+
shortcut: {
50+
key: "k",
51+
metaKey: false,
52+
ctrlKey: false,
53+
shiftKey: true,
54+
altKey: false,
55+
modKey: true,
56+
},
57+
},
58+
];
59+
const input = (keybinding: string | null): NewProjectScriptInput => ({
60+
name: lint.name,
61+
command: lint.command,
62+
icon: lint.icon,
63+
runOnWorktreeCreate: false,
64+
waitForSetup: false,
65+
runOnSettle: false,
66+
keybinding,
67+
previewUrl: null,
68+
autoOpenPreview: false,
69+
});
70+
71+
type ProjectScriptSettings = ReturnType<typeof useProjectScriptSettings>;
72+
73+
function Probe(props: {
74+
keybindings: ResolvedKeybindingsConfig;
75+
onResult: (settings: ProjectScriptSettings) => void;
76+
}) {
77+
props.onResult(
78+
useProjectScriptSettings([
79+
{
80+
environmentId,
81+
settings: DEFAULT_SERVER_SETTINGS,
82+
keybindings: props.keybindings,
83+
project: { id: ProjectId.make("project"), scripts: [lint] },
84+
},
85+
]),
86+
);
87+
return null;
88+
}
89+
90+
let renderer: ReactTestRenderer | undefined;
91+
92+
function renderSettings(keybindings: ResolvedKeybindingsConfig) {
93+
let settings: ProjectScriptSettings | undefined;
94+
act(() => {
95+
renderer = create(<Probe keybindings={keybindings} onResult={(next) => (settings = next)} />);
96+
});
97+
return settings!;
98+
}
99+
100+
async function submitInAct(submit: ProjectScriptSettings["submit"], keybinding: string | null) {
101+
let result: Awaited<ReturnType<typeof submit>> | undefined;
102+
await act(async () => {
103+
result = await submit(lint.id, input(keybinding));
104+
});
105+
return result!;
106+
}
107+
108+
beforeEach(() => {
109+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
110+
vi.clearAllMocks();
111+
for (const command of Object.values(commands)) {
112+
command.mockResolvedValue(AsyncResult.success(undefined));
113+
}
114+
});
115+
116+
afterEach(() => {
117+
act(() => renderer?.unmount());
118+
renderer = undefined;
119+
vi.unstubAllGlobals();
120+
});
121+
122+
describe("useProjectScriptSettings in a browser client", () => {
123+
it("saves a recorded action shortcut", async () => {
124+
const { submit } = renderSettings([]);
125+
126+
const result = await submitInAct(submit, "mod+shift+k");
127+
128+
expect(result._tag).toBe("Success");
129+
expect(commands.upsertKeybinding).toHaveBeenCalledWith({
130+
environmentId,
131+
input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
132+
});
133+
});
134+
135+
it("removes a cleared action shortcut", async () => {
136+
const { submit } = renderSettings(lintBinding);
137+
138+
const result = await submitInAct(submit, null);
139+
140+
expect(result._tag).toBe("Success");
141+
expect(commands.removeKeybinding).toHaveBeenCalledWith({
142+
environmentId,
143+
input: expect.objectContaining({ key: "mod+shift+k", command: "script.lint.run" }),
144+
});
145+
expect(commands.upsertKeybinding).not.toHaveBeenCalled();
146+
});
147+
});

‎apps/web/src/components/settings/useProjectScriptSettings.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as Cause from "effect/Cause";
1919
import { AsyncResult } from "effect/reactivity";
2020
import { useRef, useState } from "react";
2121

22-
import { isElectron } from "../../env";
2322
import {
2423
decodeProjectScriptKeybindingRule,
2524
keybindingValueForCommand,
@@ -50,7 +49,7 @@ function reportScriptFailure(result: AtomCommandResult<unknown, unknown>) {
5049

5150
/**
5251
* Edits the action list on every target: the environment default when there is
53-
* no project, else that project's override entry. Shortcuts follow on desktop.
52+
* no project, else that project's override entry, then saves changed shortcuts.
5453
*/
5554
export function useProjectScriptSettings(
5655
targets: readonly {
@@ -129,7 +128,6 @@ export function useProjectScriptSettings(
129128
},
130129
});
131130
if (result._tag === "Failure") return reportScriptFailure(result);
132-
if (!isElectron) continue;
133131
const changedIds = scriptId
134132
? [scriptId]
135133
: current

0 commit comments

Comments
 (0)