Move editor definitions to contracts for strict IPC typing
EDITORS config and EditorId type now live in @acme/contracts so the shell.openInEditor IPC channel is strictly typed end-to-end. The main process uses the contract's command field (null = file manager via shell.openPath). Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
1 parent
3303d9a7fa
commit
6204eaf5e1
3 files changed
+22
-16
No files matched your search
@@ -13,6 +13,7 @@ import {
|
||||
} from "electron";
|
||||
|
||||
import {
|
||||
EDITORS,
|
||||
IPC_CHANNELS,
|
||||
type TerminalCommandInput,
|
||||
type TerminalCommandResult,
|
||||
@@ -136,16 +137,13 @@ function registerIpcHandlers(): void {
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.shellOpenInEditor,
|
||||
async (_event, cwd: string, editor: string) => {
|
||||
if (editor === "file-manager") {
|
||||
const editorDef = EDITORS.find((e) => e.id === editor);
|
||||
if (!editorDef) throw new Error(`Unknown editor: ${editor}`);
|
||||
if (!editorDef.command) {
|
||||
await shell.openPath(cwd);
|
||||
return;
|
||||
}
|
||||
const EDITOR_COMMANDS: Record<string, { command: string; args: (cwd: string) => string[] }> = {
|
||||
cursor: { command: "cursor", args: (p) => [p] },
|
||||
};
|
||||
const entry = EDITOR_COMMANDS[editor];
|
||||
if (!entry) throw new Error(`Unknown editor: ${editor}`);
|
||||
const child = spawn(entry.command, entry.args(cwd), {
|
||||
const child = spawn(editorDef.command, [cwd], {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
import { EDITORS, type EditorId } from "@acme/contracts";
|
||||
import {
|
||||
DEFAULT_MODEL,
|
||||
DEFAULT_REASONING,
|
||||
@@ -36,10 +37,10 @@ const FILE_MANAGER_LABEL = navigator.platform.includes("Mac")
|
||||
? "Explorer"
|
||||
: "Files";
|
||||
|
||||
const EDITORS = [
|
||||
{ id: "cursor", label: "Cursor" },
|
||||
{ id: "file-manager", label: FILE_MANAGER_LABEL },
|
||||
] as const;
|
||||
function editorLabel(editor: (typeof EDITORS)[number]): string {
|
||||
return editor.command ? editor.label : FILE_MANAGER_LABEL;
|
||||
}
|
||||
|
||||
const LAST_EDITOR_KEY = "codething:last-editor";
|
||||
|
||||
function statusLabel(phase: string): string {
|
||||
@@ -64,8 +65,8 @@ export default function ChatView() {
|
||||
const [isConnecting, setIsConnecting] = useState(false);
|
||||
const [isModelMenuOpen, setIsModelMenuOpen] = useState(false);
|
||||
const [isEditorMenuOpen, setIsEditorMenuOpen] = useState(false);
|
||||
const [lastEditor, setLastEditor] = useState(
|
||||
() => localStorage.getItem(LAST_EDITOR_KEY) ?? EDITORS[0].id,
|
||||
const [lastEditor, setLastEditor] = useState<EditorId>(
|
||||
() => (localStorage.getItem(LAST_EDITOR_KEY) as EditorId) ?? EDITORS[0].id,
|
||||
);
|
||||
const [selectedEffort, setSelectedEffort] =
|
||||
useState<string>(DEFAULT_REASONING);
|
||||
@@ -194,7 +195,7 @@ export default function ChatView() {
|
||||
return () => window.removeEventListener("keydown", handler);
|
||||
}, [api, activeProject, lastEditor]);
|
||||
|
||||
const openInEditor = (editorId: string) => {
|
||||
const openInEditor = (editorId: EditorId) => {
|
||||
if (!api || !activeProject) return;
|
||||
void api.shell.openInEditor(activeProject.cwd, editorId);
|
||||
setLastEditor(editorId);
|
||||
@@ -394,7 +395,7 @@ export default function ChatView() {
|
||||
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-[#e0e0e0] hover:bg-white/[0.06]"
|
||||
onClick={() => openInEditor(editor.id)}
|
||||
>
|
||||
{editor.label}
|
||||
{editorLabel(editor)}
|
||||
{editor.id === lastEditor && (
|
||||
<kbd className="ml-auto text-[9px] text-[#a0a0a0]/40">
|
||||
{navigator.platform.includes("Mac") ? "\u2318" : "Ctrl+"}O
|
||||
|
||||
@@ -11,6 +11,13 @@ import type {
|
||||
import type { TerminalCommandInput, TerminalCommandResult } from "./terminal";
|
||||
import type { NewTodoInput, Todo } from "./todo";
|
||||
|
||||
export const EDITORS = [
|
||||
{ id: "cursor", label: "Cursor", command: "cursor" },
|
||||
{ id: "file-manager", label: "File Manager", command: null },
|
||||
] as const;
|
||||
|
||||
export type EditorId = (typeof EDITORS)[number]["id"];
|
||||
|
||||
export const IPC_CHANNELS = {
|
||||
todosList: "todos:list",
|
||||
todosAdd: "todos:add",
|
||||
@@ -65,6 +72,6 @@ export interface NativeApi {
|
||||
onEvent: (callback: (event: ProviderEvent) => void) => () => void;
|
||||
};
|
||||
shell: {
|
||||
openInEditor: (cwd: string, editor: string) => Promise<void>;
|
||||
openInEditor: (cwd: string, editor: EditorId) => Promise<void>;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user