// The projects of a sequence on the panel (owner review 2, R8): each project's name, Main marked, its newest version and who
// changed it last, its versions on demand, and Rename and Delete for the projects other than Main. Nothing here edits a
// project's settings: the desktop does, in its workspace, and pushes its versions here. The list follows projectChanged.
import { t, plural, applyStrings } from "../../i18n.js";
import { escapeHtml, attr, formatNumber, formatDateTime, toast, errorText, confirmDialog, formDialog, emptyState, markInvalid, redraw, fitColumns } from "../../ui.js";
/** Versions read at a time when a project is opened, and with each Load more. */
const VERSION_PAGE = 20;
/** The most versions one read of them gives (ProjectEndpoints.MaxPage): a refresh reads again as many as are shown, up to this. */
const MAX_PAGE = 200;
/** A project name, as the server takes it (ProjectService.MaxNameLength). */
const MAX_NAME = 60;
/** The name every sequence's Main project is made with; shown in the panel's language. */
const MAIN_NAME = "Main";
/** How long after a projectChanged the projects are read again; one read covers every change meanwhile. */
const AFTER_CHANGE_MS = 500;
/** Version kinds the panel names (ProjectVersionKinds). */
const KINDS = { commit: "Commit", merge: "Merge", restore: "Restore", imported: "Imported", external: "External" };
/**
* Draws the Projects section of sequence sequenceId into host (its count into meta) and keeps it current. Returns
* onEvent(e), which the sequence page passes every server event to.
*/
export function projectsSection(host, meta, { api, sequenceId }) {
const base = `/api/v1/sequences/${encodeURIComponent(sequenceId)}/projects`;
let projects = [];
/** The versions read of each project opened: { items, more }. */
const versions = new Map();
const expanded = new Set();
let timer = null;
let failed = null;
/** The projects a projectChanged named since the last read; null after a reconnection, when any may have changed. */
let changed = new Set();
host.innerHTML = `
`;
applyStrings(host);
const table = host.querySelector(".table");
const rows = host.querySelector("#project-rows");
/**
* Reads the projects again, and the versions of those opened that may have changed (every one when stale is null): as
* many as are shown, so the pages Load more read stay.
*/
async function load(stale = null) {
try {
projects = await api.get(base);
failed = null;
// A project deleted meanwhile (here or elsewhere) closes; the versions of the others that changed are read again.
for (const id of expanded) {
if (!projects.some((p) => p.id === id)) forget(id);
}
const again = [...expanded].filter((id) => stale === null || stale.has(id));
await Promise.all(again.map((id) => reloadVersions(id).catch(() => forget(id))));
} catch (err) {
projects = [];
failed = errorText(err);
}
if (!host.isConnected) return;
meta.textContent = failed ? "" : plural("Admin.Projects.Count", projects.length);
draw();
}
/**
* The newest versions of a project, or with more the next page after those shown. Read without their settings
* (summary): the panel shows none, and a large sequence's versions are megabytes each.
*/
async function loadVersions(id, more = false, limit = VERSION_PAGE) {
const known = versions.get(id);
const before = more && known?.items.length ? `&before=${known.items[known.items.length - 1].number}` : "";
const page = await api.get(`${base}/${encodeURIComponent(id)}/versions?limit=${limit}${before}&summary=true`);
versions.set(id, { items: more && known ? known.items.concat(page.items) : page.items, more: page.more });
}
/** The newest versions of a project read again, as many as are shown (at least a page, at most MAX_PAGE). */
function reloadVersions(id) {
const shown = versions.get(id)?.items.length ?? 0;
return loadVersions(id, false, Math.min(MAX_PAGE, Math.max(VERSION_PAGE, shown)));
}
function forget(id) {
expanded.delete(id);
versions.delete(id);
}
function draw() {
redraw(rows, () => {
rows.innerHTML = failed
? `
${escapeHtml(failed)}
`
: projects.map(rowHtml).join("") || `
${emptyState("Admin.Projects.Empty")}
`;
applyStrings(rows);
});
rows.querySelectorAll("[data-toggle]").forEach((b) => b.addEventListener("click", () => toggle(b.dataset.toggle)));
rows.querySelectorAll("[data-act]").forEach((b) => b.addEventListener("click", () => act(b.dataset.act, find(b.closest("[data-key]").dataset.key))));
rows.querySelectorAll("[data-more-versions]").forEach((b) => b.addEventListener("click", () => moreVersions(b.dataset.moreVersions)));
fitColumns(table);
}
function find(id) {
return projects.find((p) => p.id === id);
}
/** Main keeps the name it was made with in the panel's language; a Main renamed shows its name and the Main mark. */
function projectName(p) {
return p.isMain && p.name === MAIN_NAME ? t("Admin.Projects.Main") : p.name;
}
function rowHtml(p) {
const name = projectName(p);
const mark = p.isMain && p.name !== MAIN_NAME
? `` : "";
const who = p.headAuthor ?? p.headClientName ?? "";
const when = p.headStoredAt ?? p.updatedAt;
const actions = p.isMain ? "" : `
`;
return `