Skip to content

Commit 2d81e4c

Browse files
authored
fix(web): clear stale chat action shortcuts (#15394)
1 parent d307127 commit 2d81e4c

1 file changed

Lines changed: 61 additions & 11 deletions

File tree

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

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ import {
294294
import { cn, randomUUID } from "~/lib/utils";
295295
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
296296
import { stackedThreadToast, toastManager } from "./ui/toast";
297-
import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
297+
import {
298+
decodeProjectScriptKeybindingRule,
299+
keybindingValueForCommand,
300+
} from "~/lib/projectScriptKeybindings";
298301
import { type NewProjectScriptInput } from "./ProjectScriptsControl";
299302
import {
300303
buildProjectScript,
@@ -1534,6 +1537,9 @@ export default function ChatView(props: ChatViewProps) {
15341537
const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, {
15351538
reportFailure: false,
15361539
});
1540+
const removeKeybinding = useAtomCommand(serverEnvironment.removeKeybinding, {
1541+
reportFailure: false,
1542+
});
15371543
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
15381544
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
15391545
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
@@ -4940,20 +4946,64 @@ export default function ChatView(props: ChatViewProps) {
49404946
command: input.keybindingCommand,
49414947
});
49424948

4943-
if (isElectron && keybindingRule) {
4944-
return mapAtomCommandResult(
4945-
await upsertKeybinding({
4946-
environmentId,
4947-
input: keybindingRule,
4948-
}),
4949-
() => undefined,
4950-
);
4949+
if (!isElectron) return updateResult;
4950+
4951+
const scriptId = input.keybindingCommand
4952+
? projectScriptIdFromCommand(input.keybindingCommand)
4953+
: null;
4954+
if (!keybindingRule && !input.previousScripts.some((script) => script.id === scriptId)) {
4955+
return updateResult;
49514956
}
4952-
return updateResult;
4957+
const retainedElsewhere =
4958+
!input.nextScripts.some((script) => script.id === scriptId) &&
4959+
(settings.defaultProjectScripts.some((script) => script.id === scriptId) ||
4960+
Object.entries(settings.projectSettingsOverrides).some(
4961+
([projectId, entry]) =>
4962+
projectId !== input.projectId &&
4963+
entry.defaultProjectScripts?.some((script) => script.id === scriptId),
4964+
) ||
4965+
allProjects.some(
4966+
(other) =>
4967+
other.environmentId === environmentId &&
4968+
other.id !== input.projectId &&
4969+
resolveProjectScripts(settings, other).some((script) => script.id === scriptId),
4970+
));
4971+
if (!keybindingRule && retainedElsewhere) return updateResult;
4972+
4973+
const previousRules = (
4974+
environmentById.get(environmentId)?.serverConfig?.keybindings ?? []
4975+
).flatMap((binding) => {
4976+
if (binding.command !== input.keybindingCommand || binding.whenAst) return [];
4977+
const previous = decodeProjectScriptKeybindingRule({
4978+
keybinding: keybindingValueForCommand([binding], input.keybindingCommand),
4979+
command: input.keybindingCommand,
4980+
});
4981+
return previous ? [previous] : [];
4982+
});
4983+
const previous = previousRules.at(-1);
4984+
for (const rule of keybindingRule ? previousRules.slice(0, -1) : previousRules) {
4985+
const result = await removeKeybinding({ environmentId, input: rule });
4986+
if (result._tag === "Failure") return mapAtomCommandResult(result, () => undefined);
4987+
}
4988+
return keybindingRule
4989+
? mapAtomCommandResult(
4990+
await upsertKeybinding({
4991+
environmentId,
4992+
input:
4993+
previous && previous.key !== keybindingRule.key
4994+
? { ...keybindingRule, replace: previous }
4995+
: keybindingRule,
4996+
}),
4997+
() => undefined,
4998+
)
4999+
: updateResult;
49535000
},
49545001
[
5002+
allProjects,
5003+
environmentById,
49555004
environmentId,
4956-
settings.projectSettingsOverrides,
5005+
removeKeybinding,
5006+
settings,
49575007
supportsProjectSettingsOverrides,
49585008
updateProjectScriptSettings,
49595009
upsertKeybinding,

0 commit comments

Comments
 (0)