Skip to content

Modern Docker Management, Designed for Everyone

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

	import ServiceEditorDialog from '#lib/components/dialogs/service-editor-dialog.svelte';
	import { useEnvironmentRefresh } from '#lib/hooks/use-environment-refresh.svelte.js';
	import { DockIcon } from '#lib/icons/index.js';
	import { ResourcePageLayout, type StatCardConfig } from '#lib/layouts/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { swarmService } from '#lib/services/swarm-service.js';
	import { environmentStore } from '#lib/stores/environment.store.svelte.js';
	import type { SwarmServiceCreateSpec } from '#lib/types/swarm.js';
	import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
	import { simpleRefresh } from '#lib/utils/api.js';
	import { hasPermission } from '#lib/utils/auth.js';
	import { createRefreshActionButtons } from '#lib/utils/resource-actions.js';
	import { tryCatch } from '#lib/utils/try-catch.js';

	import SwarmServicesTable from './components/services-table.svelte';

	let { data } = $props();

	let services = $derived(data.services);
	let requestOptions = $derived(data.requestOptions);
	let isLoading = $state({ refresh: false, creating: false });
	let showCreateDialog = $state(false);

	async function refresh() {
		await simpleRefresh(
			() => swarmService.getServices(requestOptions),
			(data) => (services = data),
			m.common_refresh_failed({ resource: m.services() }),
			(loading) => (isLoading.refresh = loading)
		);
	}

	useEnvironmentRefresh(refresh);

	const totalServices = $derived(services?.pagination?.totalItems ?? services?.data?.length ?? 0);

	const currentEnvId = $derived(environmentStore.selected?.id);
	const canCreateService = $derived(hasPermission('swarm:services', currentEnvId));

	async function handleCreateService(payload: { spec: Record<string, unknown>; options?: Record<string, unknown> }) {
		const spec = payload.spec as unknown as SwarmServiceCreateSpec;
		isLoading.creating = true;
		await handleApiResultWithCallbacks({
			result: await tryCatch(swarmService.createService({ spec })),
			message: m.common_create_failed({ resource: `${m.swarm_service()} "${spec.Name}"` }),
			setLoadingState: (v) => (isLoading.creating = v),
			onSuccess: async () => {
				toast.success(m.common_create_success({ resource: `${m.swarm_service()} "${spec.Name}"` }));
				showCreateDialog = false;
				await refresh();
			}
		});
	}

	const actionButtons = $derived.by(() =>
		createRefreshActionButtons({
			create: {
				allowed: canCreateService,
				label: m.common_create_button({ resource: m.swarm_service() }),
				onclick: () => (showCreateDialog = true)
			},
			refreshLabel: m.common_refresh(),
			onRefresh: refresh,
			refreshing: isLoading.refresh
		})
	);

	const statCards: StatCardConfig[] = $derived([
		{
			title: m.swarm_services_total(),
			value: totalServices,
			icon: DockIcon,
			iconColor: 'text-info'
		}
	]);
</script>

{#if showCreateDialog}
	<ServiceEditorDialog
		bind:open={showCreateDialog}
		title={m.swarm_service_create_title()}
		description={m.swarm_service_create_description()}
		submitLabel={m.common_create()}
		submitAction="create"
		initialSpec=""
		isLoading={isLoading.creating}
		onSubmit={handleCreateService}
	/>
{/if}

<ResourcePageLayout title={m.services()} subtitle={m.swarm_services_subtitle()} {actionButtons} {statCards} environmentPicker>
	{#snippet mainContent()}
		<SwarmServicesTable bind:services bind:requestOptions />
	{/snippet}
</ResourcePageLayout>