Skip to content

Commit a33c08a

Browse files
committed
fix(web): clear stale chat action shortcuts
1 parent 00eb8f6 commit a33c08a

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
@@ -292,7 +292,10 @@ import {
292292
import { cn, randomUUID } from "~/lib/utils";
293293
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
294294
import { stackedThreadToast, toastManager } from "./ui/toast";
295-
import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
295+
import {
296+
decodeProjectScriptKeybindingRule,
297+
keybindingValueForCommand,
298+
} from "~/lib/projectScriptKeybindings";
296299
import { type NewProjectScriptInput } from "./ProjectScriptsControl";
297300
import {
298301
buildProjectScript,
@@ -1532,6 +1535,9 @@ export default function ChatView(props: ChatViewProps) {
15321535
const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, {
15331536
reportFailure: false,
15341537
});
1538+
const removeKeybinding = useAtomCommand(serverEnvironment.removeKeybinding, {
1539+
reportFailure: false,
1540+
});
15351541
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
15361542
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
15371543
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
@@ -4827,20 +4833,64 @@ export default function ChatView(props: ChatViewProps) {
48274833
command: input.keybindingCommand,
48284834
});
48294835

4830-
if (isElectron && keybindingRule) {
4831-
return mapAtomCommandResult(
4832-
await upsertKeybinding({
4833-
environmentId,
4834-
input: keybindingRule,
4835-
}),
4836-
() => undefined,
4837-
);
4836+
if (!isElectron) return updateResult;
4837+
4838+
const scriptId = input.keybindingCommand
4839+
? projectScriptIdFromCommand(input.keybindingCommand)
4840+
: null;
4841+
if (!keybindingRule && !input.previousScripts.some((script) => script.id === scriptId)) {
4842+
return updateResult;
48384843
}
4839-
return updateResult;
4844+
const retainedElsewhere =
4845+
!input.nextScripts.some((script) => script.id === scriptId) &&
4846+
(settings.defaultProjectScripts.some((script) => script.id === scriptId) ||
4847+
Object.entries(settings.projectSettingsOverrides).some(
4848+
([projectId, entry]) =>
4849+
projectId !== input.projectId &&
4850+
entry.defaultProjectScripts?.some((script) => script.id === scriptId),
4851+
) ||
4852+
allProjects.some(
4853+
(other) =>
4854+
other.environmentId === environmentId &&
4855+
other.id !== input.projectId &&
4856+
resolveProjectScripts(settings, other).some((script) => script.id === scriptId),
4857+
));
4858+
if (!keybindingRule && retainedElsewhere) return updateResult;
4859+
4860+
const previousRules = (
4861+
environmentById.get(environmentId)?.serverConfig?.keybindings ?? []
4862+
).flatMap((binding) => {
4863+
if (binding.command !== input.keybindingCommand || binding.whenAst) return [];
4864+
const previous = decodeProjectScriptKeybindingRule({
4865+
keybinding: keybindingValueForCommand([binding], input.keybindingCommand),
4866+
command: input.keybindingCommand,
4867+
});
4868+
return previous ? [previous] : [];
4869+
});
4870+
const previous = previousRules.at(-1);
4871+
for (const rule of keybindingRule ? previousRules.slice(0, -1) : previousRules) {
4872+
const result = await removeKeybinding({ environmentId, input: rule });
4873+
if (result._tag === "Failure") return mapAtomCommandResult(result, () => undefined);
4874+
}
4875+
return keybindingRule
4876+
? mapAtomCommandResult(
4877+
await upsertKeybinding({
4878+
environmentId,
4879+
input:
4880+
previous && previous.key !== keybindingRule.key
4881+
? { ...keybindingRule, replace: previous }
4882+
: keybindingRule,
4883+
}),
4884+
() => undefined,
4885+
)
4886+
: updateResult;
48404887
},
48414888
[
4889+
allProjects,
4890+
environmentById,
48424891
environmentId,
4843-
settings.projectSettingsOverrides,
4892+
removeKeybinding,
4893+
settings,
48444894
supportsProjectSettingsOverrides,
48454895
updateProjectScriptSettings,
48464896
upsertKeybinding,

0 commit comments

Comments
 (0)