import { stat } from "node:fs/promises";
import PieceFrame from "@/components/piece/PieceFrame";
import type { Crumb } from "@/components/shell/AppHeader";
import { CUT_NAMES, type Song } from "@/lib/browse";
import { projectHref, projectsHref } from "@/lib/hrefs";
import type { NoteMap } from "@/lib/notes";
import { readPieceFrame, type PieceFrame as Frame } from "@/lib/pieces/frame";
import { SONG_PIECE_TABS } from "@/lib/pieces/song-tabs.mjs";
import { specFile } from "@/lib/spec";
import type { ProjectRef } from "@/lib/project-types";
// ---------------------------------------------------------------------------
// A SONG PIECE's frame (plans/umtool-redesign.md, P4a-3): the P4a frame
// (components/piece/PieceFrame.tsx -- head, stepper, lifecycle, Next, tabs,
// keys) around one of a song's five tabs, Overview · Cuts · Spec · Cover ·
// Notes (lib/pieces/song-tabs.mjs). The song page and a cut's page (a view of
// the Cuts tab) both draw through it.
//
// The stepper is Spec → Cuts → Done, by the one stage rule (lib/pieces/tabs.mjs
// pieceSteps on the flow the song kind declares). The page has the song in
// hand, so the facts that light it are read fresh here (a promote shows at
// once); Work's facet reads the same facts off the pieces graph.
// ---------------------------------------------------------------------------
/** What a song piece has reached, from the song the page already read (one stat for spec.json). */
export async function songReachedNow(song: Song): Promise<{ spec: boolean; cuts: boolean; done: boolean }> {
const spec = await stat(specFile(song.id)).then(
() => true,
() => false,
);
return { spec, cuts: song.present > 0, done: song.present === CUT_NAMES.length };
}
export async function readSongFrame(project: ProjectRef, song: Song): Promise {
return readPieceFrame(project, { reached: await songReachedNow(song) });
}
/** A song tab's count, in Meter: cuts built, covers on offer, notes written. */
export function songCounts(song: Song, notes: NoteMap, covers: number): Partial> {
const written = Object.values(notes).filter((n) => n && typeof n.note === "string" && n.note.trim()).length;
return { cuts: song.present, cover: covers, notes: written };
}
export default function SongFrame({
project,
song,
frame,
tab,
counts,
note,
meta,
idHref,
bare = false,
children,
}: {
project: ProjectRef;
song: Song;
frame: Frame;
tab: string;
counts: Partial>;
/** The head's status line. */
note?: React.ReactNode;
/** A line beside the title (a cut's page: which cut). */
meta?: React.ReactNode;
/** Link the trail's project id (a cut's page: back to the song). */
idHref?: string;
/** The body pads itself (a cut's page: the bench is edge to edge). */
bare?: boolean;
children: React.ReactNode;
}) {
const title = frame.piece?.title ?? song.title;
const crumbs: Crumb[] = [
{ href: projectsHref(), label: "Projects" },
{ label: project.id, ...(idHref ? { href: idHref } : {}) },
{ label: title },
];
return (
({ ...t, href: projectHref(project.id, { tab: t.id }) }))}
counts={counts}
note={note}
meta={meta}
more={{project.id}}
>
{children}
);
}