From b67fddf4b88389e3cb4266a1bf60e1f185c96c13 Mon Sep 17 00:00:00 2001 From: Jonathan Jogenfors Date: Fri, 9 Feb 2024 01:09:09 +0100 Subject: [PATCH] fix(web): Handle duplicate library settings gracefully (#6950) * don't add duplicate import paths * improve library import paths form * same for exclusion patterns * remove newline --- .../library-exclusion-pattern-form.svelte | 22 ++++++++++-- .../forms/library-import-path-form.svelte | 27 ++++++++++++--- .../forms/library-import-paths-form.svelte | 34 ++++++++++++++----- .../forms/library-scan-settings-form.svelte | 19 +++++++---- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/web/src/lib/components/forms/library-exclusion-pattern-form.svelte b/web/src/lib/components/forms/library-exclusion-pattern-form.svelte index fccf3b57b9..439ae2b368 100644 --- a/web/src/lib/components/forms/library-exclusion-pattern-form.svelte +++ b/web/src/lib/components/forms/library-exclusion-pattern-form.svelte @@ -4,11 +4,22 @@ import FullScreenModal from '../shared-components/full-screen-modal.svelte'; import Icon from '$lib/components/elements/icon.svelte'; import { mdiFolderRemove } from '@mdi/js'; + import { onMount } from 'svelte'; export let exclusionPattern: string; - export let canDelete = false; + export let exclusionPatterns: string[] = []; + export let isEditing = false; export let submitText = 'Submit'; + onMount(() => { + if (isEditing) { + exclusionPatterns = exclusionPatterns.filter((pattern) => pattern !== exclusionPattern); + } + }); + + $: isDuplicate = exclusionPattern !== null && exclusionPatterns.includes(exclusionPattern); + $: canSubmit = exclusionPattern !== '' && exclusionPattern !== null && !exclusionPatterns.includes(exclusionPattern); + const dispatch = createEventDispatcher<{ cancel: void; submit: { excludePattern: string }; @@ -49,11 +60,16 @@
- {#if canDelete} + {#if isEditing} {/if} - + +
+
+ {#if isDuplicate} +

This exclusion pattern already exists.

+ {/if}
diff --git a/web/src/lib/components/forms/library-import-path-form.svelte b/web/src/lib/components/forms/library-import-path-form.svelte index f01b902dce..06a5f63ea5 100644 --- a/web/src/lib/components/forms/library-import-path-form.svelte +++ b/web/src/lib/components/forms/library-import-path-form.svelte @@ -4,16 +4,27 @@ import Button from '../elements/buttons/button.svelte'; import FullScreenModal from '../shared-components/full-screen-modal.svelte'; import { mdiFolderSync } from '@mdi/js'; + import { onMount } from 'svelte'; - export let importPath: string; + export let importPath: string | null; + export let importPaths: string[] = []; export let title = 'Import path'; export let cancelText = 'Cancel'; export let submitText = 'Save'; - export let canDelete = false; + export let isEditing = false; + + onMount(() => { + if (isEditing) { + importPaths = importPaths.filter((path) => path !== importPath); + } + }); + + $: isDuplicate = importPath !== null && importPaths.includes(importPath); + $: canSubmit = importPath !== '' && importPath !== null && !importPaths.includes(importPath); const dispatch = createEventDispatcher<{ cancel: void; - submit: { importPath: string }; + submit: { importPath: string | null }; delete: void; }>(); const handleCancel = () => dispatch('cancel'); @@ -47,11 +58,17 @@
- {#if canDelete} + {#if isEditing} {/if} - + +
+ +
+ {#if isDuplicate} +

This import path already exists.

+ {/if}
diff --git a/web/src/lib/components/forms/library-import-paths-form.svelte b/web/src/lib/components/forms/library-import-paths-form.svelte index 8659cdcd05..bd32b39b5e 100644 --- a/web/src/lib/components/forms/library-import-paths-form.svelte +++ b/web/src/lib/components/forms/library-import-paths-form.svelte @@ -13,7 +13,7 @@ let addImportPath = false; let editImportPath: number | null = null; - let importPathToAdd: string; + let importPathToAdd: string | null = null; let editedImportPath: string; let importPaths: string[] = []; @@ -39,7 +39,7 @@ }; const handleAddImportPath = async () => { - if (!addImportPath) { + if (!addImportPath || !importPathToAdd) { return; } @@ -48,12 +48,16 @@ } try { - library.importPaths.push(importPathToAdd); - importPaths = library.importPaths; + // Check so that import path isn't duplicated + if (!library.importPaths.includes(importPathToAdd)) { + library.importPaths.push(importPathToAdd); + importPaths = library.importPaths; + } } catch (error) { - handleError(error, 'Unable to remove import path'); + handleError(error, 'Unable to add import path'); } finally { addImportPath = false; + importPathToAdd = null; } }; @@ -67,8 +71,13 @@ } try { - library.importPaths[editImportPath] = editedImportPath; - importPaths = library.importPaths; + // Check so that import path isn't duplicated + + if (!library.importPaths.includes(editedImportPath)) { + // Update import path + library.importPaths[editImportPath] = editedImportPath; + importPaths = library.importPaths; + } } catch (error) { editImportPath = null; handleError(error, 'Unable to edit import path'); @@ -103,9 +112,11 @@ title="Add Import Path" submitText="Add" bind:importPath={importPathToAdd} + {importPaths} on:submit={handleAddImportPath} on:cancel={() => { addImportPath = false; + importPathToAdd = null; }} /> {/if} @@ -114,8 +125,9 @@ { @@ -157,7 +169,11 @@ : 'bg-immich-bg dark:bg-immich-dark-gray/50' }`} > - + + {#if importPaths.length === 0} + No paths added + {/if}