Add Khanegi reset-to-default for full ui-config appearance.
Seeds an immutable app+pages snapshot on first open and restores theme, nav, and header from it. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -116,4 +116,30 @@ export function createDefaultKhanegiUiConfig() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UiConfig = ReturnType<typeof createDefaultKhanegiUiConfig>
|
export type UiConfigBase = ReturnType<typeof createDefaultKhanegiUiConfig>
|
||||||
|
|
||||||
|
export type UiConfig = UiConfigBase & {
|
||||||
|
/** Immutable factory look — set once; never overwrite on normal save */
|
||||||
|
defaultSnapshot?: {
|
||||||
|
app: UiConfigBase['app']
|
||||||
|
pages: Record<string, unknown>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cloneKhanegiAppSnapshot(config: {
|
||||||
|
app: UiConfigBase['app']
|
||||||
|
pages?: Record<string, unknown>
|
||||||
|
}) {
|
||||||
|
return {
|
||||||
|
app: JSON.parse(JSON.stringify(config.app)) as UiConfigBase['app'],
|
||||||
|
pages: JSON.parse(JSON.stringify(config.pages || {})) as Record<string, unknown>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Strip SuperAdmin-only meta before sending to product apps via /get_user */
|
||||||
|
export function stripKhanegiSaMeta<T extends Record<string, unknown> | null | undefined>(ui: T): T {
|
||||||
|
if (!ui || typeof ui !== 'object') return ui
|
||||||
|
const { defaultSnapshot: _snap, _backfill, ...rest } = ui as Record<string, unknown>
|
||||||
|
return rest as T
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+106
-17
@@ -1,10 +1,13 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import { message } from 'ant-design-vue'
|
import { message, Modal } from 'ant-design-vue'
|
||||||
import { ArrowRightOutlined } from '@ant-design/icons-vue'
|
import { ArrowRightOutlined } from '@ant-design/icons-vue'
|
||||||
import api from '@/api/http'
|
import api from '@/api/http'
|
||||||
import { createDefaultKhanegiUiConfig } from '@/config/defaultKhanegiUiConfig'
|
import {
|
||||||
|
cloneKhanegiAppSnapshot,
|
||||||
|
createDefaultKhanegiUiConfig,
|
||||||
|
} from '@/config/defaultKhanegiUiConfig'
|
||||||
import {
|
import {
|
||||||
applyKhanegiPreset,
|
applyKhanegiPreset,
|
||||||
khanegiThemePresets,
|
khanegiThemePresets,
|
||||||
@@ -19,6 +22,7 @@ const route = useRoute()
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const loading = ref(true)
|
const loading = ref(true)
|
||||||
const saving = ref(false)
|
const saving = ref(false)
|
||||||
|
const resetting = ref(false)
|
||||||
const tenantName = ref('')
|
const tenantName = ref('')
|
||||||
const product = ref<'hamkar' | 'khanegi'>('khanegi')
|
const product = ref<'hamkar' | 'khanegi'>('khanegi')
|
||||||
const activeTab = ref('theme')
|
const activeTab = ref('theme')
|
||||||
@@ -27,8 +31,13 @@ const jsonError = ref('')
|
|||||||
const selectedPreset = ref<KhanegiPresetId>('navy-gold')
|
const selectedPreset = ref<KhanegiPresetId>('navy-gold')
|
||||||
const previewMode = ref<'dark' | 'light'>('dark')
|
const previewMode = ref<'dark' | 'light'>('dark')
|
||||||
|
|
||||||
const config = reactive(createDefaultKhanegiUiConfig())
|
const config = reactive(createDefaultKhanegiUiConfig()) as ReturnType<
|
||||||
|
typeof createDefaultKhanegiUiConfig
|
||||||
|
> & {
|
||||||
|
defaultSnapshot?: ReturnType<typeof cloneKhanegiAppSnapshot>
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasDefaultSnapshot = computed(() => Boolean(config.defaultSnapshot))
|
||||||
const isCustom = computed(() => selectedPreset.value === 'custom')
|
const isCustom = computed(() => selectedPreset.value === 'custom')
|
||||||
|
|
||||||
const activePreviewColors = computed(() => {
|
const activePreviewColors = computed(() => {
|
||||||
@@ -91,7 +100,13 @@ function syncAdvancedJson() {
|
|||||||
function applyAdvancedJson() {
|
function applyAdvancedJson() {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(advancedJson.value)
|
const parsed = JSON.parse(advancedJson.value)
|
||||||
|
const keepSnapshot = config.defaultSnapshot
|
||||||
|
? JSON.parse(JSON.stringify(config.defaultSnapshot))
|
||||||
|
: undefined
|
||||||
Object.assign(config, createDefaultKhanegiUiConfig(), parsed)
|
Object.assign(config, createDefaultKhanegiUiConfig(), parsed)
|
||||||
|
if (keepSnapshot && !config.defaultSnapshot) {
|
||||||
|
config.defaultSnapshot = keepSnapshot
|
||||||
|
}
|
||||||
ensureSchemes()
|
ensureSchemes()
|
||||||
selectedPreset.value = resolvePresetId(config)
|
selectedPreset.value = resolvePresetId(config)
|
||||||
jsonError.value = ''
|
jsonError.value = ''
|
||||||
@@ -128,6 +143,7 @@ async function load() {
|
|||||||
router.replace({ name: 'version-edit', params: { id: String(route.params.id) } })
|
router.replace({ name: 'version-edit', params: { id: String(route.params.id) } })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const hadSnapshot = Boolean(data.appUiConfig?.defaultSnapshot)
|
||||||
if (data.appUiConfig && typeof data.appUiConfig === 'object') {
|
if (data.appUiConfig && typeof data.appUiConfig === 'object') {
|
||||||
Object.assign(config, createDefaultKhanegiUiConfig(), data.appUiConfig)
|
Object.assign(config, createDefaultKhanegiUiConfig(), data.appUiConfig)
|
||||||
if (!config.app?.theme?.colors) {
|
if (!config.app?.theme?.colors) {
|
||||||
@@ -135,9 +151,17 @@ async function load() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ensureSchemes()
|
ensureSchemes()
|
||||||
|
if (!config.defaultSnapshot) {
|
||||||
|
config.defaultSnapshot = cloneKhanegiAppSnapshot(config)
|
||||||
|
}
|
||||||
selectedPreset.value = resolvePresetId(config)
|
selectedPreset.value = resolvePresetId(config)
|
||||||
previewMode.value = config.app.theme.mode === 'light' ? 'light' : 'dark'
|
previewMode.value = config.app.theme.mode === 'light' ? 'light' : 'dark'
|
||||||
syncAdvancedJson()
|
syncAdvancedJson()
|
||||||
|
|
||||||
|
// Persist newly inferred factory snapshot once
|
||||||
|
if (!hadSnapshot && config.defaultSnapshot) {
|
||||||
|
await persistConfig()
|
||||||
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
message.error(e?.response?.data?.message || 'خطا در بارگذاری')
|
message.error(e?.response?.data?.message || 'خطا در بارگذاری')
|
||||||
} finally {
|
} finally {
|
||||||
@@ -145,24 +169,41 @@ async function load() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildSavePayload() {
|
||||||
|
ensureSchemes()
|
||||||
|
config.app.theme.presetId = selectedPreset.value
|
||||||
|
syncActiveColorsFromMode(config)
|
||||||
|
if (selectedPreset.value === 'custom') {
|
||||||
|
markCustomAndSyncSchemes(config)
|
||||||
|
}
|
||||||
|
const payload = 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))
|
||||||
|
}
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
async function persistConfig() {
|
||||||
|
const payload = buildSavePayload()
|
||||||
|
const colors = payload.app.theme.colors
|
||||||
|
await api.patch(`/tenants/${route.params.id}`, {
|
||||||
|
appUiConfig: payload,
|
||||||
|
primaryColor: colors.primary,
|
||||||
|
secondaryColor: colors.secondary,
|
||||||
|
backgroundColor: colors.background,
|
||||||
|
})
|
||||||
|
Object.assign(config, createDefaultKhanegiUiConfig(), payload)
|
||||||
|
ensureSchemes()
|
||||||
|
selectedPreset.value = resolvePresetId(config)
|
||||||
|
syncAdvancedJson()
|
||||||
|
}
|
||||||
|
|
||||||
async function save() {
|
async function save() {
|
||||||
saving.value = true
|
saving.value = true
|
||||||
try {
|
try {
|
||||||
ensureSchemes()
|
await persistConfig()
|
||||||
config.app.theme.presetId = selectedPreset.value
|
|
||||||
syncActiveColorsFromMode(config)
|
|
||||||
if (selectedPreset.value === 'custom') {
|
|
||||||
markCustomAndSyncSchemes(config)
|
|
||||||
}
|
|
||||||
const colors = config.app.theme.colors
|
|
||||||
await api.patch(`/tenants/${route.params.id}`, {
|
|
||||||
appUiConfig: JSON.parse(JSON.stringify(config)),
|
|
||||||
primaryColor: colors.primary,
|
|
||||||
secondaryColor: colors.secondary,
|
|
||||||
backgroundColor: colors.background,
|
|
||||||
})
|
|
||||||
message.success('ظاهر اپ ذخیره شد')
|
message.success('ظاهر اپ ذخیره شد')
|
||||||
syncAdvancedJson()
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
const msg = e?.response?.data?.message
|
const msg = e?.response?.data?.message
|
||||||
message.error(Array.isArray(msg) ? msg.join('، ') : msg || 'ذخیره ناموفق')
|
message.error(Array.isArray(msg) ? msg.join('، ') : msg || 'ذخیره ناموفق')
|
||||||
@@ -171,6 +212,39 @@ async function save() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resetToDefault() {
|
||||||
|
if (!config.defaultSnapshot) {
|
||||||
|
message.warning('ظاهر پیشفرض برای این نسخه ثبت نشده است')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Modal.confirm({
|
||||||
|
title: 'بازگشت به ظاهر اولیه؟',
|
||||||
|
content: 'تم، منو و هدر به ظاهر کارخانهٔ همین نسخه برمیگردد و ذخیره میشود.',
|
||||||
|
okText: 'بازگشت به پیشفرض',
|
||||||
|
cancelText: 'انصراف',
|
||||||
|
okButtonProps: { danger: true },
|
||||||
|
async onOk() {
|
||||||
|
resetting.value = true
|
||||||
|
try {
|
||||||
|
const snap = config.defaultSnapshot!
|
||||||
|
config.app = JSON.parse(JSON.stringify(snap.app))
|
||||||
|
config.pages = JSON.parse(JSON.stringify(snap.pages || {}))
|
||||||
|
ensureSchemes()
|
||||||
|
selectedPreset.value = resolvePresetId(config)
|
||||||
|
previewMode.value = config.app.theme.mode === 'light' ? 'light' : 'dark'
|
||||||
|
await persistConfig()
|
||||||
|
message.success('ظاهر اولیه بازیابی شد')
|
||||||
|
} catch (e: any) {
|
||||||
|
const msg = e?.response?.data?.message
|
||||||
|
message.error(Array.isArray(msg) ? msg.join('، ') : msg || 'بازیابی ناموفق')
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
resetting.value = false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => config.app.theme.mode,
|
() => config.app.theme.mode,
|
||||||
() => {
|
() => {
|
||||||
@@ -407,6 +481,15 @@ onMounted(load)
|
|||||||
</a-tabs>
|
</a-tabs>
|
||||||
|
|
||||||
<div class="sticky-actions">
|
<div class="sticky-actions">
|
||||||
|
<a-button
|
||||||
|
size="large"
|
||||||
|
block
|
||||||
|
:loading="resetting"
|
||||||
|
:disabled="!hasDefaultSnapshot"
|
||||||
|
@click="resetToDefault"
|
||||||
|
>
|
||||||
|
بازگشت به ظاهر اولیه
|
||||||
|
</a-button>
|
||||||
<a-button type="primary" size="large" block :loading="saving" @click="save">ذخیره ظاهر</a-button>
|
<a-button type="primary" size="large" block :loading="saving" @click="save">ذخیره ظاهر</a-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -608,4 +691,10 @@ onMounted(load)
|
|||||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace !important;
|
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace !important;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.sticky-actions {
|
||||||
|
display: grid;
|
||||||
|
gap: 8px;
|
||||||
|
margin-top: 16px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user