dynamic banners of khanegi_theme
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import type { IBenefits } from '@/model/banner'
|
||||
|
||||
/** کلید واحد همهٔ بنرهای اپ (اسلایدر + تم لایت/دارک) */
|
||||
export const APP_BANNERS_CONST_KEY = 'app_banners'
|
||||
|
||||
/** کلید قدیمی تم — فقط برای مهاجرت خواندن */
|
||||
export const LEGACY_THEME_BANNERS_CONST_KEY = 'settings_profile_banners'
|
||||
|
||||
/** کلید قدیمی اسلایدر — فقط برای مهاجرت خواندن */
|
||||
export const LEGACY_SLIDER_BANNERS_CONST_KEY = 'header_banner'
|
||||
|
||||
export type AppThemeBannerKey =
|
||||
| 'homeAsset'
|
||||
| 'piggyCreate'
|
||||
| 'productsHero1'
|
||||
| 'productsHero2'
|
||||
| 'productsHero3'
|
||||
| 'referral'
|
||||
| 'share'
|
||||
| 'invite'
|
||||
|
||||
/** @deprecated از AppThemeBannerKey استفاده کنید */
|
||||
export type SettingsProfileBannerKey = AppThemeBannerKey
|
||||
|
||||
export interface ThemeImagePair {
|
||||
light: string | null
|
||||
dark: string | null
|
||||
}
|
||||
|
||||
export interface AppBannersValue {
|
||||
slider: IBenefits[]
|
||||
homeAsset: ThemeImagePair
|
||||
piggyCreate: ThemeImagePair
|
||||
productsHero1: ThemeImagePair
|
||||
productsHero2: ThemeImagePair
|
||||
productsHero3: ThemeImagePair
|
||||
referral: ThemeImagePair
|
||||
share: ThemeImagePair
|
||||
invite: ThemeImagePair
|
||||
}
|
||||
|
||||
/** @deprecated از AppBannersValue استفاده کنید */
|
||||
export type SettingsProfileBannersValue = Omit<AppBannersValue, 'slider'>
|
||||
|
||||
export const SETTINGS_PROFILE_BANNERS_CONST_KEY = APP_BANNERS_CONST_KEY
|
||||
|
||||
export function emptyThemePair(): ThemeImagePair {
|
||||
return { light: null, dark: null }
|
||||
}
|
||||
|
||||
export function emptyAppBanners(): AppBannersValue {
|
||||
return {
|
||||
slider: [],
|
||||
homeAsset: emptyThemePair(),
|
||||
piggyCreate: emptyThemePair(),
|
||||
productsHero1: emptyThemePair(),
|
||||
productsHero2: emptyThemePair(),
|
||||
productsHero3: emptyThemePair(),
|
||||
referral: emptyThemePair(),
|
||||
share: emptyThemePair(),
|
||||
invite: emptyThemePair(),
|
||||
}
|
||||
}
|
||||
|
||||
export function emptySettingsProfileBanners(): SettingsProfileBannersValue {
|
||||
const { slider: _slider, ...themes } = emptyAppBanners()
|
||||
return themes
|
||||
}
|
||||
|
||||
function normalizePair(raw: unknown): ThemeImagePair {
|
||||
if (!raw || typeof raw !== 'object') return emptyThemePair()
|
||||
const obj = raw as Record<string, unknown>
|
||||
return {
|
||||
light: typeof obj.light === 'string' && obj.light.trim() ? obj.light : null,
|
||||
dark: typeof obj.dark === 'string' && obj.dark.trim() ? obj.dark : null,
|
||||
}
|
||||
}
|
||||
|
||||
function parseSlider(raw: unknown): IBenefits[] {
|
||||
if (!raw) return []
|
||||
if (Array.isArray(raw)) return raw as IBenefits[]
|
||||
if (typeof raw === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(raw)
|
||||
return Array.isArray(parsed) ? (parsed as IBenefits[]) : []
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
export function parseAppBanners(value: string | null | undefined): AppBannersValue {
|
||||
const fallback = emptyAppBanners()
|
||||
if (!value) return fallback
|
||||
try {
|
||||
const parsed = JSON.parse(value)
|
||||
if (!parsed || typeof parsed !== 'object') return fallback
|
||||
|
||||
// فرمت قدیمی: فقط آرایهٔ اسلایدر (header_banner)
|
||||
if (Array.isArray(parsed)) {
|
||||
return { ...fallback, slider: parsed as IBenefits[] }
|
||||
}
|
||||
|
||||
return {
|
||||
slider: parseSlider(parsed.slider),
|
||||
homeAsset: normalizePair(parsed.homeAsset),
|
||||
piggyCreate: normalizePair(parsed.piggyCreate),
|
||||
productsHero1: normalizePair(parsed.productsHero1),
|
||||
productsHero2: normalizePair(parsed.productsHero2),
|
||||
productsHero3: normalizePair(parsed.productsHero3),
|
||||
referral: normalizePair(parsed.referral),
|
||||
share: normalizePair(parsed.share),
|
||||
invite: normalizePair(parsed.invite),
|
||||
}
|
||||
} catch {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
export function parseSettingsProfileBanners(value: string | null | undefined): SettingsProfileBannersValue {
|
||||
const { slider: _slider, ...themes } = parseAppBanners(value)
|
||||
return themes
|
||||
}
|
||||
|
||||
export function serializeAppBanners(data: AppBannersValue): string {
|
||||
return JSON.stringify({
|
||||
slider: data.slider ?? [],
|
||||
homeAsset: { ...data.homeAsset },
|
||||
piggyCreate: { ...data.piggyCreate },
|
||||
productsHero1: { ...data.productsHero1 },
|
||||
productsHero2: { ...data.productsHero2 },
|
||||
productsHero3: { ...data.productsHero3 },
|
||||
referral: { ...data.referral },
|
||||
share: { ...data.share },
|
||||
invite: { ...data.invite },
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/** سازگاری با importهای قبلی — منبع اصلی: appBanners.ts */
|
||||
export {
|
||||
APP_BANNERS_CONST_KEY as SETTINGS_PROFILE_BANNERS_CONST_KEY,
|
||||
emptySettingsProfileBanners,
|
||||
emptyThemePair,
|
||||
parseSettingsProfileBanners,
|
||||
type AppThemeBannerKey as SettingsProfileBannerKey,
|
||||
type SettingsProfileBannersValue,
|
||||
type ThemeImagePair,
|
||||
} from '@/model/appBanners'
|
||||
Reference in New Issue
Block a user