Skip to content

Modern Docker Management, Designed for Everyone

4.5 KB
Raw Blame
<script lang="ts">
	import { toast } from 'svelte-sonner';

	import type { ActionButton } from '#lib/components/action-button-group/types.js';
	import { openConfirmDialog } from '#lib/components/confirm-dialog/index.js';
	import { EmptyState } from '#lib/components/states/index.js';
	import { CloudStorageIcon, RemoteEnvironmentIcon } from '#lib/icons/index.js';
	import { SettingsPageLayout } from '#lib/layouts/index.js';
	import * as m from '#lib/paraglide/messages.js';
	import { s3DestinationService } from '#lib/services/s3-destination-service.js';
	import settingsStore from '#lib/stores/config-store.svelte.js';
	import type { CreateS3Destination, S3Destination } from '#lib/types/s3-destination.js';
	import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
	import { discoverDestinationBackups } from '#lib/utils/backups.js';
	import { tryCatch } from '#lib/utils/try-catch.js';

	import S3DestinationDialog from './components/s3-destination-dialog.svelte';
	import S3DestinationTable from './components/s3-destination-table.svelte';

	let { data } = $props();
	let destinations = $derived(data.destinations);
	let requestOptions = $derived<SearchPaginationSortRequest>(data.requestOptions);
	let selected = $state<S3Destination | null>(null);
	let dialogOpen = $state(false);
	let destinationSession = $state(0);
	let saving = $state(false);
	const isReadOnly = $derived.by(() => settingsStore.current?.uiConfigDisabled);

	function openCreate() {
		selected = null;
		destinationSession += 1;
		dialogOpen = true;
	}

	function openEdit(destination: S3Destination) {
		selected = destination;
		destinationSession += 1;
		dialogOpen = true;
	}

	async function saveDestination(input: CreateS3Destination) {
		saving = true;
		try {
			let created: S3Destination | null = null;
			const operationResult = await tryCatch(
				(async () => {
					if (selected) {
						await s3DestinationService.update(selected.id, input);
						toast.success(m.s3_destination_updated({ name: input.name }));
					} else {
						created = await s3DestinationService.create(input);
						toast.success(m.s3_destination_created({ name: input.name }));
					}
					destinations = await s3DestinationService.list(requestOptions);
					dialogOpen = false;
					selected = null;
					if (created) await discoverDestinationBackups(created.id);
				})()
			);
			if (operationResult.error !== null) {
				const error = operationResult.error;

				toast.error(error instanceof Error ? error.message : m.s3_destination_save_failed());
			}
		} finally {
			saving = false;
		}
	}

	function deleteDestination(destination: S3Destination) {
		openConfirmDialog({
			title: m.delete_name({ name: destination.name }),
			message: m.s3_destination_delete_message(),
			confirm: {
				label: m.common_delete(),
				destructive: true,
				action: async () => {
					const operationResult = await tryCatch(
						(async () => {
							await s3DestinationService.delete(destination.id);
							destinations = await s3DestinationService.list(requestOptions);
							toast.success(m.s3_destination_deleted({ name: destination.name }));
						})()
					);
					if (operationResult.error !== null) {
						const error = operationResult.error;

						toast.error(error instanceof Error ? error.message : m.s3_destination_delete_failed());
					}
				}
			}
		});
	}

	const actionButtons: ActionButton[] = $derived.by(() => [
		{
			id: 'create',
			action: 'create',
			placement: 'primary',
			label: m.s3_destination_add(),
			onclick: openCreate,
			disabled: isReadOnly
		}
	]);
</script>

{#snippet destinationsEmptyState()}
	<EmptyState
		variant="plain"
		icon={CloudStorageIcon}
		title={m.s3_destinations_empty_title()}
		description={m.s3_destinations_empty_description()}
		actionLabel={isReadOnly ? undefined : m.s3_destination_add()}
		onAction={openCreate}
	/>
{/snippet}

<SettingsPageLayout
	title={m.s3_destinations_title()}
	description={m.s3_destinations_description()}
	icon={RemoteEnvironmentIcon}
	pageType="management"
	showReadOnlyTag={isReadOnly}
	{actionButtons}
>
	{#snippet mainContent()}
		<S3DestinationTable
			emptyState={destinationsEmptyState}
			bind:destinations
			bind:requestOptions
			onDestinationsChanged={(options) => s3DestinationService.list(options)}
			onEdit={openEdit}
			onDelete={deleteDestination}
		/>
	{/snippet}
	{#snippet additionalContent()}
		{#if destinationSession > 0}
			{#key destinationSession}
				<S3DestinationDialog bind:open={dialogOpen} destination={selected} {saving} onSubmit={saveDestination} />
			{/key}
		{/if}
	{/snippet}
</SettingsPageLayout>