Skip to content

Modern Docker Management, Designed for Everyone

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

	import type { ActionButton } from '#lib/components/action-button-group/types.js';
	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import WebhookFormSheet from '#lib/components/sheets/webhook-form-sheet.svelte';
	import * as ResponsiveDialog from '#lib/components/ui/responsive-dialog/index.js';
	import { Snippet } from '#lib/components/ui/snippet/index.js';
	import { GlobeIcon } from '#lib/icons/index.js';
	import { SettingsPageLayout } from '#lib/layouts/index.js';
	import * as m from '#lib/paraglide/messages.js';
	import { webhookService } from '#lib/services/webhook-service.js';
	import type { Webhook, WebhookCreated, CreateWebhook } from '#lib/types/environment.js';
	import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
	import { tryCatch } from '#lib/utils/try-catch.js';

	import WebhookTable from './components/webhook-table.svelte';

	let { data } = $props();

	let webhooks = $derived<Webhook[]>(data.webhooks);

	let isDialogOpen = $state({
		create: false,
		showToken: false
	});

	let newlyCreatedWebhook = $state<WebhookCreated | null>(null);

	let isLoading = $state({ creating: false });

	let newlyCreatedWebhookUrl = $derived.by(() => {
		if (!newlyCreatedWebhook?.token) {
			return '';
		}

		const origin = globalThis.location?.origin ?? '';
		return `${origin}/api/webhooks/trigger/${encodeURIComponent(newlyCreatedWebhook.token)}`;
	});

	async function handleCreateWebhook(webhook: CreateWebhook) {
		isLoading.creating = true;
		const result = await tryCatch(webhookService.create(webhook));
		await handleApiResultWithCallbacks({
			result,
			message: m.webhook_create_failed({ name: webhook.name }),
			setLoadingState: (value) => (isLoading.creating = value),
			onSuccess: async (created) => {
				toast.success(m.webhook_create_success({ name: webhook.name }));
				webhooks = await webhookService.getWebhooks();
				isDialogOpen.create = false;
				newlyCreatedWebhook = created as WebhookCreated;
				isDialogOpen.showToken = true;
			}
		});
	}

	const actionButtons: ActionButton[] = $derived.by(() => [
		{
			id: 'create',
			action: 'create',
			placement: 'primary',
			label: m.create_webhook(),
			onclick: () => (isDialogOpen.create = true),
			loading: isLoading.creating,
			disabled: isLoading.creating
		}
	]);
</script>

<SettingsPageLayout
	title={m.webhook_page_title()}
	description={m.webhook_page_description()}
	icon={GlobeIcon}
	pageType="management"
	{actionButtons}
	environmentPicker
>
	{#snippet mainContent()}
		<WebhookTable
			bind:webhooks
			onWebhooksChanged={async () => {
				webhooks = await webhookService.getWebhooks();
			}}
		/>
	{/snippet}

	{#snippet additionalContent()}
		<WebhookFormSheet bind:open={isDialogOpen.create} onSubmit={handleCreateWebhook} isLoading={isLoading.creating} />

		<ResponsiveDialog.Root
			bind:open={isDialogOpen.showToken}
			title={m.webhook_created_title()}
			description={m.webhook_created_description()}
			contentClass="!max-w-fit"
		>
			{#snippet children()}
				<div class="space-y-4 py-4">
					<div class="rounded-lg bg-muted p-4">
						<p class="mb-2 text-sm font-medium text-muted-foreground">{m.webhook_url()}</p>
						<Snippet
							text={newlyCreatedWebhookUrl}
							onCopy={(status) => {
								if (status === 'success') {
									toast.success(m.webhook_token_copied());
								}
							}}
						/>
					</div>
					<div class="rounded-lg border border-warning/30 bg-warning/10 p-4">
						<p class="text-sm text-warning">
							<strong>{m.common_important()}:</strong>
							{m.webhook_token_warning()}
						</p>
					</div>
				</div>
			{/snippet}
			{#snippet footer()}
				<ArcaneButton action="confirm" onclick={() => (isDialogOpen.showToken = false)} customLabel={m.common_done()} />
			{/snippet}
		</ResponsiveDialog.Root>
	{/snippet}
</SettingsPageLayout>