// @ts-check
// The Files tab: the project as the TUI's FILES tree, drawn by the host so its guides, marks and
// change counts are the TUI's, with folders unfolding in place, and a file read in place,
// highlighted as the host renders it, with a way to mention it to the agent.
import { useEffect } from "preact/hooks";
import { useRendered } from "./render.js";
import { fileMention, treeRows } from "./files.js";
import { html, landedIn } from "./html.js";
import { useEscape } from "./dock.js";
import { Icon } from "./icons.js";
import { PickablePiece } from "./lines-view.js";
/** @import { PanelHooks } from "./panel.js" */
/** @import { OpenFile } from "./files.js" */
/** @param {{ hooks: PanelHooks }} props */
export function FilesTab({ hooks }) {
return html`<${TreeView} hooks=${hooks} />`;
}
/** @param {{ hooks: PanelHooks }} props */
function TreeView({ hooks }) {
const { files, listings, changes } = hooks;
useFreshFolders(hooks);
const rows = treeRows(listings.folders.value, files.unfolded.value);
const rendered = useRendered({ kind: "tree", rows, changes }, false);
return html`
`;
}
/**
* Lists the root and every open folder afresh when the tree is shown, since the agent may have
* changed them since. @param {PanelHooks} hooks
*/
function useFreshFolders({ files, listings, send }) {
useEffect(() => {
for (const folder of ["", ...files.unfolded.value]) listings.ask(folder, send, { fresh: true });
}, []);
}
/** A tap on a folder unfolds or folds it; one on a file opens it. @param {MouseEvent} event @param {PanelHooks} hooks */
export function tapRow(event, { files, github, review, listings, send }) {
const row = landedIn(event, "button.tree-row");
const folder = row?.dataset.folder;
if (folder !== undefined) {
files.toggle(folder);
if (files.unfolded.value.has(folder)) listings.ask(folder, send, { fresh: true });
return;
}
const file = row?.dataset.file;
if (file === undefined) return;
github.close();
review.close();
files.read(file, send);
}
/**
* The open file, in the pane a diff opens in: over the page on a phone, beside the tree on a wide
* screen, as the TUI peeks a file beside its dock. Escape closes it before the panel.
* @param {{ hooks: PanelHooks, wide: boolean }} props
*/
export function FilePeek({ hooks, wide }) {
const { files, send } = hooks;
const open = files.open.value;
useEscape(open ? files.close : null);
if (!open) return null;
return html`