diff --git a/src/ui/App.tsx b/src/ui/App.tsx index e6645ab..087bebf 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -777,30 +777,15 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C { keys: ["enter", "space"], run: () => cycleSetting(settingsIndex ?? 0), hint: "change" }, { keys: ["esc", "q", "h", "←"], run: closeSettings, hint: "close" }, ); - const prCommand = (prefix: string) => pullRequests.commands.find((command) => command.id.startsWith(prefix)); - const prKey = (key: string, prefix: string, hint: string): Binding | null => { - const command = prCommand(prefix); - return command ? { keys: [key], run: command.run, hint } : null; - }; const pullRequestKeys = keys( { keys: ["j", "↓"], label: "j/k", run: () => scrollBy(1), hint: "scroll" }, { keys: ["k", "↑"], run: () => scrollBy(-1) }, { keys: ["g"], run: () => scrollRef.current?.scrollTo(0) }, { keys: ["G"], run: () => scrollRef.current?.scrollTo(scrollRef.current.scrollHeight) }, - prKey("o", "pr-open", "open"), - prKey("r", "pr-refresh", "refresh"), - prKey("c", "pr-comment", "comment"), - prKey("R", "pr-review", "review"), - prKey("a", "pr-actions", "actions"), - prKey("w", "watch-pull-request", "watch"), - prKey("u", "unlink-pull-request", "unlink"), - { keys: ["esc", "q"], run: pullRequests.close, hint: "close" }, + pullRequests.viewKeys, ); const findKeys = keys( - { keys: ["enter"], run: () => find.step(1), hint: "next match" }, - { keys: ["shift+enter"], run: () => find.step(-1), hint: "previous" }, - { keys: ["ctrl+n"], label: "ctrl+n/p", run: () => find.step(1), hint: "match" }, - { keys: ["ctrl+p"], run: () => find.step(-1) }, + find.bindings, { keys: ["esc"], run: () => setMode("nav"), hint: "close find" }, quitKey, paletteKey, diff --git a/src/ui/usePullRequests.ts b/src/ui/usePullRequests.ts index 6b4bca7..cf5b7a1 100644 --- a/src/ui/usePullRequests.ts +++ b/src/ui/usePullRequests.ts @@ -1,4 +1,4 @@ -import type { CliRenderer, KeyEvent, ScrollBoxRenderable } from "@opentui/core"; +import type { CliRenderer } from "@opentui/core"; import type { GitPreparePullRequestThreadResult, OrchestrationProjectShell, @@ -22,6 +22,7 @@ import { useEffect, useMemo, useRef, useState } from "react"; import type { T3Session } from "../client/session.ts"; import { pullRequestItems, type ComposerItem } from "../model/composerMenu.ts"; +import { keys, type Binding } from "../model/keymap.ts"; import type { PaletteCommand, Picker } from "../model/palette.ts"; import { COMPOSER_PULL_REQUEST_LIST_LIMIT, @@ -417,50 +418,22 @@ export function usePullRequests(deps: PullRequestDeps) { } : null; - /** Keys inside the PR view. Returns true when handled. */ - const handleKey = (key: KeyEvent, scroll: ScrollBoxRenderable | null): boolean => { - if (!loadedRef.current) return false; - const current = loadedRef.current; - switch (key.name) { - case "j": - case "down": - scroll?.scrollBy(1); - return true; - case "k": - case "up": - scroll?.scrollBy(-1); - return true; - case "g": - if (key.shift) scroll?.scrollTo(scroll.scrollHeight); - else scroll?.scrollTo(0); - return true; - case "o": - openInBrowser(); - return true; - case "r": - if (key.shift) review(); - else refresh(); - return true; - case "c": - comment(); - return true; - case "a": - actions(); - return true; - case "w": - if (supportsWatch && (current.link.snapshot === null || current.link.snapshot.state === "open")) setWatching(current.link, !watchingCurrent); - return true; - case "u": - unlink(current.link); - return true; - case "escape": - case "q": - close(); - return true; - default: - return false; - } - }; + /** Keys inside the PR view, for the PR it shows. */ + const current = loaded; + const viewKeys: Binding[] = current + ? keys( + { keys: ["o"], run: openInBrowser, hint: "open" }, + { keys: ["r"], run: refresh, hint: "refresh" }, + current.detail && [ + { keys: ["c"], run: comment, hint: "comment" }, + { keys: ["R"], run: review, hint: "review" }, + { keys: ["a"], run: actions, hint: "actions" }, + ], + supportsWatch && (current.link.snapshot === null || current.link.snapshot.state === "open") && { keys: ["w"], run: () => setWatching(current.link, !watchingCurrent), hint: watchingCurrent ? "unwatch" : "watch" }, + { keys: ["u"], run: () => unlink(current.link), hint: current.link.source === "stack" ? "dismiss" : "unlink" }, + { keys: ["esc", "q"], run: close, hint: "close" }, + ) + : []; const commands: (PaletteCommand & { run: () => void })[] = []; if (threadShell && links.length > 0) { @@ -499,7 +472,7 @@ export function usePullRequests(deps: PullRequestDeps) { commands.push({ id: "pr-refresh", title: "Refresh pull request", shortcut: "r", keywords: ["refresh", "reload"], run: refresh }); } - return { view, isOpen: loaded !== null, open, close, handleKey, commands }; + return { view, isOpen: loaded !== null, open, close, commands, viewKeys }; } /** Two links name the same pull request when host, repository and number all match. */ diff --git a/src/ui/useThreadFind.ts b/src/ui/useThreadFind.ts index e26881a..26674df 100644 --- a/src/ui/useThreadFind.ts +++ b/src/ui/useThreadFind.ts @@ -1,7 +1,7 @@ -import type { KeyEvent } from "@opentui/core"; import type { OrchestrationV2SearchThreadResult, ThreadId } from "@t3tools/contracts"; import { useEffect, useRef, useState } from "react"; import type { T3Session, ThreadState } from "../client/session.ts"; +import type { Binding } from "../model/keymap.ts"; import type { Timeline } from "../model/timeline.ts"; import { stepThreadFindIndex } from "../../vendor/t3code/apps/web/src/components/chat/threadFind.ts"; @@ -62,15 +62,13 @@ export function useThreadFind(session: T3Session, id: ThreadId | undefined, thre if (data) setIndex(stepThreadFindIndex(data.activeIndex, data.totalMatches, delta)); }; - const handleKey = (keyEvent: KeyEvent): boolean => { - if (!open) return false; - if (keyEvent.name === "escape") return true; - if (keyEvent.name === "return") step(keyEvent.shift ? -1 : 1); - else if (keyEvent.ctrl && keyEvent.name === "n") step(1); - else if (keyEvent.ctrl && keyEvent.name === "p") step(-1); - else return false; - return true; - }; + /** While the find bar has the keys, next to the query field. */ + const bindings: Binding[] = [ + { keys: ["enter"], run: () => step(1), hint: "next match" }, + { keys: ["shift+enter"], run: () => step(-1), hint: "previous" }, + { keys: ["ctrl+n"], label: "ctrl+n/p", run: () => step(1), hint: "match" }, + { keys: ["ctrl+p"], run: () => step(-1) }, + ]; const onQuery = (value: string) => { setQuery(value); @@ -86,6 +84,6 @@ export function useThreadFind(session: T3Session, id: ThreadId | undefined, thre error, pending: key !== null && data === null && !error, step, - handleKey, + bindings, }; }