Files
descrybe/apps/web/src/lib/export-feeds-helpers.ts
T

150 lines
4.5 KiB
TypeScript
Raw Normal View History

/**
* Pure helpers for Export Feeds selection/status filters and empty-state
* resolution (page list + product ExportSelectionDialog).
*/
/** Default product statuses when an export feed has no explicit filter. */
export const DEFAULT_EXPORT_STATUSES = ["processed", "completed"] as const;
export type ExportFeedFilters = {
statuses?: string[];
feed_id?: string;
};
export type ExportFeedOption = {
id: string;
name: string;
format: string;
};
export type ExportDialogBodyKind = "loading" | "error" | "empty" | "ready";
export type ExportFeedsListKind = "loading" | "loadFailed" | "none" | "noMatch" | "list";
/** Split a comma-separated status CSV into trimmed non-empty tokens. */
export function parseStatusList(raw: string | null | undefined): string[] {
return String(raw ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
}
/** Serialize statuses for the form input; falls back to the default CSV. */
export function formatStatusList(statuses: string[] | null | undefined): string {
if (!Array.isArray(statuses) || statuses.length === 0) {
return DEFAULT_EXPORT_STATUSES.join(",");
}
return statuses.join(",");
}
/** Build API filters from the create/edit form status CSV + optional source feed. */
export function buildExportFilters(input: {
statusesCsv: string;
sourceFeedId?: string | null;
}): ExportFeedFilters {
const statuses = parseStatusList(input.statusesCsv);
const filters: ExportFeedFilters = {};
if (statuses.length) filters.statuses = statuses;
const feedId = String(input.sourceFeedId ?? "").trim();
if (feedId) filters.feed_id = feedId;
return filters;
}
export function activeFormValue(isActive: boolean | null | undefined): "active" | "inactive" {
return isActive === false ? "inactive" : "active";
}
export function isActiveFromForm(value: string): boolean {
return value !== "inactive";
}
/** Map list API rows to active feed options for the product export dialog. */
export function mapActiveExportFeedOptions(
feeds: Array<{
id: unknown;
name?: unknown;
format?: unknown;
is_active?: boolean | null;
}>
): ExportFeedOption[] {
return feeds
.filter((f) => f.is_active !== false)
.map((f) => ({
id: String(f.id),
name: String(f.name ?? f.id),
format: String(f.format ?? "xml").toLowerCase()
}));
}
2026-09-20 10:48:19 +02:00
/** Column keys from an export feed template (fields[] or legacy mappings). */
export function fieldKeysFromExportTemplate(template: unknown): string[] {
if (!template || typeof template !== "object") return [];
const t = template as { fields?: unknown; mappings?: unknown };
if (Array.isArray(t.fields) && t.fields.length > 0) {
const keys: string[] = [];
for (const f of t.fields) {
if (!f || typeof f !== "object") continue;
const row = f as { key?: unknown; name?: unknown };
const key = String(row.key ?? row.name ?? "").trim();
if (key) keys.push(key);
}
return keys;
}
if (t.mappings && typeof t.mappings === "object" && !Array.isArray(t.mappings)) {
return Object.keys(t.mappings as Record<string, unknown>);
}
return [];
}
/** Which body the product export dialog should show. */
export function resolveExportDialogBody(input: {
loading: boolean;
error: string;
feedCount: number;
}): ExportDialogBodyKind {
if (input.loading) return "loading";
if (input.feedCount === 0 && input.error) return "error";
if (input.feedCount === 0) return "empty";
return "ready";
}
export function canConfirmExport(input: {
loading: boolean;
exporting: boolean;
selectedFeedId: string;
feedCount: number;
}): boolean {
return !(
input.loading ||
input.exporting ||
!input.selectedFeedId ||
input.feedCount === 0
);
}
/** Which empty/list state the Export Feeds page table area should show. */
export function resolveExportFeedsListKind(input: {
loading: boolean;
totalCount: number;
filteredCount: number;
listError: string;
}): ExportFeedsListKind {
if (input.loading && input.totalCount === 0) return "loading";
if (input.totalCount === 0 && input.listError) return "loadFailed";
if (input.totalCount === 0) return "none";
if (input.filteredCount === 0) return "noMatch";
return "list";
}
export function filterExportFeedsBySearch<
T extends { name?: unknown; format?: unknown; public_token?: unknown }
>(feeds: T[], search: string): T[] {
const q = search.trim().toLowerCase();
if (!q) return feeds;
return feeds.filter((f) => {
const name = String(f.name ?? "").toLowerCase();
const format = String(f.format ?? "").toLowerCase();
const token = String(f.public_token ?? "").toLowerCase();
return name.includes(q) || format.includes(q) || token.includes(q);
});
}