refactor: job create modal (#18106)

* refactor: job create modal

* chore: better modal manager types (#18107)
This commit is contained in:
Daniel Dietzler
2025-05-06 14:44:44 +02:00
committed by GitHub
parent 33f3751b72
commit 1138f6dcce
4 changed files with 83 additions and 68 deletions

View File

@@ -2,11 +2,14 @@ import ConfirmDialog from '$lib/components/shared-components/dialog/confirm-dial
import { mount, unmount, type Component, type ComponentProps } from 'svelte';
type OnCloseData<T> = T extends { onClose: (data: infer R) => void } ? R : never;
// TODO make `props` optional if component only has `onClose`
// type OptionalIfEmpty<T extends object> = keyof T extends never ? undefined : T;
type OptionalIfEmpty<T extends object> = keyof T extends never ? undefined : T;
class ModalManager {
open<T extends object, K = OnCloseData<T>>(Component: Component<T>, props: Omit<T, 'onClose'>) {
open<T extends object, K = OnCloseData<T>>(
Component: Component<T>,
props?: OptionalIfEmpty<Omit<T, 'onClose'>> | Record<string, never>,
): Promise<K>;
open<T extends object, K = OnCloseData<T>>(Component: Component<T>, props: OptionalIfEmpty<Omit<T, 'onClose'>>) {
return new Promise<K>((resolve) => {
let modal: object = {};
@@ -18,7 +21,7 @@ class ModalManager {
modal = mount(Component, {
target: document.body,
props: {
...(props as T),
...((props ?? {}) as T),
onClose,
},
});

View File

@@ -0,0 +1,65 @@
<script lang="ts">
import Combobox, { type ComboBoxOption } from '$lib/components/shared-components/combobox.svelte';
import ConfirmDialog from '$lib/components/shared-components/dialog/confirm-dialog.svelte';
import {
notificationController,
NotificationType,
} from '$lib/components/shared-components/notification/notification';
import { handleError } from '$lib/utils/handle-error';
import { createJob, ManualJobName } from '@immich/sdk';
import { t } from 'svelte-i18n';
type Props = { onClose: (confirmed: boolean) => void };
let { onClose }: Props = $props();
const options = [
{ title: $t('admin.person_cleanup_job'), value: ManualJobName.PersonCleanup },
{ title: $t('admin.tag_cleanup_job'), value: ManualJobName.TagCleanup },
{ title: $t('admin.user_cleanup_job'), value: ManualJobName.UserCleanup },
{ title: $t('admin.memory_cleanup_job'), value: ManualJobName.MemoryCleanup },
{ title: $t('admin.memory_generate_job'), value: ManualJobName.MemoryCreate },
{ title: $t('admin.backup_database'), value: ManualJobName.BackupDatabase },
].map(({ value, title }) => ({ id: value, label: title, value }));
let selectedJob: ComboBoxOption | undefined = $state(undefined);
const onsubmit = async (event: Event) => {
event.preventDefault();
await handleCreate();
};
const handleCreate = async () => {
if (!selectedJob) {
return;
}
try {
await createJob({ jobCreateDto: { name: selectedJob.value as ManualJobName } });
notificationController.show({ message: $t('admin.job_created'), type: NotificationType.Info });
onClose(true);
} catch (error) {
handleError(error, $t('errors.unable_to_submit_job'));
}
};
</script>
<ConfirmDialog
confirmColor="primary"
title={$t('admin.create_job')}
disabled={!selectedJob}
onClose={(confirmed) => (confirmed ? handleCreate() : onClose(false))}
>
{#snippet promptSnippet()}
<form {onsubmit} autocomplete="off" id="create-tag-form" class="w-full">
<div class="flex flex-col gap-1 text-start">
<Combobox
bind:selectedOption={selectedJob}
label={$t('jobs')}
{options}
placeholder={$t('admin.search_jobs')}
/>
</div>
</form>
{/snippet}
</ConfirmDialog>