Files
descrybe/apps/web/src/lib/products-search.ts
T
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

249 lines
7.1 KiB
TypeScript

/**
* Products list URL search/filter parsing helpers.
*/
export type ProductTab = "processed" | "needs_review" | "error" | "processing" | "unprocessed";
export function tabToApiParams(tab: ProductTab): { kind: "processed" | "raw"; status: string } {
switch (tab) {
case "unprocessed":
return { kind: "raw", status: "unprocessed" };
case "needs_review":
return { kind: "processed", status: "needs_review" };
case "error":
// Failures are recorded on raw_products.processing_status, not processed_products.
return { kind: "raw", status: "failed" };
case "processing":
// In-flight work lives on job rows; raw rarely has processing_status=processing.
// Query raw+processing so the tab stays empty until a real filter exists (not wrong processed rows).
return { kind: "raw", status: "processing" };
case "processed":
default:
return { kind: "processed", status: "completed" };
}
}
/** Display / workflow order for products list tabs. */
const PRODUCT_TABS: readonly ProductTab[] = [
"unprocessed",
"processing",
"needs_review",
"processed",
"error"
];
/** Normalize URL token: trim, lower-case, hyphens → underscores. */
function normalizeQueryToken(value: string | null | undefined): string {
return String(value ?? "")
.trim()
.toLowerCase()
.replace(/-/g, "_");
}
export function isProductTab(value: string | null | undefined): value is ProductTab {
const token = normalizeQueryToken(value);
return PRODUCT_TABS.includes(token as ProductTab);
}
/**
* Resolve the products list tab from the URL.
* Canonical deep links use type/status (written by syncUrl); ?tab= is accepted as an alias
* so checklist / docs / settings-style links stay consistent.
*/
export function tabFromSearchParams(params: URLSearchParams): ProductTab {
const type = normalizeQueryToken(params.get("type"));
const status = normalizeQueryToken(params.get("status"));
// Status-specific tabs first — error/processing also use type=raw.
if (status === "needs_review") return "needs_review";
if (status === "failed" || status === "error") return "error";
if (status === "processing") return "processing";
if (type === "raw" || status === "unprocessed") return "unprocessed";
const tab = normalizeQueryToken(params.get("tab"));
if (isProductTab(tab)) return tab;
return "processed";
}
/** True when the URL picked a tab explicitly (skip empty-processed fallback). */
export function hasExplicitProductTab(params: URLSearchParams): boolean {
const type = normalizeQueryToken(params.get("type"));
const status = normalizeQueryToken(params.get("status"));
if (type || status) return true;
return isProductTab(params.get("tab"));
}
/** Search text from list URL — prefers `q` (canonical) over `search` (legacy), trimmed. */
export function searchQueryFromSearchParams(params: URLSearchParams): string {
const q = String(params.get("q") ?? "").trim();
if (q) return q;
return String(params.get("search") ?? "").trim();
}
const PRODUCT_COVERAGE_FILTERS = new Set([
"incomplete",
"complete",
"missing_name",
"missing_description",
"missing_attributes",
"missing_category"
]);
const PRODUCT_EPREL_FILTERS = new Set(["has_eprel", "no_eprel"]);
const PRODUCT_SYNC_CHANGE_FILTERS = new Set([
"any",
"price",
"stock",
"availability",
"title",
"new",
"other"
]);
const PRODUCT_SORT_BY = new Set(["updatedAt", "name", "createdAt"]);
/** Map common coverage aliases to UI/API values (aligned with catalog normalizeCoverageFilter). */
function normalizeCoverageQueryToken(raw: string): string {
const c = normalizeQueryToken(raw);
switch (c) {
case "":
case "all":
case "any":
return "all";
case "complete":
case "full":
case "ok":
return "complete";
case "incomplete":
case "partial":
return "incomplete";
case "missing_name":
case "name":
return "missing_name";
case "missing_description":
case "description":
return "missing_description";
case "missing_attributes":
case "attributes":
case "attrs":
return "missing_attributes";
case "missing_category":
case "category":
return "missing_category";
default:
return PRODUCT_COVERAGE_FILTERS.has(c) ? c : "all";
}
}
function normalizeEprelQueryToken(raw: string): string {
const c = normalizeQueryToken(raw);
switch (c) {
case "":
case "all":
case "any":
return "all";
case "has_eprel":
case "eprel":
case "with_eprel":
case "yes":
case "true":
case "1":
return "has_eprel";
case "no_eprel":
case "without_eprel":
case "missing_eprel":
case "none":
case "no":
case "false":
case "0":
return "no_eprel";
default:
return PRODUCT_EPREL_FILTERS.has(c) ? c : "all";
}
}
function normalizeSyncChangeQueryToken(raw: string): string {
const c = normalizeQueryToken(raw);
switch (c) {
case "":
case "all":
return "all";
case "any":
case "changed":
case "has_change":
case "has_changes":
return "any";
case "price":
case "price_changed":
return "price";
case "stock":
case "stock_changed":
case "qty":
case "quantity":
return "stock";
case "availability":
case "availability_changed":
case "stock_status":
return "availability";
case "title":
case "name":
case "title_changed":
return "title";
case "other":
case "other_changed":
return "other";
case "new":
case "inserted":
return "new";
default:
return PRODUCT_SYNC_CHANGE_FILTERS.has(c) ? c : "all";
}
}
function normalizeSortByQueryToken(raw: string): string {
const token = String(raw ?? "").trim();
if (PRODUCT_SORT_BY.has(token)) return token;
const lower = normalizeQueryToken(token);
if (lower === "updated_at" || lower === "updatedat") return "updatedAt";
if (lower === "created_at" || lower === "createdat") return "createdAt";
if (lower === "name") return "name";
return "updatedAt";
}
function normalizeSortOrderQueryToken(raw: string): "asc" | "desc" {
return normalizeQueryToken(raw) === "asc" ? "asc" : "desc";
}
export type ProductListURLFilters = {
searchQuery: string;
categoryFilter: string;
feedFilter: string;
coverageFilter: string;
eprelFilter: string;
syncChangeFilter: string;
sortBy: string;
sortOrder: "asc" | "desc";
};
/** Parse products-list filter/query params (aliases + invalid → safe defaults). */
export function productListFiltersFromSearchParams(params: URLSearchParams): ProductListURLFilters {
const category = String(params.get("category") ?? "").trim();
const feed =
String(params.get("feed_id") ?? "").trim() || String(params.get("feedId") ?? "").trim();
return {
searchQuery: searchQueryFromSearchParams(params),
categoryFilter: category && category.toLowerCase() !== "all" ? category : "all",
feedFilter: feed && feed.toLowerCase() !== "all" ? feed : "all",
coverageFilter: normalizeCoverageQueryToken(params.get("coverage") ?? ""),
eprelFilter: normalizeEprelQueryToken(params.get("eprel") ?? ""),
syncChangeFilter: normalizeSyncChangeQueryToken(
params.get("sync_change") ?? params.get("syncChange") ?? ""
),
sortBy: normalizeSortByQueryToken(
params.get("sortBy") ?? params.get("sort_by") ?? "updatedAt"
),
sortOrder: normalizeSortOrderQueryToken(
params.get("sortOrder") ?? params.get("sort_order") ?? "desc"
)
};
}