The best way to control Claude Code, Codex and more from any computer or phone

main
ts 140 lines 5.22 KB
Raw
1 import { useMemo, useState } from "@alchemy.run/sigil/react";
2 import { useTerminalInput } from "../ui/index.ts";
3 import { type PlanRow, type PlanTree, type PlanTreeState, type PlanView } from "./PlanTree.ts";
4 import { stackOutputLineCount } from "./StackOutputs.tsx";
5 import { isTerminalStatus } from "./statusStyle.ts";
6
7 export type VirtualPlanLine =
8 | { readonly kind: "row"; readonly row: PlanRow }
9 | {
10 readonly kind: "yaml";
11 readonly key: string;
12 readonly line: string;
13 readonly paddingLeft: number;
14 }
15 | {
16 readonly kind: "note";
17 readonly key: string;
18 readonly paddingLeft: number;
19 };
20
21 const planLines = (rows: readonly PlanRow[], detailed: boolean): VirtualPlanLine[] =>
22 rows.flatMap((row): VirtualPlanLine[] => {
23 const lines: VirtualPlanLine[] = [{ kind: "row", row }];
24 if (row.type !== "resource") return lines;
25 const showYaml = detailed || row.propertyYaml?.kind === "drift";
26 if (!showYaml) return lines;
27 if (row.propertyYaml !== undefined) {
28 lines.push(
29 ...row.propertyYaml.lines.map((line, index) => ({
30 kind: "yaml" as const,
31 key: `${row.key}:yaml:${index}`,
32 line,
33 paddingLeft: row.depth * 2 + 2,
34 })),
35 );
36 } else if (row.action === "update" || row.action === "adopted" || row.action === "replace") {
37 lines.push({
38 kind: "note",
39 key: `${row.key}:note`,
40 paddingLeft: row.depth * 2 + 2,
41 });
42 }
43 return lines;
44 });
45
46 export interface PlanWindow {
47 readonly selectedView: PlanView;
48 readonly hasOutput: boolean;
49 readonly virtual: boolean;
50 readonly budget: number;
51 readonly offset: number;
52 readonly hiddenBelow: number;
53 readonly planLines: readonly VirtualPlanLine[] | undefined;
54 }
55
56 export const usePlanViewport = (options: {
57 readonly tree: PlanTree;
58 readonly state: PlanTreeState;
59 readonly lineBudget: number;
60 readonly collapsible: boolean;
61 readonly collapsed: boolean;
62 }): PlanWindow => {
63 const { tree, state, lineBudget, collapsible, collapsed } = options;
64 const { rows, detailed } = tree;
65 const { tasks, output, view, viewport } = state;
66 const hasOutput = output !== undefined;
67 const selectedView = view === "output" && hasOutput ? "output" : "plan";
68 const virtual = viewport === "virtual";
69 const expandedLines = useMemo(() => planLines(rows, detailed), [rows, detailed]);
70 const available =
71 lineBudget === Number.POSITIVE_INFINITY ? rows.length : Math.max(1, Math.floor(lineBudget));
72 const length =
73 selectedView === "output"
74 ? stackOutputLineCount(output)
75 : virtual
76 ? expandedLines.length
77 : rows.length;
78 // Overflow markers render above and below the content window, so they must
79 // consume its budget rather than extending the complete widget. Reserve
80 // both even when following the end (where only the upper marker is shown),
81 // because manual scrolling can make both visible without changing height.
82 const budget = virtual && length > available ? Math.max(1, available - 2) : available;
83 const maxOffset = Math.max(0, length - budget);
84 const activeRow = tree.progressRows.find((row) => {
85 const status = tasks.get(row.key)?.status;
86 return status !== undefined && !isTerminalStatus(status);
87 });
88 const activeRowIndex = activeRow === undefined ? -1 : rows.indexOf(activeRow);
89 const activeLineIndex = virtual
90 ? expandedLines.findIndex((line) => line.kind === "row" && line.row === rows[activeRowIndex])
91 : activeRowIndex;
92 const followedOffset =
93 selectedView === "output"
94 ? maxOffset
95 : Math.min(
96 maxOffset,
97 Math.max(0, (activeLineIndex < 0 ? length : activeLineIndex) - Math.floor(budget / 3)),
98 );
99 const [manualOffsets, setManualOffsets] = useState<Record<PlanView, number | undefined>>({
100 plan: undefined,
101 output: undefined,
102 });
103 const offset = virtual ? Math.min(maxOffset, manualOffsets[selectedView] ?? followedOffset) : 0;
104 const setOffset = (update: (current: number | undefined) => number | undefined) =>
105 setManualOffsets((current) => ({
106 ...current,
107 [selectedView]: update(current[selectedView]),
108 }));
109
110 useTerminalInput((input, key) => {
111 if (collapsible && !key.ctrl && !key.meta && input.toLowerCase() === "p") {
112 tree.setExpanded(collapsed);
113 return;
114 }
115 if (collapsed) return;
116 if (hasOutput && (key.left || key.right)) {
117 tree.setView(selectedView === "plan" ? "output" : "plan");
118 return;
119 }
120 if (!virtual) return;
121 const page = Math.max(1, Math.floor(lineBudget));
122 if (key.up) setOffset((current) => Math.max(0, (current ?? offset) - 1));
123 else if (key.down) setOffset((current) => Math.min(maxOffset, (current ?? offset) + 1));
124 else if (key.pageUp) setOffset((current) => Math.max(0, (current ?? offset) - page));
125 else if (key.pageDown) setOffset((current) => Math.min(maxOffset, (current ?? offset) + page));
126 else if (key.home) setOffset(() => 0);
127 else if (key.end) setOffset(() => undefined);
128 });
129
130 return {
131 selectedView,
132 hasOutput,
133 virtual,
134 budget,
135 offset,
136 hiddenBelow: virtual ? Math.max(0, length - offset - budget) : 0,
137 planLines:
138 virtual && selectedView === "plan" ? expandedLines.slice(offset, offset + budget) : undefined,
139 };
140 };