diff --git a/src/config/defaultKhanegiUiConfig.ts b/src/config/defaultKhanegiUiConfig.ts index 0633e09..20fa628 100644 --- a/src/config/defaultKhanegiUiConfig.ts +++ b/src/config/defaultKhanegiUiConfig.ts @@ -143,3 +143,41 @@ export function stripKhanegiSaMeta | null | un return rest as T } +function mapLegacyRoute(route: unknown) { + return route === 'products' ? 'buyProduct' : route +} + +function mapLegacyNavItems(items: T[] | undefined) { + if (!Array.isArray(items)) return items + return items.map((item) => ({ + ...item, + route: mapLegacyRoute(item.route) as T['route'], + })) +} + +/** هم‌تراز با اسکیمای userTheme (Zod) — جلوگیری از fallback تم */ +export function normalizeKhanegiUiConfigForClient>(ui: T): T { + if (!ui || typeof ui !== 'object') return ui + const next = JSON.parse(JSON.stringify(ui)) as T + + const fixApp = (app: any) => { + if (!app || typeof app !== 'object') return + if (app.startRoute === 'products') app.startRoute = 'buyProduct' + if (app.header && typeof app.header === 'object') { + app.header.variant = 'default' + } + if (app.navigation && typeof app.navigation === 'object') { + if (app.navigation.type === 'sidebar+bottom') app.navigation.type = 'both' + app.navigation.sidebar = mapLegacyNavItems(app.navigation.sidebar) + app.navigation.bottom = mapLegacyNavItems(app.navigation.bottom) + if (app.navigation.sidebarReferral?.route === 'products') { + app.navigation.sidebarReferral.route = 'buyProduct' + } + } + } + + fixApp((next as any).app) + fixApp((next as any).defaultSnapshot?.app) + return next +} + diff --git a/src/pages/UiConfigPage.vue b/src/pages/UiConfigPage.vue index 7be76b6..00a5291 100644 --- a/src/pages/UiConfigPage.vue +++ b/src/pages/UiConfigPage.vue @@ -7,6 +7,7 @@ import api from '@/api/http' import { cloneKhanegiAppSnapshot, createDefaultKhanegiUiConfig, + normalizeKhanegiUiConfigForClient, } from '@/config/defaultKhanegiUiConfig' import { applyKhanegiPreset, @@ -201,10 +202,15 @@ function buildSavePayload() { if (selectedPreset.value === 'custom') { markCustomAndSyncSchemes(config) } - const payload = JSON.parse(JSON.stringify(config)) as typeof config + const payload = normalizeKhanegiUiConfigForClient( + JSON.parse(JSON.stringify(config)) as typeof config, + ) // Never drop or rewrite an existing factory snapshot on normal save if (config.defaultSnapshot) { - payload.defaultSnapshot = JSON.parse(JSON.stringify(config.defaultSnapshot)) + payload.defaultSnapshot = normalizeKhanegiUiConfigForClient({ + app: JSON.parse(JSON.stringify(config.defaultSnapshot.app)), + pages: JSON.parse(JSON.stringify(config.defaultSnapshot.pages || {})), + }) as typeof payload.defaultSnapshot } return payload }