mirror of
https://github.com/samsonjs/spirit-tracker.git
synced 2026-04-27 15:07:43 +00:00
feat: Better URL rendering
This commit is contained in:
parent
f33ff16419
commit
198d3abe19
2 changed files with 75 additions and 26 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
/* viz/app/linker_page.js */
|
|
||||||
import { esc, renderThumbHtml } from "./dom.js";
|
import { esc, renderThumbHtml } from "./dom.js";
|
||||||
import {
|
import {
|
||||||
tokenizeQuery,
|
tokenizeQuery,
|
||||||
|
|
@ -91,6 +90,7 @@ function storesOverlap(aItem, bItem) {
|
||||||
const b = bItem?.stores;
|
const b = bItem?.stores;
|
||||||
if (!a || !b) return false;
|
if (!a || !b) return false;
|
||||||
|
|
||||||
|
// stores are Set(storeLabel). Exact-label overlap is the intended rule.
|
||||||
for (const s of a) {
|
for (const s of a) {
|
||||||
if (b.has(s)) return true;
|
if (b.has(s)) return true;
|
||||||
}
|
}
|
||||||
|
|
@ -115,6 +115,7 @@ function isBCStoreLabel(label) {
|
||||||
return s.includes("bcl") || s.includes("strath");
|
return s.includes("bcl") || s.includes("strath");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// infer BC-ness by checking any row for that skuKey in current index
|
||||||
function skuIsBC(allRows, skuKey) {
|
function skuIsBC(allRows, skuKey) {
|
||||||
for (const r of allRows) {
|
for (const r of allRows) {
|
||||||
if (keySkuForRow(r) !== skuKey) continue;
|
if (keySkuForRow(r) !== skuKey) continue;
|
||||||
|
|
@ -143,8 +144,16 @@ function topSuggestions(allAgg, limit, otherPinnedSku, mappedSkus) {
|
||||||
return scored.slice(0, limit).map((x) => x.it);
|
return scored.slice(0, limit).map((x) => x.it);
|
||||||
}
|
}
|
||||||
|
|
||||||
function recommendSimilar(allAgg, pinned, limit, otherPinnedSku, mappedSkus, isIgnoredPairFn) {
|
function recommendSimilar(
|
||||||
if (!pinned || !pinned.name) return topSuggestions(allAgg, limit, otherPinnedSku, mappedSkus);
|
allAgg,
|
||||||
|
pinned,
|
||||||
|
limit,
|
||||||
|
otherPinnedSku,
|
||||||
|
mappedSkus,
|
||||||
|
isIgnoredPairFn
|
||||||
|
) {
|
||||||
|
if (!pinned || !pinned.name)
|
||||||
|
return topSuggestions(allAgg, limit, otherPinnedSku, mappedSkus);
|
||||||
|
|
||||||
const base = String(pinned.name || "");
|
const base = String(pinned.name || "");
|
||||||
const pinnedSku = String(pinned.sku || "");
|
const pinnedSku = String(pinned.sku || "");
|
||||||
|
|
@ -157,8 +166,14 @@ function recommendSimilar(allAgg, pinned, limit, otherPinnedSku, mappedSkus, isI
|
||||||
if (it.sku === pinned.sku) continue;
|
if (it.sku === pinned.sku) continue;
|
||||||
if (otherPinnedSku && String(it.sku) === String(otherPinnedSku)) continue;
|
if (otherPinnedSku && String(it.sku) === String(otherPinnedSku)) continue;
|
||||||
|
|
||||||
|
// never suggest same-store pairs
|
||||||
if (storesOverlap(pinned, it)) continue;
|
if (storesOverlap(pinned, it)) continue;
|
||||||
if (typeof isIgnoredPairFn === "function" && isIgnoredPairFn(pinnedSku, String(it.sku || ""))) continue;
|
|
||||||
|
if (
|
||||||
|
typeof isIgnoredPairFn === "function" &&
|
||||||
|
isIgnoredPairFn(pinnedSku, String(it.sku || ""))
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
|
||||||
const s = similarityScore(base, it.name || "");
|
const s = similarityScore(base, it.name || "");
|
||||||
if (s > 0) scored.push({ it, s });
|
if (s > 0) scored.push({ it, s });
|
||||||
|
|
@ -167,6 +182,7 @@ function recommendSimilar(allAgg, pinned, limit, otherPinnedSku, mappedSkus, isI
|
||||||
return scored.slice(0, limit).map((x) => x.it);
|
return scored.slice(0, limit).map((x) => x.it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FAST initial pairing (approx) with ignore-pair exclusion + same-store exclusion
|
||||||
function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn) {
|
function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn) {
|
||||||
const items = allAgg.filter((it) => {
|
const items = allAgg.filter((it) => {
|
||||||
if (!it) return false;
|
if (!it) return false;
|
||||||
|
|
@ -183,7 +199,9 @@ function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn
|
||||||
const itemNormName = new Map();
|
const itemNormName = new Map();
|
||||||
|
|
||||||
for (const it of items) {
|
for (const it of items) {
|
||||||
const toks = Array.from(new Set(tokenizeQuery(it.name || ""))).filter(Boolean).slice(0, 10);
|
const toks = Array.from(new Set(tokenizeQuery(it.name || "")))
|
||||||
|
.filter(Boolean)
|
||||||
|
.slice(0, 10);
|
||||||
itemTokens.set(it.sku, toks);
|
itemTokens.set(it.sku, toks);
|
||||||
itemNormName.set(it.sku, normSearchText(it.name || ""));
|
itemNormName.set(it.sku, normSearchText(it.name || ""));
|
||||||
for (const t of toks) {
|
for (const t of toks) {
|
||||||
|
|
@ -215,7 +233,13 @@ function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn
|
||||||
if (mappedSkus && mappedSkus.has(bSku)) continue;
|
if (mappedSkus && mappedSkus.has(bSku)) continue;
|
||||||
if (isUnknownSkuKey(bSku)) continue;
|
if (isUnknownSkuKey(bSku)) continue;
|
||||||
|
|
||||||
if (typeof isIgnoredPairFn === "function" && isIgnoredPairFn(aSku, bSku)) continue;
|
if (
|
||||||
|
typeof isIgnoredPairFn === "function" &&
|
||||||
|
isIgnoredPairFn(aSku, bSku)
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// never suggest same-store pairs
|
||||||
if (storesOverlap(a, b)) continue;
|
if (storesOverlap(a, b)) continue;
|
||||||
|
|
||||||
cand.set(bSku, b);
|
cand.set(bSku, b);
|
||||||
|
|
@ -251,7 +275,8 @@ function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn
|
||||||
const bSku = String(bestB.sku || "");
|
const bSku = String(bestB.sku || "");
|
||||||
const key = aSku < bSku ? `${aSku}|${bSku}` : `${bSku}|${aSku}`;
|
const key = aSku < bSku ? `${aSku}|${bSku}` : `${bSku}|${aSku}`;
|
||||||
const prev = bestByPair.get(key);
|
const prev = bestByPair.get(key);
|
||||||
if (!prev || bestS > prev.score) bestByPair.set(key, { a, b: bestB, score: bestS });
|
if (!prev || bestS > prev.score)
|
||||||
|
bestByPair.set(key, { a, b: bestB, score: bestS });
|
||||||
}
|
}
|
||||||
|
|
||||||
const pairs = Array.from(bestByPair.values());
|
const pairs = Array.from(bestByPair.values());
|
||||||
|
|
@ -264,6 +289,7 @@ function computeInitialPairsFast(allAgg, mappedSkus, limitPairs, isIgnoredPairFn
|
||||||
const bSku = String(p.b.sku || "");
|
const bSku = String(p.b.sku || "");
|
||||||
if (!aSku || !bSku || aSku === bSku) continue;
|
if (!aSku || !bSku || aSku === bSku) continue;
|
||||||
if (used.has(aSku) || used.has(bSku)) continue;
|
if (used.has(aSku) || used.has(bSku)) continue;
|
||||||
|
|
||||||
if (storesOverlap(p.a, p.b)) continue;
|
if (storesOverlap(p.a, p.b)) continue;
|
||||||
|
|
||||||
used.add(aSku);
|
used.add(aSku);
|
||||||
|
|
@ -333,7 +359,7 @@ export async function renderSkuLinker($app) {
|
||||||
const idx = await loadIndex();
|
const idx = await loadIndex();
|
||||||
const allRows = Array.isArray(idx.items) ? idx.items : [];
|
const allRows = Array.isArray(idx.items) ? idx.items : [];
|
||||||
|
|
||||||
// skuKey -> storeLabel -> url
|
// skuKey -> storeLabel -> url (used to ensure store badge uses matching URL)
|
||||||
const URL_BY_SKU_STORE = new Map();
|
const URL_BY_SKU_STORE = new Map();
|
||||||
for (const r of allRows) {
|
for (const r of allRows) {
|
||||||
if (!r || r.removed) continue;
|
if (!r || r.removed) continue;
|
||||||
|
|
@ -349,11 +375,12 @@ export async function renderSkuLinker($app) {
|
||||||
if (!m.has(storeLabel)) m.set(storeLabel, url);
|
if (!m.has(storeLabel)) m.set(storeLabel, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// candidates for this page (hide unknown u: entirely)
|
||||||
const allAgg = aggregateBySku(allRows, (x) => x).filter((it) => !isUnknownSkuKey(it.sku));
|
const allAgg = aggregateBySku(allRows, (x) => x).filter((it) => !isUnknownSkuKey(it.sku));
|
||||||
|
|
||||||
const meta = await loadSkuMetaBestEffort();
|
const meta = await loadSkuMetaBestEffort();
|
||||||
const mappedSkus = buildMappedSkuSet(meta.links || []);
|
const mappedSkus = buildMappedSkuSet(meta.links || []);
|
||||||
const ignoreSet = rules.ignoreSet;
|
const ignoreSet = rules.ignoreSet; // already canonicalized as "a|b"
|
||||||
|
|
||||||
function isIgnoredPair(a, b) {
|
function isIgnoredPair(a, b) {
|
||||||
return rules.isIgnoredPair(String(a || ""), String(b || ""));
|
return rules.isIgnoredPair(String(a || ""), String(b || ""));
|
||||||
|
|
@ -370,6 +397,7 @@ export async function renderSkuLinker($app) {
|
||||||
const price = it.cheapestPriceStr ? it.cheapestPriceStr : "(no price)";
|
const price = it.cheapestPriceStr ? it.cheapestPriceStr : "(no price)";
|
||||||
const store = it.cheapestStoreLabel || ([...it.stores][0] || "Store");
|
const store = it.cheapestStoreLabel || ([...it.stores][0] || "Store");
|
||||||
|
|
||||||
|
// IMPORTANT: link must match displayed store label
|
||||||
const href =
|
const href =
|
||||||
URL_BY_SKU_STORE.get(String(it.sku || ""))?.get(String(store || "")) ||
|
URL_BY_SKU_STORE.get(String(it.sku || ""))?.get(String(store || "")) ||
|
||||||
String(it.sampleUrl || "").trim() ||
|
String(it.sampleUrl || "").trim() ||
|
||||||
|
|
@ -383,22 +411,22 @@ export async function renderSkuLinker($app) {
|
||||||
)}${esc(plus)}</a>`
|
)}${esc(plus)}</a>`
|
||||||
: `<span class="badge">${esc(store)}${esc(plus)}</span>`;
|
: `<span class="badge">${esc(store)}${esc(plus)}</span>`;
|
||||||
|
|
||||||
|
const pinnedBadge = pinned ? `<span class="badge">PINNED</span>` : ``;
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<div class="item ${pinned ? "pinnedItem" : ""}" data-sku="${esc(it.sku)}">
|
<div class="item ${pinned ? "pinnedItem" : ""}" data-sku="${esc(it.sku)}">
|
||||||
<div class="itemRow">
|
<div class="itemRow">
|
||||||
<div class="thumbBox">${renderThumbHtml(it.img)}</div>
|
<div class="thumbBox">${renderThumbHtml(it.img)}</div>
|
||||||
|
|
||||||
<div class="itemBody">
|
<div class="itemBody">
|
||||||
<div class="itemMain">
|
<div class="itemTop">
|
||||||
<div class="itemName">${esc(it.name || "(no name)")}</div>
|
<div class="itemName">${esc(it.name || "(no name)")}</div>
|
||||||
${pinned ? `<div class="small">Pinned (click again to unpin)</div>` : ``}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="itemFacts">
|
|
||||||
<div class="mono priceBig">${esc(price)}</div>
|
|
||||||
${storeBadge}
|
|
||||||
<span class="badge mono">${esc(displaySku(it.sku))}</span>
|
<span class="badge mono">${esc(displaySku(it.sku))}</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="metaRow">
|
||||||
|
${pinnedBadge}
|
||||||
|
<span class="mono price">${esc(price)}</span>
|
||||||
|
${storeBadge}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -531,8 +559,11 @@ export async function renderSkuLinker($app) {
|
||||||
$ignoreBtn.disabled = false;
|
$ignoreBtn.disabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isIgnoredPair(a, b)) $status.textContent = "This pair is already ignored.";
|
if (isIgnoredPair(a, b)) {
|
||||||
else if ($status.textContent === "Pin one item on each side to enable linking / ignoring.") $status.textContent = "";
|
$status.textContent = "This pair is already ignored.";
|
||||||
|
} else if ($status.textContent === "Pin one item on each side to enable linking / ignoring.") {
|
||||||
|
$status.textContent = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateAll() {
|
function updateAll() {
|
||||||
|
|
@ -584,12 +615,18 @@ export async function renderSkuLinker($app) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Direction: if either is BC-based, FROM is BC sku.
|
||||||
const aBC = skuIsBC(allRows, a);
|
const aBC = skuIsBC(allRows, a);
|
||||||
const bBC = skuIsBC(allRows, b);
|
const bBC = skuIsBC(allRows, b);
|
||||||
|
|
||||||
let fromSku = a, toSku = b;
|
let fromSku = a, toSku = b;
|
||||||
if (aBC && !bBC) { fromSku = a; toSku = b; }
|
if (aBC && !bBC) {
|
||||||
else if (bBC && !aBC) { fromSku = b; toSku = a; }
|
fromSku = a;
|
||||||
|
toSku = b;
|
||||||
|
} else if (bBC && !aBC) {
|
||||||
|
fromSku = b;
|
||||||
|
toSku = a;
|
||||||
|
}
|
||||||
|
|
||||||
$status.textContent = `Writing: ${displaySku(fromSku)} → ${displaySku(toSku)} …`;
|
$status.textContent = `Writing: ${displaySku(fromSku)} → ${displaySku(toSku)} …`;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,7 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
background: #0f1318;
|
background: #0f1318;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
min-height: 92px; /* keep results uniform */
|
||||||
}
|
}
|
||||||
|
|
||||||
.item:hover { border-color: #2f3a46; }
|
.item:hover { border-color: #2f3a46; }
|
||||||
|
|
@ -81,20 +82,21 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
.itemRow {
|
.itemRow {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
align-items: stretch; /* let thumb fill card height */
|
align-items: center; /* keep thumb centered; don't stretch */
|
||||||
|
min-height: 68px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.thumbBox {
|
.thumbBox {
|
||||||
width: 64px;
|
width: 64px;
|
||||||
|
height: 64px; /* fixed thumb height */
|
||||||
border-radius: 12px;
|
border-radius: 12px;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
background: #0b0d10;
|
background: #0b0d10;
|
||||||
flex: 0 0 64px;
|
flex: 0 0 64px;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: stretch;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
min-height: 64px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.thumb {
|
.thumb {
|
||||||
|
|
@ -113,6 +115,9 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
.itemBody {
|
.itemBody {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.itemTop {
|
.itemTop {
|
||||||
|
|
@ -126,6 +131,9 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
|
white-space: nowrap; /* lock to one line */
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
.badge {
|
.badge {
|
||||||
|
|
@ -144,8 +152,9 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
margin-top: 8px;
|
margin-top: 8px;
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 10px;
|
gap: 10px;
|
||||||
flex-wrap: wrap;
|
flex-wrap: nowrap; /* keep single row (desktop) */
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
overflow: hidden;
|
||||||
color: var(--muted);
|
color: var(--muted);
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
|
|
@ -238,7 +247,10 @@ a.badge:hover { text-decoration: underline; cursor: pointer; }
|
||||||
|
|
||||||
@media (max-width: 640px) {
|
@media (max-width: 640px) {
|
||||||
.container { padding: 14px; }
|
.container { padding: 14px; }
|
||||||
.thumbBox { width: 56px; flex: 0 0 56px; min-height: 56px; }
|
.thumbBox { width: 56px; height: 56px; flex: 0 0 56px; }
|
||||||
|
.item { min-height: 0; } /* allow natural height on small screens */
|
||||||
|
.metaRow { flex-wrap: wrap; } /* wrapping is fine on mobile */
|
||||||
|
|
||||||
.detailThumbBox { width: 84px; height: 84px; flex: 0 0 84px; }
|
.detailThumbBox { width: 84px; height: 84px; flex: 0 0 84px; }
|
||||||
|
|
||||||
.chartBox {
|
.chartBox {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue