All files / src delivery-graph-client.ts

100% Statements 177/177
71.42% Branches 5/7
100% Functions 5/5
100% Lines 177/177

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 1781x 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  
/** Client-side delivery graph filters + model rebuild (pairs with force-graph runtime). */
export const DELIVERY_GRAPH_CLIENT = `
(function () {
  const payload = window.__GRAPH_PAYLOAD__;
  if (!payload || payload.kind !== "delivery" || !Array.isArray(payload.stories)) return;
  if (!window.MeridianFilterSheet) return;
 
  const vscode = window.__MERIDIAN_VSCODE__ || null;
  const saved = vscode?.getState?.() || {};
  const SPRINT_NONE = window.MeridianFilterSheet.SPRINT_NONE;
 
  function sprintKey(story) {
    return story.sprint || SPRINT_NONE;
  }
 
  function sprintLabel(key) {
    return key === SPRINT_NONE ? "Sem sprint" : key;
  }
 
  function storiesInVersionScope(stories, versions) {
    return stories.filter((s) => versions.has(s.version));
  }
 
  function sprintIdsInScope(stories, versions) {
    const ids = new Set(storiesInVersionScope(stories, versions).map(sprintKey));
    return [...ids].sort((a, b) => {
      if (a === SPRINT_NONE) return 1;
      if (b === SPRINT_NONE) return -1;
      return a.localeCompare(b, undefined, { numeric: true });
    });
  }
 
  function epicIdsInScope(stories, versions) {
    const ids = new Set(storiesInVersionScope(stories, versions).map((s) => s.epic));
    return [...ids].sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
  }
 
  function versionItems() {
    return (payload.versions || []).map((id) => ({
      id,
      label: id,
      title: id,
    }));
  }
 
  function sprintItems(selected) {
    return sprintIdsInScope(payload.stories, selected.version).map((id) => ({
      id,
      label: sprintLabel(id),
      title: sprintLabel(id),
    }));
  }
 
  function epicItems(selected) {
    const byId = new Map((payload.epics || []).map((e) => [e.id, e]));
    return epicIdsInScope(payload.stories, selected.version).map((id) => {
      const epic = byId.get(id);
      return {
        id,
        label: id,
        title: epic?.title || id,
      };
    });
  }
 
  function filterStories(selected) {
    const versions = selected.version;
    const sprints = selected.sprint;
    const epics = selected.epic;
    if (!versions.size || !sprints.size || !epics.size) return [];
    return payload.stories.filter((s) => {
      if (!versions.has(s.version)) return false;
      if (!sprints.has(sprintKey(s))) return false;
      if (!epics.has(s.epic)) return false;
      return true;
    });
  }
 
  function buildGraph(stories) {
    const scopedIds = new Set(stories.map((s) => s.id));
    const nodes = stories.map((s) => ({
      id: s.id,
      label: s.title,
      status: s.status || "",
    }));
    const edges = [];
    const seen = new Set();
    for (const story of stories) {
      for (const dep of story.dependsOn || []) {
        if (!scopedIds.has(dep)) continue;
        const key = story.id + "->" + dep;
        if (seen.has(key)) continue;
        seen.add(key);
        edges.push({ from: story.id, to: dep });
      }
    }
    nodes.sort((a, b) => a.id.localeCompare(b.id));
    edges.sort((a, b) => (a.from + a.to).localeCompare(b.from + b.to));
    return { nodes, edges };
  }
 
  function renderSummary(selected) {
    const el = document.getElementById("filter-summary");
    if (!el) return;
    const scoped = filterStories(selected);
    el.textContent =
      scoped.length +
      " US · " +
      selected.version.size +
      " versão(ões) · " +
      selected.sprint.size +
      " sprint(s) · " +
      selected.epic.size +
      " épico(s)";
  }
 
  const defaultVersions =
    saved.selectedVersions ||
    payload.defaultVersions ||
    (payload.versions || []);
  const defaultVersionSet = new Set(defaultVersions);
  const defaultSprints =
    saved.selectedSprints || sprintIdsInScope(payload.stories, defaultVersionSet);
  const defaultEpics =
    saved.selectedEpics || epicIdsInScope(payload.stories, defaultVersionSet);
 
  try {
    window.MeridianFilterSheet.mount({
    columns: [
      {
        key: "version",
        label: "Versão",
        listId: "version-chips",
        allId: "version-all",
        noneId: "version-none",
        defaultSelected: defaultVersions,
        items: versionItems,
      },
      {
        key: "sprint",
        label: "Sprint",
        listId: "sprint-chips",
        allId: "sprint-all",
        noneId: "sprint-none",
        defaultSelected: defaultSprints,
        items: sprintItems,
      },
      {
        key: "epic",
        label: "Épico",
        listId: "epic-chips",
        allId: "epic-all",
        noneId: "epic-none",
        defaultSelected: defaultEpics,
        items: epicItems,
      },
    ],
    renderSummary,
    onChange(selected) {
      const model = buildGraph(filterStories(selected));
      window.__MERIDIAN_FORCE_GRAPH__?.replot(model);
      const empty = document.querySelector(".empty-overlay");
      if (empty) empty.hidden = model.nodes.length > 0;
    },
    persist(plain) {
      vscode?.setState?.({
        selectedVersions: plain.version,
        selectedSprints: plain.sprint,
        selectedEpics: plain.epic,
      });
    },
  });
  } catch (err) {
    console.error("Delivery graph filters failed to mount", err);
  }
})();
`.trim()