This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-26 13:20:36 -08:00
1 parent 89c364a1a3
commit f20bbdf267
5 files changed
+163 -43

No files matched your search

@@ -97,6 +97,9 @@ Align frontend input/output validation with `@t3tools/contracts` schemas so the
## Phase 5: Script/Keybinding Validation Consistency
### Status
Complete
### Issue
`ProjectScriptsControl` and `ChatView` rely mostly on local non-empty checks while sending keybinding data into a contract-validated endpoint (`server.upsertKeybinding`).
+9 -15
View File
@@ -92,6 +92,7 @@ import { CursorIcon, Icon } from "./Icons";
import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
import { Badge } from "./ui/badge";
import { Command, CommandInput, CommandItem, CommandList } from "./ui/command";
import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
import ProjectScriptsControl, { type NewProjectScriptInput } from "./ProjectScriptsControl";
import {
commandForProjectScript,
@@ -814,21 +815,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
scripts: input.nextScripts,
});
if (isElectron && input.keybinding) {
const keybindingUpdate = await api.server.upsertKeybinding({
key: input.keybinding,
command: input.keybindingCommand,
});
queryClient.setQueryData(
serverQueryKeys.config(),
(current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
current
? { ...current, keybindings: keybindingUpdate.keybindings }
: {
cwd: input.projectCwd,
keybindings: keybindingUpdate.keybindings,
},
);
const keybindingRule = decodeProjectScriptKeybindingRule({
keybinding: input.keybinding,
command: input.keybindingCommand,
});
if (isElectron && keybindingRule) {
await api.server.upsertKeybinding(keybindingRule);
await queryClient.invalidateQueries({ queryKey: serverQueryKeys.all });
}
},
[queryClient],
@@ -16,7 +16,8 @@ import {
} from "lucide-react";
import React, { type FormEvent, type KeyboardEvent, useMemo, useState } from "react";
import { commandForProjectScript, primaryProjectScript } from "~/projectScripts";
import { keybindingValueForCommand, decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
import { commandForProjectScript, nextProjectScriptId, primaryProjectScript } from "~/projectScripts";
import { shortcutLabelForCommand } from "~/keybindings";
import { isMacPlatform } from "~/lib/utils";
import { Button } from "./ui/button";
@@ -129,32 +130,6 @@ function keybindingFromEvent(event: KeyboardEvent<HTMLInputElement>): string | n
return parts.join("+");
}
function keybindingValueForCommand(
keybindings: ResolvedKeybindingsConfig,
command: string,
): string | null {
for (let index = keybindings.length - 1; index >= 0; index -= 1) {
const binding = keybindings[index];
if (!binding || binding.command !== command) continue;
const parts: string[] = [];
if (binding.shortcut.modKey) parts.push("mod");
if (binding.shortcut.ctrlKey) parts.push("ctrl");
if (binding.shortcut.metaKey) parts.push("meta");
if (binding.shortcut.altKey) parts.push("alt");
if (binding.shortcut.shiftKey) parts.push("shift");
const keyToken =
binding.shortcut.key === " "
? "space"
: binding.shortcut.key === "escape"
? "esc"
: binding.shortcut.key;
parts.push(keyToken);
return parts.join("+");
}
return null;
}
export default function ProjectScriptsControl({
scripts,
keybindings,
@@ -212,12 +187,18 @@ export default function ProjectScriptsControl({
setValidationError(null);
try {
const scriptIdForValidation =
editingScriptId ?? nextProjectScriptId(trimmedName, scripts.map((script) => script.id));
const keybindingRule = decodeProjectScriptKeybindingRule({
keybinding,
command: commandForProjectScript(scriptIdForValidation),
});
const payload = {
name: trimmedName,
command: trimmedCommand,
icon,
runOnWorktreeCreate,
keybinding: keybinding.trim() || null,
keybinding: keybindingRule?.key ?? null,
} satisfies NewProjectScriptInput;
if (editingScriptId) {
await onUpdateScript(editingScriptId, payload);
@@ -0,0 +1,83 @@
import { MAX_KEYBINDING_VALUE_LENGTH, type KeybindingCommand } from "@t3tools/contracts";
import { describe, expect, it } from "vitest";
import { commandForProjectScript } from "../projectScripts";
import {
decodeProjectScriptKeybindingRule,
keybindingValueForCommand,
PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE,
} from "./projectScriptKeybindings";
describe("projectScriptKeybindings", () => {
it("decodes and trims valid keybinding rules", () => {
const rule = decodeProjectScriptKeybindingRule({
keybinding: " mod+k ",
command: commandForProjectScript("lint"),
});
expect(rule).toEqual({
key: "mod+k",
command: "script.lint.run",
});
});
it("returns null when keybinding is empty", () => {
expect(
decodeProjectScriptKeybindingRule({
keybinding: " ",
command: commandForProjectScript("lint"),
}),
).toBeNull();
});
it("rejects invalid keybinding values", () => {
expect(() =>
decodeProjectScriptKeybindingRule({
keybinding: "k".repeat(MAX_KEYBINDING_VALUE_LENGTH + 1),
command: commandForProjectScript("lint"),
}),
).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
});
it("rejects invalid commands", () => {
expect(() =>
decodeProjectScriptKeybindingRule({
keybinding: "mod+k",
command: "script.BAD.run" as KeybindingCommand,
}),
).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
});
it("reads latest matching keybinding value for a command", () => {
const command = commandForProjectScript("test");
const value = keybindingValueForCommand(
[
{
command,
shortcut: {
key: "escape",
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
modKey: true,
},
},
{
command,
shortcut: {
key: "k",
metaKey: false,
ctrlKey: false,
shiftKey: true,
altKey: false,
modKey: true,
},
},
],
command,
);
expect(value).toBe("mod+shift+k");
});
});
@@ -0,0 +1,59 @@
import {
KeybindingRule as KeybindingRuleSchema,
type KeybindingCommand,
type KeybindingRule,
type ResolvedKeybindingsConfig,
} from "@t3tools/contracts";
import { Schema } from "effect";
export const PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE = "Invalid keybinding.";
function normalizeProjectScriptKeybindingInput(
keybinding: string | null | undefined,
): string | null {
const trimmed = keybinding?.trim() ?? "";
return trimmed.length > 0 ? trimmed : null;
}
export function decodeProjectScriptKeybindingRule(input: {
keybinding: string | null | undefined;
command: KeybindingCommand;
}): KeybindingRule | null {
const normalizedKey = normalizeProjectScriptKeybindingInput(input.keybinding);
if (!normalizedKey) return null;
const decoded = Schema.decodeUnknownOption(KeybindingRuleSchema)({
key: normalizedKey,
command: input.command,
});
if (decoded._tag === "None") {
throw new Error(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);
}
return decoded.value;
}
export function keybindingValueForCommand(
keybindings: ResolvedKeybindingsConfig,
command: KeybindingCommand,
): string | null {
for (let index = keybindings.length - 1; index >= 0; index -= 1) {
const binding = keybindings[index];
if (!binding || binding.command !== command) continue;
const parts: string[] = [];
if (binding.shortcut.modKey) parts.push("mod");
if (binding.shortcut.ctrlKey) parts.push("ctrl");
if (binding.shortcut.metaKey) parts.push("meta");
if (binding.shortcut.altKey) parts.push("alt");
if (binding.shortcut.shiftKey) parts.push("shift");
const keyToken =
binding.shortcut.key === " "
? "space"
: binding.shortcut.key === "escape"
? "esc"
: binding.shortcut.key;
parts.push(keyToken);
return parts.join("+");
}
return null;
}