60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import type { Const } from '@/model/configuration'
|
|
import type { CaptureSite } from '@/model/captureSite'
|
|
import { isSwitchOn, switchToString } from '@/pages/menuPages/configuration/settingTypes'
|
|
|
|
export const TRACKING_SYSTEMS_KEY = 'tracking_systems'
|
|
export const TRACKING_SYSTEMS_ENABLED_KEY = 'tracking_systems_enabled'
|
|
|
|
export function findSettingRow(rows: Const[], key: string): Const | undefined {
|
|
return rows.find((row) => row.key === key)
|
|
}
|
|
|
|
export function buildSettingSavePayload(row: Const, value: string): Const {
|
|
return {
|
|
id: row.id,
|
|
key: row.key,
|
|
value,
|
|
description: row.description ?? '',
|
|
is_active: true,
|
|
title_fa: row.title_fa ?? '',
|
|
type: row.type ?? 'text',
|
|
}
|
|
}
|
|
|
|
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,
|
|
}))
|
|
)
|
|
}
|
|
|
|
export function parseTrackingSystemsEnabled(value: string | null | undefined): boolean {
|
|
return isSwitchOn(value ?? undefined)
|
|
}
|
|
|
|
export function serializeTrackingSystemsEnabled(enabled: boolean): string {
|
|
return switchToString(enabled)
|
|
}
|