HyperFlow / extension /src /render.ts
Gaurav711's picture
Deploy 891f179
2b86c60 verified
Raw History Blame Contribute Delete
3.04 kB
// 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);
}