File size: 3,040 Bytes
2b86c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Popup rendering. Every string from a web page is untrusted and is only ever set as textContent.
import type { Report } from './analyze';

const rupees = (minor: number) => `₹${(minor / 100).toFixed(2)}`;

function el<K extends keyof HTMLElementTagNameMap>(tag: K, text?: string, className?: string): HTMLElementTagNameMap[K] {
  const node = document.createElement(tag);
  if (text !== undefined) node.textContent = text;
  if (className) node.className = className;
  return node;
}

export type ConfirmHandler = (lineIndex: number, store: string) => void;

export function renderUnsupported(root: HTMLElement, store: string, reason: string): void {
  root.replaceChildren(el('h1', 'Cart Guard'), el('p', `This ${store} page layout is not supported yet, so no numbers are shown.`, 'warn'),
    el('p', reason, 'muted'));
}

export function renderReport(root: HTMLElement, report: Report, onConfirm?: ConfirmHandler): void {
  const findings = el('section');
  findings.append(el('h2', 'Fee X-ray'));
  if (!report.findings.length) findings.append(el('p', 'No fee issues found on this cart.', 'muted'));
  for (const f of report.findings) {
    const item = el('div', undefined, `finding ${f.severity}`);
    item.append(el('strong', f.message));
    if (f.evidence.length) item.append(el('p', 'From the page: ' + f.evidence.join(' · '), 'muted'));
    findings.append(item);
  }

  const split = el('section');
  split.append(el('h2', 'Cheapest split'));
  const s = report.split;
  if (!s || s.status !== 'optimal') {
    split.append(el('p', 'No split available for this cart.', 'muted'));
  } else {
    const table = el('table');
    for (const [store, total] of Object.entries(s.storeTotalsMinor)) {
      const row = el('tr');
      row.append(el('td', store), el('td', rupees(total)));
      table.append(row);
    }
    split.append(table, el('p', `Total ${rupees(s.totalMinor!)}` +
      (s.savingsMinor ? `, ${rupees(s.savingsMinor)} less than the best single store (${s.bestSingleStore.store})` : '')));
    if (s.unpurchasable.length) split.append(el('p', `${s.unpurchasable.length} item(s) not available anywhere.`, 'warn'));
  }

  const matches = el('section');
  matches.append(el('h2', 'Matches to confirm'));
  report.matches.forEach(({ line, matches: found }, lineIndex) => {
    for (const m of found.filter((x) => !x.confirmed)) {
      const button = el('button', 'Same product');
      button.type = 'button';
      button.addEventListener('click', () => onConfirm?.(lineIndex, m.seen.store));
      const row = el('div', undefined, 'match');
      row.append(el('span', `${line} ↔ ${m.seen.name} (${m.seen.store}, ${rupees(m.seen.priceMinor)})`), button);
      matches.append(row);
    }
  });
  if (matches.childElementCount === 1) matches.append(el('p', 'Nothing to confirm.', 'muted'));

  const notes = el('details');
  notes.append(el('summary', 'Assumptions'), ...report.assumptions.map((a) => el('p', a, 'muted')));
  root.replaceChildren(el('h1', 'Cart Guard'), findings, split, matches, notes);
}