Move Tree component to @yaakapp-internal/ui package

Decouple Tree from client app's hotkey system by adding
getSelectedItems() to TreeHandle and having callers register
hotkeys externally. Extract shared action callbacks to eliminate
duplication between hotkey and context menu handlers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Gregory Schier
2026-03-08 22:32:49 -07:00
parent 4c041e68a9
commit 12ece44197
31 changed files with 477 additions and 545 deletions

View File

@@ -0,0 +1,31 @@
import { Button, type ButtonProps } from "@yaakapp-internal/ui";
import { useCallback, useState } from "react";
import type { ActionInvocation } from "@yaakapp-internal/proxy-lib";
import { useActionMetadata } from "./hooks";
import { rpc } from "./rpc";
type ActionButtonProps = Omit<ButtonProps, "onClick" | "children"> & {
action: ActionInvocation;
/** Override the label from metadata */
children?: React.ReactNode;
};
export function ActionButton({ action, children, ...props }: ActionButtonProps) {
const meta = useActionMetadata(action);
const [busy, setBusy] = useState(false);
const onClick = useCallback(async () => {
setBusy(true);
try {
await rpc("execute_action", action);
} finally {
setBusy(false);
}
}, [action]);
return (
<Button {...props} disabled={props.disabled || busy} isLoading={busy} onClick={onClick}>
{children ?? meta?.label ?? "…"}
</Button>
);
}

35
apps/yaak-proxy/hooks.ts Normal file
View File

@@ -0,0 +1,35 @@
import { useEffect, useState } from "react";
import type {
ActionInvocation,
ActionMetadata,
} from "@yaakapp-internal/proxy-lib";
import { rpc } from "./rpc";
let cachedActions: [ActionInvocation, ActionMetadata][] | null = null;
/** Fetch and cache all action metadata. */
async function getActions(): Promise<[ActionInvocation, ActionMetadata][]> {
if (!cachedActions) {
const { actions } = await rpc("list_actions", {});
cachedActions = actions;
}
return cachedActions;
}
/** Look up metadata for a specific action invocation. */
export function useActionMetadata(
action: ActionInvocation,
): ActionMetadata | null {
const [meta, setMeta] = useState<ActionMetadata | null>(null);
useEffect(() => {
getActions().then((actions) => {
const match = actions.find(
([inv]) => inv.scope === action.scope && inv.action === action.action,
);
setMeta(match?.[1] ?? null);
});
}, [action]);
return meta;
}

View File

@@ -0,0 +1,64 @@
import type {
ActionInvocation,
ActionMetadata,
} from "@yaakapp-internal/proxy-lib";
import { rpc } from "./rpc";
type ActionBinding = {
invocation: ActionInvocation;
meta: ActionMetadata;
keys: { key: string; ctrl: boolean; shift: boolean; alt: boolean; meta: boolean };
};
/** Parse a hotkey string like "Ctrl+Shift+P" into its parts. */
function parseHotkey(hotkey: string): ActionBinding["keys"] {
const parts = hotkey.split("+").map((p) => p.trim().toLowerCase());
return {
ctrl: parts.includes("ctrl") || parts.includes("control"),
shift: parts.includes("shift"),
alt: parts.includes("alt"),
meta: parts.includes("meta") || parts.includes("cmd") || parts.includes("command"),
key: parts.filter(
(p) => !["ctrl", "control", "shift", "alt", "meta", "cmd", "command"].includes(p),
)[0] ?? "",
};
}
function matchesEvent(binding: ActionBinding["keys"], e: KeyboardEvent): boolean {
return (
e.ctrlKey === binding.ctrl &&
e.shiftKey === binding.shift &&
e.altKey === binding.alt &&
e.metaKey === binding.meta &&
e.key.toLowerCase() === binding.key
);
}
/** Fetch all actions from Rust and register a global keydown listener. */
export async function initHotkeys(): Promise<() => void> {
const { actions } = await rpc("list_actions", {});
const bindings: ActionBinding[] = actions
.filter(
(entry): entry is [ActionInvocation, ActionMetadata & { defaultHotkey: string }] =>
entry[1].defaultHotkey != null,
)
.map(([invocation, meta]) => ({
invocation,
meta,
keys: parseHotkey(meta.defaultHotkey),
}));
function onKeyDown(e: KeyboardEvent) {
for (const binding of bindings) {
if (matchesEvent(binding.keys, e)) {
e.preventDefault();
rpc("execute_action", binding.invocation);
return;
}
}
}
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}

