Skip to content
TypeScript

fix(web): clear stale chat action shortcuts (#15394)

maria2d81e4c27559
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 8c4c0bb1a45..5ef916db839 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -294,7 +294,10 @@ import {
import { cn, randomUUID } from "~/lib/utils";
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
import { stackedThreadToast, toastManager } from "./ui/toast";
-import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
+import {
+ decodeProjectScriptKeybindingRule,
+ keybindingValueForCommand,
+} from "~/lib/projectScriptKeybindings";
import { type NewProjectScriptInput } from "./ProjectScriptsControl";
import {
buildProjectScript,
@@ -1534,6 +1537,9 @@ export default function ChatView(props: ChatViewProps) {
const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, {
reportFailure: false,
});
+ const removeKeybinding = useAtomCommand(serverEnvironment.removeKeybinding, {
+ reportFailure: false,
+ });
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
@@ -4940,20 +4946,64 @@ export default function ChatView(props: ChatViewProps) {
command: input.keybindingCommand,
});
- if (isElectron && keybindingRule) {
- return mapAtomCommandResult(
- await upsertKeybinding({
- environmentId,
- input: keybindingRule,
- }),
- () => undefined,
- );
+ if (!isElectron) return updateResult;
+
+ const scriptId = input.keybindingCommand
+ ? projectScriptIdFromCommand(input.keybindingCommand)
+ : null;
+ if (!keybindingRule && !input.previousScripts.some((script) => script.id === scriptId)) {
+ return updateResult;
}
- return updateResult;
+ const retainedElsewhere =
+ !input.nextScripts.some((script) => script.id === scriptId) &&
+ (settings.defaultProjectScripts.some((script) => script.id === scriptId) ||
+ Object.entries(settings.projectSettingsOverrides).some(
+ ([projectId, entry]) =>
+ projectId !== input.projectId &&
+ entry.defaultProjectScripts?.some((script) => script.id === scriptId),
+ ) ||
+ allProjects.some(
+ (other) =>
+ other.environmentId === environmentId &&
+ other.id !== input.projectId &&
+ resolveProjectScripts(settings, other).some((script) => script.id === scriptId),
+ ));
+ if (!keybindingRule && retainedElsewhere) return updateResult;
+
+ const previousRules = (
+ environmentById.get(environmentId)?.serverConfig?.keybindings ?? []
+ ).flatMap((binding) => {
+ if (binding.command !== input.keybindingCommand || binding.whenAst) return [];
+ const previous = decodeProjectScriptKeybindingRule({
+ keybinding: keybindingValueForCommand([binding], input.keybindingCommand),
+ command: input.keybindingCommand,
+ });
+ return previous ? [previous] : [];
+ });
+ const previous = previousRules.at(-1);
+ for (const rule of keybindingRule ? previousRules.slice(0, -1) : previousRules) {
+ const result = await removeKeybinding({ environmentId, input: rule });
+ if (result._tag === "Failure") return mapAtomCommandResult(result, () => undefined);
+ }
+ return keybindingRule
+ ? mapAtomCommandResult(
+ await upsertKeybinding({
+ environmentId,
+ input:
+ previous && previous.key !== keybindingRule.key
+ ? { ...keybindingRule, replace: previous }
+ : keybindingRule,
+ }),
+ () => undefined,
+ )
+ : updateResult;
},
[
+ allProjects,
+ environmentById,
environmentId,
- settings.projectSettingsOverrides,
+ removeKeybinding,
+ settings,
supportsProjectSettingsOverrides,
updateProjectScriptSettings,
upsertKeybinding,