Files
boursecu_admin/src/utils/config.ts
T
2026-06-23 22:31:07 +03:30

65 lines
2.2 KiB
TypeScript

import { setCachedMainMetal } from '@/utils/metalLabels'
export function getConfigApp(key: string) {
const config = window.__APP_CONFIG__ ? window.__APP_CONFIG__.user : {}
if (config) {
return config[key]
}
return null
}
let storeModeFromSettings: boolean | null = null
let settingsBootstrapped = false
function isSwitchOn(value: string | undefined | null): boolean {
const v = (value ?? '').trim().toLowerCase()
return v === 'true' || v === '1' || v === 'yes'
}
export function setStoreModeFromSettings(value: boolean): void {
storeModeFromSettings = value
}
async function bootstrapSettingsFromApi(): Promise<void> {
if (settingsBootstrapped) return
try {
const { getConfiguration } = await import('@/api/configuration')
const { parseMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
const rows = await getConfiguration()
const list = Array.isArray(rows) ? rows : []
const isStoreRow = list.find((item) => item.key === 'is_store')
const mainWalletRow = list.find((item) => item.key === 'main_wallet_products')
const mainMetalRow = list.find((item) => item.key === 'main_metal')
storeModeFromSettings = isStoreRow ? isSwitchOn(isStoreRow.value) : false
setCachedMainMetal(mainMetalRow?.value)
const { setCachedMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
setCachedMainWalletProductIds(parseMainWalletProductIds(mainWalletRow?.value))
} catch {
storeModeFromSettings = false
setCachedMainMetal(null)
const { setCachedMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
setCachedMainWalletProductIds([])
} finally {
settingsBootstrapped = true
}
}
/** is_store و main_wallet_products از API تنظیمات — نه از get_user */
export async function initStoreModeFromSettings(): Promise<boolean> {
if (storeModeFromSettings !== null) return storeModeFromSettings
await bootstrapSettingsFromApi()
return storeModeFromSettings === true
}
export async function initWalletSettingsFromApi(): Promise<void> {
await bootstrapSettingsFromApi()
}
export function isStoreApp(): boolean {
return storeModeFromSettings === true
}