24 lines
949 B
TypeScript
24 lines
949 B
TypeScript
import { fetchCategoryList } from '@/api/demo'
|
|
import { fetchAllPagedRecords } from '@/utils/fetch-paged-list'
|
|
import { buildTreePathLabelFromFlat } from '@/utils/tree-path'
|
|
|
|
let cachedFlatCategories: any[] = []
|
|
let cachedAt = 0
|
|
const CACHE_TTL = 60 * 1000
|
|
|
|
export async function ensureCategoryPathDataLoaded(force = false): Promise<void> {
|
|
if (!force && cachedFlatCategories.length > 0 && Date.now() - cachedAt < CACHE_TTL) return
|
|
const all = await fetchAllPagedRecords((page, pageSize) => fetchCategoryList({ page, pageSize }))
|
|
cachedFlatCategories = all || []
|
|
cachedAt = Date.now()
|
|
}
|
|
|
|
export function formatCategoryPathLabel(categoryId: unknown, fallbackName?: unknown): string {
|
|
const cid = String(categoryId ?? '').trim()
|
|
const fallback = String(fallbackName ?? '').trim()
|
|
if (!cid) return fallback || '—'
|
|
const path = buildTreePathLabelFromFlat(cid, cachedFlatCategories)
|
|
if (path) return path
|
|
return fallback || '—'
|
|
}
|