1405/04/27
-responive bugs
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { getSettings } from '@/api/configuration'
|
||||
import type { AppSetting } from '@/store/settingsStore'
|
||||
import { useSettingsStore } from '@/store/settingsStore'
|
||||
|
||||
export const TRACKING_SYSTEMS_KEY = 'tracking_systems'
|
||||
export const TRACKING_SYSTEMS_ENABLED_KEY = 'tracking_systems_enabled'
|
||||
|
||||
export interface TrackingSystemSite {
|
||||
id: string
|
||||
name: string
|
||||
url: string
|
||||
}
|
||||
|
||||
let trackingSettingsCache: AppSetting[] | null = null
|
||||
let trackingSettingsPromise: Promise<AppSetting[]> | null = null
|
||||
|
||||
export function normalizeSettingsResponse(data: unknown): AppSetting[] {
|
||||
if (Array.isArray(data)) return data as AppSetting[]
|
||||
|
||||
if (data && typeof data === 'object') {
|
||||
const record = data as Record<string, unknown>
|
||||
const nested = record.data ?? record.settings ?? record.rows
|
||||
if (Array.isArray(nested)) return nested as AppSetting[]
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
export function findSettingValue(settings: AppSetting[], key: string): unknown {
|
||||
const setting = settings.find((item) => String(item?.key ?? '').trim() === key)
|
||||
return setting?.value ?? null
|
||||
}
|
||||
|
||||
export function isSettingTruthy(value: unknown): boolean {
|
||||
if (value === true) return true
|
||||
const normalized = String(value ?? '').trim().toLowerCase()
|
||||
return normalized === 'true' || normalized === '1' || normalized === 'yes'
|
||||
}
|
||||
|
||||
export function parseTrackingSystems(raw: unknown): TrackingSystemSite[] {
|
||||
let value: unknown = raw
|
||||
|
||||
for (let depth = 0; depth < 2; depth += 1) {
|
||||
if (typeof value !== 'string') break
|
||||
const trimmed = value.trim()
|
||||
if (!trimmed) return []
|
||||
|
||||
try {
|
||||
value = JSON.parse(trimmed)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(value)) return []
|
||||
|
||||
return value
|
||||
.map((item) => {
|
||||
if (!item || typeof item !== 'object') return null
|
||||
|
||||
const record = item as Record<string, unknown>
|
||||
const id = String(record.id ?? '').trim()
|
||||
const name = String(record.name ?? record.title ?? '').trim()
|
||||
const url = String(record.url ?? record.link ?? '').trim()
|
||||
|
||||
if (!name || !url) return null
|
||||
return { id: id || `${name}-${url}`, name, url }
|
||||
})
|
||||
.filter((item): item is TrackingSystemSite => item != null)
|
||||
}
|
||||
|
||||
export function resolveTrackingSystems(settings: AppSetting[]): TrackingSystemSite[] {
|
||||
return parseTrackingSystems(findSettingValue(settings, TRACKING_SYSTEMS_KEY))
|
||||
}
|
||||
|
||||
export function isTrackingSystemsEnabled(settings: AppSetting[]): boolean {
|
||||
return isSettingTruthy(findSettingValue(settings, TRACKING_SYSTEMS_ENABLED_KEY))
|
||||
}
|
||||
|
||||
export async function fetchTrackingSettings(force = false): Promise<AppSetting[]> {
|
||||
if (!force && trackingSettingsCache) return trackingSettingsCache
|
||||
if (!force && trackingSettingsPromise) return trackingSettingsPromise
|
||||
|
||||
trackingSettingsPromise = (async () => {
|
||||
const settingsStore = useSettingsStore()
|
||||
|
||||
try {
|
||||
await settingsStore.fetchSettings(force)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
try {
|
||||
const freshSettings = normalizeSettingsResponse(await getSettings())
|
||||
if (freshSettings.length > 0) settingsStore.mergeSettings(freshSettings)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
trackingSettingsCache = [...settingsStore.settings]
|
||||
return trackingSettingsCache
|
||||
})()
|
||||
.catch((error) => {
|
||||
console.log(error)
|
||||
trackingSettingsCache = []
|
||||
return []
|
||||
})
|
||||
.finally(() => {
|
||||
trackingSettingsPromise = null
|
||||
})
|
||||
|
||||
return trackingSettingsPromise
|
||||
}
|
||||
|
||||
export function resetTrackingSettingsCache() {
|
||||
trackingSettingsCache = null
|
||||
trackingSettingsPromise = null
|
||||
}
|
||||
Reference in New Issue
Block a user