View File

@@ -1,11 +1,13 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { type } from "@tauri-apps/plugin-os";
import { Button, HeaderSize } from "@yaakapp-internal/ui";
import { HeaderSize } from "@yaakapp-internal/ui";
import { ActionButton } from "./ActionButton";
import classNames from "classnames";
import { createStore, Provider, useAtomValue } from "jotai";
import { StrictMode, useState } from "react";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./main.css";
import { initHotkeys } from "./hotkeys";
import { listen, rpc } from "./rpc";
import { applyChange, dataAtom, httpExchangesAtom, replaceAll } from "./store";
@@ -19,6 +21,9 @@ rpc("list_models", {}).then((res) => {
);
});
// Register hotkeys from action metadata
initHotkeys();
// Subscribe to model change events from the backend
listen("model_write", (payload) => {
jotaiStore.set(dataAtom, (prev) =>
@@ -27,37 +32,9 @@ listen("model_write", (payload) => {
});
function App() {
const [status, setStatus] = useState("Idle");
const [busy, setBusy] = useState(false);
const osType = type();
const exchanges = useAtomValue(httpExchangesAtom);
async function startProxy() {
setBusy(true);
setStatus("Starting...");
try {
await rpc("execute_action", { scope: "global", action: "proxy_start" });
setStatus("Running");
} catch (err) {
setStatus(`Failed: ${String(err)}`);
} finally {
setBusy(false);
}
}
async function stopProxy() {
setBusy(true);
setStatus("Stopping...");
try {
await rpc("execute_action", { scope: "global", action: "proxy_stop" });
setStatus("Stopped");
} catch (err) {
setStatus(`Failed: ${String(err)}`);
} finally {
setBusy(false);
}
}
return (
<div
className={classNames(
@@ -82,20 +59,16 @@ function App() {
</HeaderSize>
<main className="overflow-auto p-4">
<div className="flex items-center gap-3 mb-4">
<Button disabled={busy} onClick={startProxy} size="sm" tone="primary">
Start Proxy
</Button>
<Button
disabled={busy}
onClick={stopProxy}
<ActionButton
action={{ scope: "global", action: "proxy_start" }}
size="sm"
tone="primary"
/>
<ActionButton
action={{ scope: "global", action: "proxy_stop" }}
size="sm"
variant="border"
>
Stop Proxy
</Button>
<span className="text-xs text-text-subtlest">
{status}
</span>
/>
</div>
<div className="text-xs font-mono">

View File

@@ -14,6 +14,7 @@
"@tauri-apps/plugin-os": "^2.3.2",
"@yaakapp-internal/theme": "^1.0.0",
"@yaakapp-internal/model-store": "^1.0.0",
"@yaakapp-internal/proxy-lib": "^1.0.0",
"@yaakapp-internal/ui": "^1.0.0",
"classnames": "^2.5.1",
"jotai": "^2.18.0",

View File

@@ -3,7 +3,7 @@ import { listen as tauriListen } from "@tauri-apps/api/event";
import type {
RpcEventSchema,
RpcSchema,
} from "../../crates-proxy/yaak-proxy-lib/bindings/gen_rpc";
} from "@yaakapp-internal/proxy-lib";
type Req<K extends keyof RpcSchema> = RpcSchema[K][0];
type Res<K extends keyof RpcSchema> = RpcSchema[K][1];

View File

@@ -1,5 +1,5 @@
import { createModelStore } from "@yaakapp-internal/model-store";
import type { HttpExchange } from "../../crates-proxy/yaak-proxy-lib/bindings/gen_models";
import type { HttpExchange } from "@yaakapp-internal/proxy-lib";
type ProxyModels = {
http_exchange: HttpExchange;