Skip to content

Modern Docker Management, Designed for Everyone

3.6 KB
Raw Blame
<script lang="ts">
	import { createMutation } from '@tanstack/svelte-query';
	import { z } from 'zod/v4';

	import FormInput from '#lib/components/form/form-input.svelte';
	import SwitchWithLabel from '#lib/components/form/labeled-switch.svelte';
	import SheetFooterActions from '#lib/components/sheets/sheet-footer-actions.svelte';
	import * as Alert from '#lib/components/ui/alert/index.js';
	import * as ResponsiveDialog from '#lib/components/ui/responsive-dialog/index.js';
	import { AlertIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { templateService } from '#lib/services/template-service.js';
	import { createForm, preventDefault } from '#lib/utils/settings.svelte.js';

	type TemplateRegistryFormProps = {
		open: boolean;
		onSubmit: (registry: { name: string; url: string; description?: string; enabled: boolean }) => void;
		isLoading: boolean;
	};

	let { open = $bindable(false), onSubmit, isLoading }: TemplateRegistryFormProps = $props();

	const formSchema = z.object({
		url: z.url().min(1, m.registry_url_is_required()),
		enabled: z.boolean().default(true)
	});

	let formData = $derived({
		url: '',
		enabled: true
	});

	let form = $derived(createForm<typeof formSchema>(formSchema, formData));
	let inputs = $derived(form.inputs);

	let submitError = $state<string | null>(null);
	const validateRegistryMutation = createMutation(() => ({
		mutationFn: ({ url }: { url: string; enabled: boolean }) => templateService.fetchRegistry(url),
		onSuccess: (registryData, variables) => {
			if (!registryData.name || !registryData.templates || !Array.isArray(registryData.templates)) {
				submitError = m.templates_registry_invalid_format();
				return;
			}

			onSubmit({
				name: registryData.name,
				url: variables.url,
				description: registryData.description || '',
				enabled: variables.enabled
			});
		},
		onError: (error) => {
			submitError = error instanceof Error ? error.message : m.templates_registry_validate_failed();
		}
	}));
	const isValidating = $derived(validateRegistryMutation.isPending);

	function handleSubmit() {
		submitError = null;

		const data = form.validate();
		if (!data) return;
		validateRegistryMutation.mutate({ url: data.url, enabled: data.enabled });
	}

	function handleOpenChange(newOpenState: boolean) {
		open = newOpenState;
		if (!newOpenState) {
			submitError = null;
		}
	}
</script>

<ResponsiveDialog.Root
	{open}
	onOpenChange={handleOpenChange}
	variant="sheet"
	title={m.templates_add_registry_title()}
	description={m.templates_add_registry_description()}
	contentClass="sm:max-w-125"
>
	{#snippet children()}
		<form onsubmit={preventDefault(handleSubmit)} class="grid gap-4 py-6">
			<FormInput
				label={m.templates_registry_url_label()}
				type="text"
				placeholder={m.templates_registry_url_placeholder()}
				description={m.templates_registry_url_description()}
				bind:input={inputs.url}
			/>

			<SwitchWithLabel
				id="enabledSwitch"
				label={m.templates_enable_registry_label()}
				description={m.templates_enable_registry_description()}
				bind:checked={inputs.enabled.value}
			/>

			{#if submitError}
				<Alert.Root variant="destructive-subtle">
					<AlertIcon class="size-4" />
					<Alert.Title>{m.templates_registry_validation_error_title()}</Alert.Title>
					<Alert.Description>{submitError}</Alert.Description>
				</Alert.Root>
			{/if}
		</form>
	{/snippet}

	{#snippet footer()}
		<SheetFooterActions
			bind:open
			cancelDisabled={isLoading || isValidating}
			submitDisabled={isLoading || isValidating}
			submitLoading={isLoading || isValidating}
			onSubmit={handleSubmit}
			submitLabel={m.templates_add_registry_button()}
		/>
	{/snippet}
</ResponsiveDialog.Root>