All files / src epics-webview-html.ts

100% Statements 221/221
69.23% Branches 9/13
100% Functions 8/8
100% Lines 221/221

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 2221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import * as crypto from "node:crypto"
 
import { planningPayloadForListViews, type PlanningPayload } from "./planning-payload.js"
import {
  FILTER_CHIP_SCRIPT,
  PAGINATION_SCRIPT,
  PLANNING_DETAIL_SCRIPT,
  PLANNING_WEBVIEW_STYLES,
  planningWebviewShell,
} from "./webview-common.js"
import {
  PROJECT_CONTEXT_SCRIPT,
  PROJECT_CONTEXT_STYLES,
  projectContextToolbarHtml,
  type WebviewProjectContext,
} from "./webview-project-context.js"
 
export function epicsWebviewHtml(
  payload: PlanningPayload,
  context: WebviewProjectContext,
): string {
  const nonce = crypto.randomBytes(16).toString("hex")
  const dataJson = JSON.stringify(planningPayloadForListViews(payload))
  const contextJson = JSON.stringify(context)
  const body = `
  <div class="toolbar">
    ${projectContextToolbarHtml(context)}
    <div class="toolbar-row">
      <span class="toolbar-label">Version</span>
      <button type="button" class="chip" id="version-all">All</button>
      <button type="button" class="chip" id="version-none">None</button>
      <div id="version-chips" class="chip-group"></div>
      <span class="count" id="summary"></span>
    </div>
  </div>
  <div class="main" id="list"></div>
  <div class="pager" id="pager"></div>`
  const script = `
    const vscode = acquireVsCodeApi();
    const payload = ${dataJson};
    const projectContext = ${contextJson};
    ${PROJECT_CONTEXT_SCRIPT}
    wireProjectContext(projectContext);
    ${FILTER_CHIP_SCRIPT}
    ${PAGINATION_SCRIPT}
    ${PLANNING_DETAIL_SCRIPT}
    const STATE_VERSION = 6;
    const saved = vscode.getState() || {};
    let selectedVersions = new Set(
      saved.stateVersion === STATE_VERSION && saved.selectedVersions
        ? saved.selectedVersions
        : payload.defaultVersions,
    );
    let expanded = new Set(
      saved.stateVersion === STATE_VERSION && saved.expanded
        ? saved.expanded
        : [],
    );
    let pageSize = saved.stateVersion === STATE_VERSION
      ? normalizePageSize(saved.pageSize)
      : DEFAULT_PAGE_SIZE;
    let currentPage = saved.stateVersion === STATE_VERSION && saved.currentPage
      ? saved.currentPage
      : 1;
 
    function versionItems() {
      return payload.versions.map((v) => ({
        id: v.id,
        label: v.id,
        title: (selectedVersions.has(v.id) ? "Hide" : "Show") + " " + v.id + " — " + v.title,
      }));
    }
 
    function storiesInVersions() {
      if (selectedVersions.size === 0) return [];
      return payload.stories.filter((s) => selectedVersions.has(s.version));
    }
 
    function epicsInScope(scoped) {
      const ids = new Set(scoped.map((s) => s.epic));
      return payload.epics.filter((e) => ids.has(e.id));
    }
 
    function filteredEpics() {
      const scoped = storiesInVersions();
      const inScope = new Set(epicsInScope(scoped).map((e) => e.id));
      return payload.epics.filter((e) => inScope.has(e.id));
    }
 
    function renderToolbar() {
      const vItems = versionItems();
      const noVer = selectedVersions.size === 0;
      const allVer = vItems.length > 0 && vItems.every((v) => selectedVersions.has(v.id));
      wireAllNone(
        document.getElementById("version-all"),
        document.getElementById("version-none"),
        allVer,
        noVer,
        false,
        () => { selectedVersions = new Set(vItems.map((v) => v.id)); currentPage = 1; persist(); renderAll(); },
        () => { selectedVersions = new Set(); currentPage = 1; persist(); renderAll(); },
      );
      renderChipGroup(document.getElementById("version-chips"), vItems, selectedVersions, false, (id) => {
        if (selectedVersions.has(id)) selectedVersions.delete(id);
        else selectedVersions.add(id);
        currentPage = 1;
        persist();
        renderAll();
      });
 
      const scoped = storiesInVersions();
      const epics = epicsInScope(scoped);
      document.getElementById("summary").textContent =
        noVer ? "0 epics" : epics.length + " epic(s) · " + scoped.length + " stories";
    }
 
    function renderList() {
      const root = document.getElementById("list");
      if (selectedVersions.size === 0) {
        root.innerHTML = '<p class="empty">No versions selected</p>';
        renderPager(document.getElementById("pager"), 0, 1, pageSize, () => {}, () => {});
        return;
      }
      const scoped = storiesInVersions();
      const all = filteredEpics();
      const paged = pageSlice(all, currentPage, pageSize);
      currentPage = paged.page;
      root.innerHTML = "";
      if (!all.length) {
        root.innerHTML = '<p class="empty">No epics for selected versions</p>';
      } else {
        for (const epic of paged.slice) {
          const open = expanded.has(epic.id);
          const epicStories = scoped.filter((s) => s.epic === epic.id);
          const p = progress(epicStories);
          const block = document.createElement("div");
          block.className = "block" + (open ? " open" : "");
 
          const head = document.createElement("div");
          head.className = "block-head";
          const toggle = () => {
            if (expanded.has(epic.id)) expanded.delete(epic.id);
            else expanded.add(epic.id);
            persist();
            renderList();
          };
          head.append(
            makeAccordionHead(open, toggle),
            makeIdLink(epic.id, () => vscode.postMessage({ type: "openEpic", id: epic.id })),
          );
          const title = document.createElement("span");
          title.className = "row-title";
          title.textContent = epic.title;
          const wrap = document.createElement("div");
          wrap.className = "progress-wrap";
          const bar = document.createElement("div");
          bar.className = "progress-bar";
          const fill = document.createElement("div");
          fill.className = "progress-fill" + (p.pct === 100 ? " done" : "");
          fill.style.width = p.pct + "%";
          bar.appendChild(fill);
          const progText = document.createElement("span");
          progText.className = "progress-text";
          progText.textContent = p.done + "/" + p.total;
          wrap.append(bar, progText);
          head.append(title, makeBadge(epic.status), wrap);
          block.appendChild(head);
 
          if (open) {
            const bodyEl = document.createElement("div");
            bodyEl.className = "block-body";
            appendDetailLine(bodyEl, "Outcome:", epic.outcome);
            const versionsInFilter = epic.versions.filter((v) => selectedVersions.has(v));
            if (versionsInFilter.length) {
              appendDetailLine(bodyEl, "Versions:", versionsInFilter.join(", "));
            }
            appendProgressBar(bodyEl, p.done, p.total);
            const storiesSection = appendSection(bodyEl, "User stories in selected versions");
            const storyIds = epicStories.map((s) => s.id);
            appendStoryRows(
              storiesSection,
              storyIds,
              payload.stories,
              "No stories for this epic in the selected versions.",
            );
            const hint = document.createElement("p");
            hint.className = "meta-line";
            hint.textContent = "Open epic file for capability, expected outcome, and out of scope.";
            bodyEl.appendChild(hint);
            block.appendChild(bodyEl);
          }
          root.appendChild(block);
        }
      }
      currentPage = renderPager(
        document.getElementById("pager"),
        all.length,
        currentPage,
        pageSize,
        (p) => { currentPage = p; persist(); renderList(); },
        (size) => { pageSize = size; currentPage = 1; persist(); renderAll(); },
      );
    }
 
    function persist() {
      vscode.setState({
        stateVersion: STATE_VERSION,
        selectedVersions: [...selectedVersions],
        expanded: [...expanded],
        pageSize,
        currentPage,
      });
    }
    function renderAll() {
      renderToolbar();
      renderList();
    }
    renderAll();
  `
  return planningWebviewShell(nonce, PLANNING_WEBVIEW_STYLES + PROJECT_CONTEXT_STYLES, body, script)
}