49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import type { Const } from '@/model/const'
|
|
import type { CaptureSite } from '@/model/captureSite'
|
|
|
|
export const REYGIri_SITES_KEY = 'reygiri_sites'
|
|
|
|
export function normalizeReygiriSitesConst(response: Const | Const[] | null | undefined): {
|
|
constId: number | null
|
|
sites: CaptureSite[]
|
|
} {
|
|
const record = Array.isArray(response) ? response[0] : response
|
|
if (!record) {
|
|
return { constId: null, sites: [] }
|
|
}
|
|
|
|
return {
|
|
constId: record.id ?? record.const_id ?? null,
|
|
sites: parseReygiriSitesValue(record.value),
|
|
}
|
|
}
|
|
|
|
export function parseReygiriSitesValue(value: string | null | undefined): CaptureSite[] {
|
|
if (!value) return []
|
|
|
|
try {
|
|
const parsed = JSON.parse(value)
|
|
if (!Array.isArray(parsed)) return []
|
|
|
|
return parsed
|
|
.map((item, index) => ({
|
|
id: String(item?.id ?? `site-${index + 1}`),
|
|
name: String(item?.name ?? item?.title ?? item?.title_fa ?? item?.caption ?? '').trim(),
|
|
url: String(item?.url ?? item?.link ?? item?.value ?? '').trim(),
|
|
}))
|
|
.filter((item) => item.name && item.url)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export function serializeReygiriSites(sites: CaptureSite[]): string {
|
|
return JSON.stringify(
|
|
sites.map(({ id, name, url }) => ({
|
|
id,
|
|
name,
|
|
url,
|
|
}))
|
|
)
|
|
}
|