Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(settings): preserve unsaved edits when saving a section

Ahmet Ömer477d9a34812a
diff --git a/packages/console/app/composables/useSettingsForm.ts b/packages/console/app/composables/useSettingsForm.ts
index 77785d31..e66e26dc 100644
--- a/packages/console/app/composables/useSettingsForm.ts
+++ b/packages/console/app/composables/useSettingsForm.ts
@@ -8,6 +8,23 @@ export const settingsFieldCommitKey: InjectionKey<
(fieldKey: string, value: any) => void
> = Symbol('settings-field-commit')
+export function applySavedSettingsFields(
+ fields: FieldDescriptor[],
+ state: Record<string, any>,
+ submitted: Record<string, any>,
+ stateAtSubmission: Record<string, string | undefined>,
+) {
+ for (const [key, value] of Object.entries(submitted)) {
+ const field = fields.find((item) => item.key === key)
+ if (!field) continue
+
+ field.value = value
+ if (JSON.stringify(state[key]) === stateAtSubmission[key]) {
+ state[key] = value
+ }
+ }
+}
+
/**
* Settings Form Composable
* 统一处理获取字段描述、管理表单状态、提交更新
@@ -75,6 +92,9 @@ export function useSettingsForm(namespace: string) {
const submit = async (data: Record<string, any>) => {
loading.value = true
error.value = null
+ const stateAtSubmission = Object.fromEntries(
+ Object.keys(data).map((key) => [key, JSON.stringify(state[key])]),
+ )
try {
// 准备批量更新请求
@@ -101,8 +121,7 @@ export function useSettingsForm(namespace: string) {
// 刷新全局设置状态,确保所有使用 getSetting() 的地方都能获取到最新值
await refreshSettings()
- // 重新加载字段以获取最新值
- await fetchFields()
+ applySavedSettingsFields(fields.value, state, data, stateAtSubmission)
toast.add({
title: t('settings.form.saveSuccess'),
diff --git a/tests/settings-form-submit.test.ts b/tests/settings-form-submit.test.ts
new file mode 100644
index 00000000..7838b21a
--- /dev/null
+++ b/tests/settings-form-submit.test.ts
@@ -0,0 +1,81 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import type {
+ FieldDescriptor,
+ SettingValue,
+} from '@chronoframe/gallery/shared/types/settings'
+import { applySavedSettingsFields } from '../packages/console/app/composables/useSettingsForm.ts'
+
+const field = (key: string, value: SettingValue): FieldDescriptor => ({
+ key,
+ type: 'string',
+ value,
+ defaultValue: value,
+ ui: { type: 'input' },
+})
+
+test('saving social links retains unsaved app and appearance edits', () => {
+ const fields = [
+ field('title', 'Original name'),
+ field('appearance.theme', 'system'),
+ field('socialLinks', { links: [] }),
+ ]
+ const state: Record<string, unknown> = {
+ title: 'Unsaved name',
+ 'appearance.theme': 'dark',
+ socialLinks: { links: [] },
+ }
+ const saved = { socialLinks: { links: ['https://instagram.com/example'] } }
+
+ applySavedSettingsFields(fields, state, saved, {
+ socialLinks: JSON.stringify(state.socialLinks),
+ })
+
+ assert.equal(state.title, 'Unsaved name')
+ assert.equal(state['appearance.theme'], 'dark')
+ assert.deepEqual(state.socialLinks, saved.socialLinks)
+ assert.equal(fields[0]?.value, 'Original name')
+ assert.equal(fields[1]?.value, 'system')
+ assert.deepEqual(fields[2]?.value, saved.socialLinks)
+})
+
+test('saving app fields retains the appearance draft and updates only app baselines', () => {
+ const fields = [
+ field('title', 'Original name'),
+ field('appearance.theme', 'system'),
+ ]
+ const state: Record<string, unknown> = {
+ title: 'Saved name',
+ 'appearance.theme': 'dark',
+ }
+
+ applySavedSettingsFields(
+ fields,
+ state,
+ { title: 'Saved name' },
+ {
+ title: JSON.stringify(state.title),
+ },
+ )
+
+ assert.equal(state['appearance.theme'], 'dark')
+ assert.equal(fields[0]?.value, 'Saved name')
+ assert.equal(fields[1]?.value, 'system')
+})
+
+test('a draft edited during a save stays dirty against the saved value', () => {
+ const fields = [field('title', 'Original name')]
+ const state: Record<string, unknown> = { title: 'Newer draft' }
+
+ applySavedSettingsFields(
+ fields,
+ state,
+ { title: 'Submitted name' },
+ {
+ title: JSON.stringify('Submitted name'),
+ },
+ )
+
+ assert.equal(state.title, 'Newer draft')
+ assert.equal(fields[0]?.value, 'Submitted name')
+})