@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 66 lines 2.43 KB
Raw
1 import { measureElement, useProgress, useTitle, type DOMElement } from "@alchemy.run/sigil";
2 import { useLayoutEffect, useRef, useState } from "@alchemy.run/sigil/react";
3 import { useTerminalSize } from "../ui/index.ts";
4 import type { PlanTree, PlanTreeState } from "./PlanTree.ts";
5
6 export const usePlanPresentation = (options: {
7 readonly tree: PlanTree;
8 readonly state: PlanTreeState;
9 readonly busy: boolean;
10 readonly collapsible: boolean;
11 readonly hasFooter: boolean;
12 }) => {
13 const { tree, state, busy, collapsible, hasFooter } = options;
14 const { mode } = tree;
15 const { label, expanded, viewport, outcome } = state;
16 const { completed, failures, total } = tree.progress();
17 const collapsed = collapsible && !expanded;
18 const settled = !busy && completed >= total;
19
20 useProgress(
21 mode === "apply"
22 ? {
23 state: outcome === "failure" || failures > 0 ? "error" : busy ? "normal" : "inactive",
24 value: total === 0 ? undefined : (completed / total) * 100,
25 }
26 : { state: "inactive" },
27 );
28 const titleProgress = settled || total === 0 ? "" : ` ${completed}/${total}`;
29 const titleDetail = tree.titleDetail ? ` · ${tree.titleDetail}` : "";
30 useTitle(mode === "apply" ? `${label}${titleProgress}${titleDetail}` : undefined);
31
32 const beforeRef = useRef<DOMElement>(null);
33 const summaryRef = useRef<DOMElement>(null);
34 const controlsRef = useRef<DOMElement>(null);
35 const afterRef = useRef<DOMElement>(null);
36 const [chromeRows, setChromeRows] = useState<number>();
37 const { rows: terminalRows } = useTerminalSize();
38 const showControls = mode === "apply" || (!collapsed && (collapsible || hasFooter));
39
40 useLayoutEffect(() => {
41 if (collapsed || viewport !== "virtual") return;
42 const height = (ref: { readonly current: DOMElement | null }) =>
43 ref.current === null ? 0 : measureElement(ref.current).height;
44 const measured =
45 height(beforeRef) +
46 height(summaryRef) +
47 height(controlsRef) +
48 height(afterRef) +
49 1 +
50 (showControls ? 1 : 0);
51 setChromeRows((current) => (current === measured ? current : measured));
52 });
53
54 const lineBudget =
55 viewport === "virtual"
56 ? Math.max(1, terminalRows - (mode === "apply" ? 3 : 0) - (chromeRows ?? 8))
57 : Number.POSITIVE_INFINITY;
58
59 return {
60 progress: { completed, failures, total },
61 collapsed,
62 showControls,
63 lineBudget,
64 refs: { beforeRef, summaryRef, controlsRef, afterRef },
65 } as const;
66 };