Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: use SettingsForm hook for managing settings logic and state
Kyle Mendelle09a808d09f7
diff --git a/frontend/src/lib/hooks/use-settings-form.svelte.ts b/frontend/src/lib/hooks/use-settings-form.svelte.tsnew file mode 100644index 000000000..52e03c45a--- /dev/null+++ b/frontend/src/lib/hooks/use-settings-form.svelte.ts@@ -0,0 +1,71 @@+import { getContext } from 'svelte';+import settingsStore from '$lib/stores/config-store';+import { settingsService } from '$lib/services/settings-service';+import type { Settings } from '$lib/types/settings.type';+import { tryCatch } from '$lib/utils/try-catch';++type SettingsFormState = {+ hasChanges: boolean;+ isLoading: boolean;+ saveFunction?: () => Promise<void> | void;+ resetFunction?: () => void;+};++type Options = {+ hasChangesChecker: () => boolean;+};++export class UseSettingsForm {+ #hasChanges = $state(false);+ #isLoading = $state(false);+ private formState: SettingsFormState | undefined;+ private hasChangesChecker: () => boolean;++ constructor({ hasChangesChecker }: Options) {+ this.hasChangesChecker = hasChangesChecker;++ try {+ this.formState = getContext('settingsFormState') as SettingsFormState | undefined;+ } catch {+ // Context not available+ }++ $effect(() => {+ this.#hasChanges = this.hasChangesChecker();+ if (this.formState) {+ this.formState.hasChanges = this.#hasChanges;+ this.formState.isLoading = this.#isLoading;+ }+ });+ }++ async updateSettings(updatedSettings: Partial<Settings>) {+ const result = await tryCatch(settingsService.updateSettings(updatedSettings as any));++ if (result.error) {+ console.error('Error updating settings:', result.error);+ throw result.error;+ }++ await settingsStore.reload();+ }++ registerFormActions(saveFunction: () => Promise<void> | void, resetFunction: () => void) {+ if (this.formState) {+ this.formState.saveFunction = saveFunction;+ this.formState.resetFunction = resetFunction;+ }+ }++ setLoading(loading: boolean) {+ this.#isLoading = loading;+ }++ get hasChanges() {+ return this.#hasChanges;+ }++ get isLoading() {+ return this.#isLoading;+ }+}diff --git a/frontend/src/routes/settings/docker/+page.svelte b/frontend/src/routes/settings/docker/+page.svelteindex a30b89931..3c782e516 100644--- a/frontend/src/routes/settings/docker/+page.svelte+++ b/frontend/src/routes/settings/docker/+page.svelte@@ -5,7 +5,7 @@import { toast } from 'svelte-sonner';import type { Settings } from '$lib/types/settings.type';import { z } from 'zod/v4';- import { getContext, onMount } from 'svelte';+ import { onMount } from 'svelte';import { createForm } from '$lib/utils/form.utils';import SwitchWithLabel from '$lib/components/form/labeled-switch.svelte';import SelectWithLabel from '$lib/components/form/select-with-label.svelte';@@ -17,16 +17,13 @@import TextInputWithLabel from '$lib/components/form/text-input-with-label.svelte';import settingsStore from '$lib/stores/config-store';import BoxesIcon from '@lucide/svelte/icons/boxes';- import { settingsService } from '$lib/services/settings-service';import { SettingsPageLayout } from '$lib/layouts';+ import { UseSettingsForm } from '$lib/hooks/use-settings-form.svelte';let { data } = $props();const currentSettings = $derived<Settings>($settingsStore || data.settings!);- let hasChanges = $state(false);- let isLoading = $state(false);+ const isReadOnly = $derived.by(() => $settingsStore?.uiConfigDisabled);- const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);- const formState = getContext('settingsFormState') as any;const formSchema = z.object({pollingEnabled: z.boolean(),pollingInterval: z.number().int().min(5).max(10080),@@ -107,22 +104,14 @@let { inputs: formInputs, ...form } = $derived(createForm<typeof formSchema>(formSchema, currentSettings));- const formHasChanges = $derived.by(- () =>+ const settingsForm = new UseSettingsForm({+ hasChangesChecker: () =>$formInputs.pollingEnabled.value !== currentSettings.pollingEnabled ||$formInputs.pollingInterval.value !== currentSettings.pollingInterval ||$formInputs.autoUpdate.value !== currentSettings.autoUpdate ||$formInputs.autoUpdateInterval.value != currentSettings.autoUpdateInterval ||$formInputs.dockerPruneMode.value != currentSettings.dockerPruneMode ||$formInputs.defaultShell.value != currentSettings.defaultShell- );-- $effect(() => {- hasChanges = formHasChanges;- if (formState) {- formState.hasChanges = hasChanges;- formState.isLoading = isLoading;- }});$effect(() => {@@ -137,35 +126,23 @@}});- async function updateSettingsConfig(updatedSettings: Partial<Settings>) {- try {- await settingsService.updateSettings(updatedSettings as any);- const updated = { ...currentSettings, ...updatedSettings };- settingsStore.set(updated);- settingsStore.reload();- } catch (error) {- console.error('Error updating settings:', error);- throw error;- }- }-async function onSubmit() {- const formData = form.validate();- if (!formData) {+ const data = form.validate();+ if (!data) {toast.error('Please check the form for errors');return;}- isLoading = true;+ settingsForm.setLoading(true);- await updateSettingsConfig(formData)+ await settingsForm+ .updateSettings(data).then(() => toast.success(m.general_settings_saved())).catch((error) => {- console.error('Failed to save settings:', error);- toast.error('Failed to save settings. Please try again.');+ console.error('Failed to save Docker settings:', error);+ toast.error('Failed to save Docker settings. Please try again.');})- .finally(() => (isLoading = false));+ .finally(() => settingsForm.setLoading(false));}-function resetForm() {$formInputs.pollingEnabled.value = currentSettings.pollingEnabled;$formInputs.pollingInterval.value = currentSettings.pollingInterval;@@ -176,10 +153,7 @@}onMount(() => {- if (formState) {- formState.saveFunction = onSubmit;- formState.resetFunction = resetForm;- }+ settingsForm.registerFormActions(onSubmit, resetForm);});</script>diff --git a/frontend/src/routes/settings/general/+page.svelte b/frontend/src/routes/settings/general/+page.svelteindex 74cdfcb6a..14543397f 100644--- a/frontend/src/routes/settings/general/+page.svelte+++ b/frontend/src/routes/settings/general/+page.svelte@@ -1,9 +1,8 @@<script lang="ts">import * as Card from '$lib/components/ui/card';import { z } from 'zod/v4';- import { getContext, onMount } from 'svelte';+ import { onMount } from 'svelte';import { createForm } from '$lib/utils/form.utils';- import type { Settings } from '$lib/types/settings.type';import { toast } from 'svelte-sonner';import SwitchWithLabel from '$lib/components/form/labeled-switch.svelte';import { m } from '$lib/paraglide/messages';@@ -13,20 +12,18 @@import TextInputWithLabel from '$lib/components/form/text-input-with-label.svelte';import settingsStore from '$lib/stores/config-store';import SettingsIcon from '@lucide/svelte/icons/settings';- import { settingsService } from '$lib/services/settings-service';import { SettingsPageLayout } from '$lib/layouts';import AccentColorPicker from '$lib/components/accent-color/accent-color-picker.svelte';import { applyAccentColor } from '$lib/utils/accent-color-util';import SparklesIcon from '@lucide/svelte/icons/sparkles';import { Switch } from '$lib/components/ui/switch/index.js';+ import { UseSettingsForm } from '$lib/hooks/use-settings-form.svelte';let { data } = $props();- let hasChanges = $state(false);- let isLoading = $state(false);const currentSettings = $derived($settingsStore || data.settings!);- const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);- const formState = getContext('settingsFormState') as any;+ const isReadOnly = $derived.by(() => $settingsStore?.uiConfigDisabled);+const formSchema = z.object({projectsDirectory: z.string().min(1, m.general_projects_directory_required()),baseServerUrl: z.string().min(1, m.general_base_url_required()),@@ -37,50 +34,31 @@let { inputs: formInputs, ...form } = $derived(createForm<typeof formSchema>(formSchema, currentSettings));- const formHasChanges = $derived.by(- () =>+ const settingsForm = new UseSettingsForm({+ hasChangesChecker: () =>$formInputs.projectsDirectory.value !== currentSettings.projectsDirectory ||$formInputs.baseServerUrl.value !== currentSettings.baseServerUrl ||$formInputs.enableGravatar.value !== currentSettings.enableGravatar ||$formInputs.accentColor.value !== currentSettings.accentColor ||$formInputs.glassEffectEnabled.value !== currentSettings.glassEffectEnabled- );-- $effect(() => {- hasChanges = formHasChanges;- if (formState) {- formState.hasChanges = hasChanges;- formState.isLoading = isLoading;- }});- async function updateSettingsConfig(updatedSettings: Partial<Settings>) {- try {- await settingsService.updateSettings(updatedSettings as any);- const updated = { ...currentSettings, ...updatedSettings };- settingsStore.set(updated);- settingsStore.reload();- } catch (error) {- console.error('Error updating settings:', error);- throw error;- }- }-async function onSubmit() {const data = form.validate();if (!data) {toast.error('Please check the form for errors');return;}- isLoading = true;+ settingsForm.setLoading(true);- await updateSettingsConfig(data)+ await settingsForm+ .updateSettings(data).then(() => toast.success(m.general_settings_saved())).catch((error) => {console.error('Failed to save settings:', error);toast.error('Failed to save settings. Please try again.');})- .finally(() => (isLoading = false));+ .finally(() => settingsForm.setLoading(false));}function resetForm() {@@ -93,10 +71,7 @@}onMount(() => {- if (formState) {- formState.saveFunction = onSubmit;- formState.resetFunction = resetForm;- }+ settingsForm.registerFormActions(onSubmit, resetForm);});</script>diff --git a/frontend/src/routes/settings/navigation/+page.svelte b/frontend/src/routes/settings/navigation/+page.svelteindex b0b1ff521..f65c93d46 100644--- a/frontend/src/routes/settings/navigation/+page.svelte+++ b/frontend/src/routes/settings/navigation/+page.svelte@@ -1,9 +1,8 @@<script lang="ts">import * as Card from '$lib/components/ui/card';import { z } from 'zod/v4';- import { getContext, onMount } from 'svelte';+ import { onMount } from 'svelte';import { createForm } from '$lib/utils/form.utils';- import type { Settings } from '$lib/types/settings.type';import { toast } from 'svelte-sonner';import EyeIcon from '@lucide/svelte/icons/eye';import NavigationIcon from '@lucide/svelte/icons/navigation';@@ -13,18 +12,15 @@import settingsStore from '$lib/stores/config-store';import { m } from '$lib/paraglide/messages';import { navigationSettingsOverridesStore, resetNavigationVisibility } from '$lib/utils/navigation.utils';- import { settingsService } from '$lib/services/settings-service';import { SettingsPageLayout } from '$lib/layouts';import { Switch } from '$lib/components/ui/switch/index.js';import { useSidebar } from '$lib/components/ui/sidebar/context.svelte.js';+ import { UseSettingsForm } from '$lib/hooks/use-settings-form.svelte';let { data } = $props();const currentSettings = $derived($settingsStore || data.settings!);- let hasChanges = $state(false);- let isLoading = $state(false);+ const isReadOnly = $derived.by(() => $settingsStore?.uiConfigDisabled);- const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);- const formState = getContext('settingsFormState') as any;const formSchema = z.object({mobileNavigationMode: z.enum(['floating', 'docked']),mobileNavigationShowLabels: z.boolean(),@@ -44,19 +40,12 @@}let { inputs: formInputs, ...form } = $derived(createForm<typeof formSchema>(formSchema, currentSettings));- const formHasChanges = $derived.by(- () =>++ const settingsForm = new UseSettingsForm({+ hasChangesChecker: () =>$formInputs.mobileNavigationMode.value !== currentSettings.mobileNavigationMode ||$formInputs.mobileNavigationShowLabels.value !== currentSettings.mobileNavigationShowLabels ||$formInputs.sidebarHoverExpansion.value !== currentSettings.sidebarHoverExpansion- );-- $effect(() => {- hasChanges = formHasChanges;- if (formState) {- formState.hasChanges = hasChanges;- formState.isLoading = isLoading;- }});function setLocalOverride(key: 'mode' | 'showLabels', value: any) {@@ -88,55 +77,30 @@toast.success(`Local override cleared for ${key.replace(/([A-Z])/g, ' $1').toLowerCase()}`);}- async function updateSettingsConfig(updatedSettings: Partial<Settings>) {- try {- await settingsService.updateSettings(updatedSettings as any);- const updated = { ...currentSettings, ...updatedSettings };- settingsStore.set(updated);- settingsStore.reload();- } catch (error) {- console.error('Error updating navigation settings:', error);- throw error;- }- }-async function onSubmit() {- const formData = form.validate();- if (!formData) {+ const data = form.validate();+ if (!data) {toast.error('Please check the form for errors');return;}- isLoading = true;+ settingsForm.setLoading(true);- // Check if mode changed (which affects scroll-to-hide behavior)- const modeChanged = formData.mobileNavigationMode !== currentSettings.mobileNavigationMode;-- await updateSettingsConfig(formData)- .then(() => {- toast.success(m.navigation_settings_saved());-- // Reset navigation bar visibility if mode changed- if (modeChanged) {- resetNavigationVisibility();- }- })+ await settingsForm+ .updateSettings(data)+ .then(() => toast.success(m.navigation_settings_saved())).catch((error) => {console.error('Failed to save navigation settings:', error);toast.error('Failed to save navigation settings. Please try again.');})- .finally(() => (isLoading = false));+ .finally(() => settingsForm.setLoading(false));}-function resetForm() {$formInputs.mobileNavigationMode.value = currentSettings.mobileNavigationMode;$formInputs.mobileNavigationShowLabels.value = currentSettings.mobileNavigationShowLabels;}onMount(() => {- if (formState) {- formState.saveFunction = onSubmit;- formState.resetFunction = resetForm;- }+ settingsForm.registerFormActions(onSubmit, resetForm);});</script>diff --git a/frontend/src/routes/settings/security/+page.svelte b/frontend/src/routes/settings/security/+page.svelteindex f26214648..f5029e4f5 100644--- a/frontend/src/routes/settings/security/+page.svelte+++ b/frontend/src/routes/settings/security/+page.svelte@@ -1,7 +1,7 @@<script lang="ts">import * as Card from '$lib/components/ui/card';import { z } from 'zod/v4';- import { getContext, onMount } from 'svelte';+ import { onMount } from 'svelte';import { createForm } from '$lib/utils/form.utils';import { Button } from '$lib/components/ui/button';import SwitchWithLabel from '$lib/components/form/labeled-switch.svelte';@@ -16,16 +16,13 @@import KeyIcon from '@lucide/svelte/icons/key';import TextInputWithLabel from '$lib/components/form/text-input-with-label.svelte';import settingsStore from '$lib/stores/config-store';- import { settingsService } from '$lib/services/settings-service';import { SettingsPageLayout } from '$lib/layouts';+ import { UseSettingsForm } from '$lib/hooks/use-settings-form.svelte';let { data }: { data: PageData } = $props();const currentSettings = $derived<Settings>($settingsStore || data.settings!);- let hasChanges = $state(false);- let isLoading = $state(false);-const isReadOnly = $derived.by(() => $settingsStore.uiConfigDisabled);- const formState = getContext('settingsFormState') as any;+const formSchema = z.object({authLocalEnabled: z.boolean(),@@ -62,37 +59,17 @@let { inputs: formInputs, ...form } = $derived(createForm<typeof formSchema>(formSchema, currentSettings));- const formHasChanges = $derived.by(- () =>+ const settingsForm = new UseSettingsForm({+ hasChangesChecker: () =>$formInputs.authLocalEnabled.value !== currentSettings.authLocalEnabled ||$formInputs.authOidcEnabled.value !== currentSettings.authOidcEnabled ||$formInputs.authSessionTimeout.value !== currentSettings.authSessionTimeout ||$formInputs.authPasswordPolicy.value !== currentSettings.authPasswordPolicy- );-- $effect(() => {- hasChanges = formHasChanges;- if (formState) {- formState.hasChanges = hasChanges;- formState.isLoading = isLoading;- }});// Helper: treat OIDC as active if forced by server or enabled in formconst isOidcActive = () => $formInputs.authOidcEnabled.value || data.oidcStatus.envForced;- async function updateSettingsConfig(updatedSettings: Partial<Settings>) {- try {- await settingsService.updateSettings(updatedSettings as any);- const updated = { ...currentSettings, ...updatedSettings };- settingsStore.set(updated);- settingsStore.reload();- } catch (error) {- console.error('Error updating settings:', error);- throw error;- }- }-async function onSubmit() {const formData = form.validate();if (!formData) {@@ -100,7 +77,7 @@return;}- isLoading = true;+ settingsForm.setLoading(true);let authOidcConfig = currentSettings.authOidcConfig;if (formData.authOidcEnabled && !data.oidcStatus.envForced) {@@ -114,19 +91,20 @@});}- await updateSettingsConfig({- authLocalEnabled: formData.authLocalEnabled,- authOidcEnabled: formData.authOidcEnabled,- authSessionTimeout: formData.authSessionTimeout,- authPasswordPolicy: formData.authPasswordPolicy,- ...(formData.authOidcEnabled && !data.oidcStatus.envForced && { authOidcConfig })- })+ await settingsForm+ .updateSettings({+ authLocalEnabled: formData.authLocalEnabled,+ authOidcEnabled: formData.authOidcEnabled,+ authSessionTimeout: formData.authSessionTimeout,+ authPasswordPolicy: formData.authPasswordPolicy,+ ...(formData.authOidcEnabled && !data.oidcStatus.envForced && { authOidcConfig })+ }).then(() => toast.success(m.security_settings_saved()))- .catch((error) => {+ .catch((error: any) => {console.error('Failed to save settings:', error);toast.error('Failed to save settings. Please try again.');})- .finally(() => (isLoading = false));+ .finally(() => settingsForm.setLoading(false));}function resetForm() {@@ -174,12 +152,12 @@async function handleSaveOidcConfig() {try {- isLoading = true;+ settingsForm.setLoading(true);$formInputs.authOidcEnabled.value = true;const formData = form.validate();if (!formData) {- isLoading = false;+ settingsForm.setLoading(false);return;}@@ -192,7 +170,7 @@adminValue: oidcConfigForm.adminValue || ''});- await updateSettingsConfig({+ await settingsForm.updateSettings({authOidcEnabled: true,authOidcConfig});@@ -200,15 +178,12 @@toast.success(m.security_oidc_saved());showOidcConfigDialog = false;} finally {- isLoading = false;+ settingsForm.setLoading(false);}}onMount(() => {- if (formState) {- formState.saveFunction = onSubmit;- formState.resetFunction = resetForm;- }+ settingsForm.registerFormActions(onSubmit, resetForm);});</script>