2024-01-12 18:44:11 +01:00
|
|
|
<svelte:options accessors />
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import {
|
|
|
|
|
NotificationType,
|
2024-02-13 17:07:37 -05:00
|
|
|
notificationController,
|
2024-01-12 18:44:11 +01:00
|
|
|
} from '$lib/components/shared-components/notification/notification';
|
|
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { getConfig, getConfigDefaults, updateConfig, type SystemConfigDto } from '@immich/sdk';
|
2024-03-06 00:45:40 -05:00
|
|
|
import { loadConfig } from '$lib/stores/server-config.store';
|
2024-01-26 18:02:56 +01:00
|
|
|
import { cloneDeep } from 'lodash-es';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { createEventDispatcher, onMount } from 'svelte';
|
|
|
|
|
import type { SettingsEventType } from './admin-settings';
|
2024-06-04 21:53:00 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-01-12 18:44:11 +01:00
|
|
|
|
|
|
|
|
export let config: SystemConfigDto;
|
|
|
|
|
|
|
|
|
|
let savedConfig: SystemConfigDto;
|
|
|
|
|
let defaultConfig: SystemConfigDto;
|
|
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{ save: void }>();
|
|
|
|
|
|
|
|
|
|
const handleReset = async (detail: SettingsEventType['reset']) => {
|
2024-02-02 04:18:00 +01:00
|
|
|
await (detail.default ? resetToDefault(detail.configKeys) : reset(detail.configKeys));
|
2024-01-12 18:44:11 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-24 21:43:43 +02:00
|
|
|
export const handleSave = async (update: Partial<SystemConfigDto>) => {
|
2024-01-12 18:44:11 +01:00
|
|
|
try {
|
2024-02-13 17:07:37 -05:00
|
|
|
const newConfig = await updateConfig({
|
2024-01-12 18:44:11 +01:00
|
|
|
systemConfigDto: {
|
|
|
|
|
...savedConfig,
|
2024-01-26 18:02:56 +01:00
|
|
|
...update,
|
2024-01-12 18:44:11 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-26 18:02:56 +01:00
|
|
|
config = cloneDeep(newConfig);
|
|
|
|
|
savedConfig = cloneDeep(newConfig);
|
2024-06-04 21:53:00 +02:00
|
|
|
notificationController.show({ message: $t('settings_saved'), type: NotificationType.Info });
|
2024-01-12 18:44:11 +01:00
|
|
|
|
2024-03-06 00:45:40 -05:00
|
|
|
await loadConfig();
|
|
|
|
|
|
2024-01-12 18:44:11 +01:00
|
|
|
dispatch('save');
|
|
|
|
|
} catch (error) {
|
2024-06-04 21:53:00 +02:00
|
|
|
handleError(error, $t('errors.unable_to_save_settings'));
|
2024-01-12 18:44:11 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const reset = async (configKeys: Array<keyof SystemConfigDto>) => {
|
2024-02-13 17:07:37 -05:00
|
|
|
const resetConfig = await getConfig();
|
2024-02-02 04:18:00 +01:00
|
|
|
|
|
|
|
|
for (const key of configKeys) {
|
|
|
|
|
config = { ...config, [key]: resetConfig[key] };
|
|
|
|
|
}
|
2024-01-12 18:44:11 +01:00
|
|
|
|
|
|
|
|
notificationController.show({
|
|
|
|
|
message: 'Reset settings to the recent saved settings',
|
|
|
|
|
type: NotificationType.Info,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-27 08:37:37 -08:00
|
|
|
const resetToDefault = (configKeys: Array<keyof SystemConfigDto>) => {
|
2024-02-02 04:18:00 +01:00
|
|
|
for (const key of configKeys) {
|
|
|
|
|
config = { ...config, [key]: defaultConfig[key] };
|
|
|
|
|
}
|
2024-01-12 18:44:11 +01:00
|
|
|
|
|
|
|
|
notificationController.show({
|
2024-06-04 21:53:00 +02:00
|
|
|
message: $t('reset_settings_to_default'),
|
2024-01-12 18:44:11 +01:00
|
|
|
type: NotificationType.Info,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMount(async () => {
|
2024-02-13 17:07:37 -05:00
|
|
|
[savedConfig, defaultConfig] = await Promise.all([getConfig(), getConfigDefaults()]);
|
2024-01-12 18:44:11 +01:00
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
{#if savedConfig && defaultConfig}
|
|
|
|
|
<slot {handleReset} {handleSave} {savedConfig} {defaultConfig} />
|
|
|
|
|
{/if}
|