2023-09-20 13:16:33 +02:00
|
|
|
<script lang="ts">
|
2024-02-20 16:53:12 +01:00
|
|
|
import Icon from '$lib/components/elements/icon.svelte';
|
2025-05-31 15:30:08 +02:00
|
|
|
import { modalManager } from '$lib/managers/modal-manager.svelte';
|
|
|
|
|
import LibraryImportPathModal from '$lib/modals/LibraryImportPathModal.svelte';
|
2024-02-29 11:22:39 -05:00
|
|
|
import type { ValidateLibraryImportPathResponseDto } from '@immich/sdk';
|
2025-03-12 17:00:16 -04:00
|
|
|
import { validate, type LibraryResponseDto } from '@immich/sdk';
|
2025-06-02 09:47:23 -05:00
|
|
|
import { Button, IconButton } from '@immich/ui';
|
2025-03-12 17:00:16 -04:00
|
|
|
import { mdiAlertOutline, mdiCheckCircleOutline, mdiPencilOutline, mdiRefresh } from '@mdi/js';
|
|
|
|
|
import { onMount } from 'svelte';
|
2024-06-04 21:53:00 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
2025-03-12 17:00:16 -04:00
|
|
|
import { handleError } from '../../utils/handle-error';
|
|
|
|
|
import { NotificationType, notificationController } from '../shared-components/notification/notification';
|
2023-09-20 13:16:33 +02:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
interface Props {
|
|
|
|
|
library: LibraryResponseDto;
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
onSubmit: (library: LibraryResponseDto) => void;
|
|
|
|
|
}
|
2023-09-20 13:16:33 +02:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
let { library = $bindable(), onCancel, onSubmit }: Props = $props();
|
2023-09-20 13:16:33 +02:00
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
let validatedPaths: ValidateLibraryImportPathResponseDto[] = $state([]);
|
|
|
|
|
|
|
|
|
|
let importPaths = $derived(validatedPaths.map((validatedPath) => validatedPath.importPath));
|
2023-09-20 13:16:33 +02:00
|
|
|
|
2024-02-20 16:53:12 +01:00
|
|
|
onMount(async () => {
|
2023-09-20 13:16:33 +02:00
|
|
|
if (library.importPaths) {
|
2024-02-20 16:53:12 +01:00
|
|
|
await handleValidation();
|
2023-09-20 13:16:33 +02:00
|
|
|
} else {
|
|
|
|
|
library.importPaths = [];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-20 16:53:12 +01:00
|
|
|
const handleValidation = async () => {
|
|
|
|
|
if (library.importPaths) {
|
|
|
|
|
const validation = await validate({
|
|
|
|
|
id: library.id,
|
|
|
|
|
validateLibraryDto: { importPaths: library.importPaths },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
validatedPaths = validation.importPaths ?? [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const revalidate = async (notifyIfSuccessful = true) => {
|
|
|
|
|
await handleValidation();
|
|
|
|
|
let failedPaths = 0;
|
|
|
|
|
for (const validatedPath of validatedPaths) {
|
|
|
|
|
if (!validatedPath.isValid) {
|
|
|
|
|
failedPaths++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (failedPaths === 0) {
|
|
|
|
|
if (notifyIfSuccessful) {
|
|
|
|
|
notificationController.show({
|
2024-06-12 12:54:40 +02:00
|
|
|
message: $t('admin.paths_validated_successfully'),
|
2024-02-20 16:53:12 +01:00
|
|
|
type: NotificationType.Info,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
notificationController.show({
|
2024-06-12 12:54:40 +02:00
|
|
|
message: $t('errors.paths_validation_failed', { values: { paths: failedPaths } }),
|
2024-02-20 16:53:12 +01:00
|
|
|
type: NotificationType.Warning,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-31 15:30:08 +02:00
|
|
|
const handleAddImportPath = async (importPathToAdd: string | null) => {
|
|
|
|
|
if (!importPathToAdd) {
|
2023-09-20 13:16:33 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!library.importPaths) {
|
|
|
|
|
library.importPaths = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-02-09 01:09:09 +01:00
|
|
|
// Check so that import path isn't duplicated
|
|
|
|
|
if (!library.importPaths.includes(importPathToAdd)) {
|
|
|
|
|
library.importPaths.push(importPathToAdd);
|
2024-02-20 16:53:12 +01:00
|
|
|
await revalidate(false);
|
2024-02-09 01:09:09 +01:00
|
|
|
}
|
2023-09-20 13:16:33 +02:00
|
|
|
} catch (error) {
|
2024-06-12 12:54:40 +02:00
|
|
|
handleError(error, $t('errors.unable_to_add_import_path'));
|
2023-09-20 13:16:33 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-31 15:30:08 +02:00
|
|
|
const handleEditImportPath = async (editedImportPath: string | null, pathIndexToEdit: number) => {
|
|
|
|
|
if (editedImportPath === null) {
|
2023-09-20 13:16:33 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!library.importPaths) {
|
|
|
|
|
library.importPaths = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-02-09 01:09:09 +01:00
|
|
|
// Check so that import path isn't duplicated
|
|
|
|
|
if (!library.importPaths.includes(editedImportPath)) {
|
|
|
|
|
// Update import path
|
2025-05-31 15:30:08 +02:00
|
|
|
library.importPaths[pathIndexToEdit] = editedImportPath;
|
2024-02-20 16:53:12 +01:00
|
|
|
await revalidate(false);
|
2024-02-09 01:09:09 +01:00
|
|
|
}
|
2023-09-20 13:16:33 +02:00
|
|
|
} catch (error) {
|
2024-06-12 12:54:40 +02:00
|
|
|
handleError(error, $t('errors.unable_to_edit_import_path'));
|
2023-09-20 13:16:33 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-31 15:30:08 +02:00
|
|
|
const handleDeleteImportPath = async (pathIndexToDelete?: number) => {
|
|
|
|
|
if (pathIndexToDelete === undefined) {
|
2023-09-20 13:16:33 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!library.importPaths) {
|
|
|
|
|
library.importPaths = [];
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 15:30:08 +02:00
|
|
|
const pathToDelete = library.importPaths[pathIndexToDelete];
|
2023-09-20 13:16:33 +02:00
|
|
|
library.importPaths = library.importPaths.filter((path) => path != pathToDelete);
|
2024-02-20 16:53:12 +01:00
|
|
|
await handleValidation();
|
2023-09-20 13:16:33 +02:00
|
|
|
} catch (error) {
|
2024-06-12 12:54:40 +02:00
|
|
|
handleError(error, $t('errors.unable_to_delete_import_path'));
|
2025-05-31 15:30:08 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEditImportPath = async (pathIndexToEdit?: number) => {
|
|
|
|
|
const result = await modalManager.show(LibraryImportPathModal, {
|
|
|
|
|
title: pathIndexToEdit === undefined ? $t('add_import_path') : $t('edit_import_path'),
|
|
|
|
|
submitText: pathIndexToEdit === undefined ? $t('add') : $t('save'),
|
|
|
|
|
isEditing: pathIndexToEdit !== undefined,
|
|
|
|
|
importPath: pathIndexToEdit === undefined ? null : library.importPaths[pathIndexToEdit],
|
|
|
|
|
importPaths: library.importPaths,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (result.action) {
|
|
|
|
|
case 'submit': {
|
|
|
|
|
// eslint-disable-next-line unicorn/prefer-ternary
|
|
|
|
|
if (pathIndexToEdit === undefined) {
|
|
|
|
|
await handleAddImportPath(result.importPath);
|
|
|
|
|
} else {
|
|
|
|
|
await handleEditImportPath(result.importPath, pathIndexToEdit);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'delete': {
|
|
|
|
|
await handleDeleteImportPath(pathIndexToEdit);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-20 13:16:33 +02:00
|
|
|
}
|
|
|
|
|
};
|
2024-11-14 08:43:25 -06:00
|
|
|
|
|
|
|
|
const onsubmit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
onSubmit({ ...library });
|
|
|
|
|
};
|
2023-09-20 13:16:33 +02:00
|
|
|
</script>
|
|
|
|
|
|
2024-11-14 08:43:25 -06:00
|
|
|
<form {onsubmit} autocomplete="off" class="m-4 flex flex-col gap-4">
|
2025-04-28 09:53:53 -04:00
|
|
|
<table class="text-start">
|
2023-09-20 13:16:33 +02:00
|
|
|
<tbody class="block w-full overflow-y-auto rounded-md border dark:border-immich-dark-gray">
|
2025-03-03 14:24:26 +00:00
|
|
|
{#each validatedPaths as validatedPath, listIndex (validatedPath.importPath)}
|
2023-09-20 13:16:33 +02:00
|
|
|
<tr
|
2025-05-12 23:17:01 +02:00
|
|
|
class="flex h-[80px] w-full place-items-center text-center dark:text-immich-dark-fg even:bg-subtle/20 odd:bg-subtle/80"
|
2023-09-20 13:16:33 +02:00
|
|
|
>
|
2025-04-28 09:53:53 -04:00
|
|
|
<td class="w-1/8 text-ellipsis ps-8 text-sm">
|
2024-02-20 16:53:12 +01:00
|
|
|
{#if validatedPath.isValid}
|
2025-06-12 19:06:38 -04:00
|
|
|
<Icon path={mdiCheckCircleOutline} size="24" title={validatedPath.message} class="text-success" />
|
2024-02-20 16:53:12 +01:00
|
|
|
{:else}
|
2025-06-12 19:06:38 -04:00
|
|
|
<Icon path={mdiAlertOutline} size="24" title={validatedPath.message} class="text-warning" />
|
2024-02-20 16:53:12 +01:00
|
|
|
{/if}
|
|
|
|
|
</td>
|
|
|
|
|
|
|
|
|
|
<td class="w-4/5 text-ellipsis px-4 text-sm">{validatedPath.importPath}</td>
|
2024-05-04 18:29:50 +00:00
|
|
|
<td class="w-1/5 text-ellipsis flex justify-center">
|
2025-06-02 09:47:23 -05:00
|
|
|
<IconButton
|
|
|
|
|
shape="round"
|
2024-05-04 18:29:50 +00:00
|
|
|
color="primary"
|
|
|
|
|
icon={mdiPencilOutline}
|
2025-06-02 09:47:23 -05:00
|
|
|
aria-label={$t('edit_import_path')}
|
2025-05-31 15:30:08 +02:00
|
|
|
onclick={() => onEditImportPath(listIndex)}
|
2025-06-02 09:47:23 -05:00
|
|
|
size="small"
|
2024-05-04 18:29:50 +00:00
|
|
|
/>
|
2023-09-20 13:16:33 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
{/each}
|
|
|
|
|
<tr
|
2025-05-12 23:17:01 +02:00
|
|
|
class="flex h-[80px] w-full place-items-center text-center dark:text-immich-dark-fg even:bg-subtle/20 odd:bg-subtle/80"
|
2023-09-20 13:16:33 +02:00
|
|
|
>
|
2024-02-09 01:09:09 +01:00
|
|
|
<td class="w-4/5 text-ellipsis px-4 text-sm">
|
|
|
|
|
{#if importPaths.length === 0}
|
2024-06-12 12:54:40 +02:00
|
|
|
{$t('admin.no_paths_added')}
|
2024-02-09 01:09:09 +01:00
|
|
|
{/if}</td
|
|
|
|
|
>
|
2025-03-12 17:00:16 -04:00
|
|
|
<td class="w-1/5 text-ellipsis px-4 text-sm">
|
2025-05-31 15:30:08 +02:00
|
|
|
<Button shape="round" size="small" onclick={() => onEditImportPath()}>{$t('add_path')}</Button>
|
2025-03-12 17:00:16 -04:00
|
|
|
</td>
|
2024-02-29 19:35:37 +01:00
|
|
|
</tr>
|
2023-09-20 13:16:33 +02:00
|
|
|
</tbody>
|
|
|
|
|
</table>
|
2024-02-20 16:53:12 +01:00
|
|
|
<div class="flex justify-between w-full">
|
|
|
|
|
<div class="justify-end gap-2">
|
2025-03-12 17:00:16 -04:00
|
|
|
<Button shape="round" leadingIcon={mdiRefresh} size="small" color="secondary" onclick={() => revalidate()}
|
|
|
|
|
>{$t('validate')}</Button
|
2024-06-04 21:53:00 +02:00
|
|
|
>
|
2024-02-20 16:53:12 +01:00
|
|
|
</div>
|
2025-03-12 17:00:16 -04:00
|
|
|
<div class="flex justify-end gap-2">
|
|
|
|
|
<Button shape="round" size="small" color="secondary" onclick={onCancel}>{$t('cancel')}</Button>
|
|
|
|
|
<Button shape="round" size="small" type="submit">{$t('save')}</Button>
|
2024-02-20 16:53:12 +01:00
|
|
|
</div>
|
2023-09-20 13:16:33 +02:00
|
|
|
</div>
|
|
|
|
|
</form>
|