// @ts-check // The dock: the plan and the changed files, as the host renders them. A rail beside the transcript // on a wide screen, shown and hidden by the header's button and `/dock` (layout.js); the project // panel's Dock tab on a phone. A long plan scrolls in its own box, and a changed file opens its // diff. import { useLayoutEffect, useRef } from "preact/hooks"; import { html, landedIn } from "./html.js"; import { Icon } from "./icons.js"; import { useRendered } from "./render.js"; import { PickablePiece } from "./lines-view.js"; import { hasDock, planScroll } from "./workspace.js"; import { useWide } from "./layout.js"; /** @import { Changes, OpenDiff, Todos } from "./workspace.js" */ /** @param {{ todos: Todos | null, changes: Changes | null, shown: boolean, open: (path: string) => void }} props */ export function Dock({ todos, changes, shown, open }) { return html``; } /** * The dock as the project panel's Dock tab, on a phone, where the panel is the one way in. * @param {{ hooks: { todos: Todos | null, changes: Changes | null, openDiff: (path: string) => void } }} props */ export function DockTab({ hooks: { todos, changes, openDiff } }) { return html`
No plan or changes yet.
`} ${todos?.total ? html`<${Section} piece=${{ kind: "plan", todos }} />` : null} ${changes?.total_files ? html`<${Section} piece=${{ kind: "changes", changes }} />` : null}`; } /** * The rows are host HTML, so a tap is read off the button it landed in. * @param {Event} event @param {(path: string) => void} open */ function pickChange(event, open) { const path = landedIn(event, "button.change")?.dataset.path; if (path) open(path); } /** @param {{ piece: import("./render.js").Piece }} props */ function Section({ piece }) { const rendered = useRendered(piece, true); const section = useRef(/** @type {HTMLElement | null} */ (null)); const handsOn = usePlanFollow(section, rendered); return html``; } /** * Scrolls a long plan to the work in hand each time it's drawn, as the TUI's rail does, until the * reader scrolls it themselves. Returns what marks that. * @param {{ current: HTMLElement | null }} section @param {string | null | undefined} rendered */ function usePlanFollow(section, rendered) { const moved = useRef(false); useLayoutEffect(() => { const list = section.current?.querySelector(".todos"); if (!(list instanceof HTMLElement) || moved.current) return; const rows = [...list.children].map((row) => ({ bottom: /** @type {HTMLElement} */ (row).offsetTop + /** @type {HTMLElement} */ (row).offsetHeight, status: row.className.match(/todo-(\S+)/)?.[1] ?? "", })); list.scrollTop = planScroll(rows, list.clientHeight); }, [rendered]); return () => (moved.current = true); } /** The header's button that shows and hides the dock. @param {{ shown: boolean, toggle: () => void }} props */ export function DockToggle({ shown, toggle }) { return html``; } /** * One file's diff until it's closed: over the page on a phone, beside the dock on a wide screen. * @param {{ open: OpenDiff | null, close: () => void, retry: () => void, ask: (words: string) => void }} props */ export function DiffSheet({ open, close, retry, ask }) { const wide = useWide(); useEscape(open ? close : null); if (!open) return null; return html`Connection lost before the diff loaded.
`; } if (open.waiting) return html`Loading diff…
`; /** @type {import("./render.js").Piece} */ const piece = open.diff ? { kind: "diff", diff: open.diff } : { kind: "diff_failed", error: open.error }; return html`<${PickablePiece} piece=${piece} ask=${ask} />`; } /** * Escape closes the sheet, for an iPad with a keyboard. Armed as the sheet reaches the page rather * than after the next paint, so an Escape pressed straight after a pane opens or closes isn't lost. * @param {(() => void) | null} onEscape */ export function useEscape(onEscape) { useLayoutEffect(() => { if (!onEscape) return; /** @param {KeyboardEvent} event */ const listener = (event) => event.key === "Escape" && onEscape(); addEventListener("keydown", listener); return () => removeEventListener("keydown", listener); }, [onEscape]); }