Skip to content

Modern Docker Management, Designed for Everyone

3.6 KB
Raw Blame
<script lang="ts">
	import { onMount } from 'svelte';

	import * as Alert from '#lib/components/ui/alert/index.js';
	import { useEnvironmentRefresh } from '#lib/hooks/use-environment-refresh.svelte.js';
	import { AlertTriangleIcon, UsersIcon } 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 userStore from '#lib/stores/user-store.svelte.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 SwarmNodesTable from './components/nodes-table.svelte';

	let { data } = $props();

	let nodes = $derived(data.nodes);
	let requestOptions = $derived(data.requestOptions);
	let isLoading = $state({ refresh: false });

	const currentEnvironmentId = $derived(environmentStore.selected?.id ?? null);

	async function refresh() {
		const environmentId = await environmentStore.getCurrentEnvironmentId();
		await simpleRefresh(
			() => swarmService.getNodes(requestOptions),
			(data) => {
				if (environmentId === currentEnvironmentId) nodes = data;
			},
			m.common_refresh_failed({ resource: m.nodes() }),
			(loading) => (isLoading.refresh = loading)
		);
	}

	useEnvironmentRefresh(refresh);

	onMount(() => {
		let active = true;
		let reconciledEnvironmentId: string | null = null;
		async function reconcileSelectedEnvironment() {
			await environmentStore.ready;
			const environmentId = environmentStore.selected?.id;
			if (!active || !environmentId || !hasPermission('swarm:nodes', environmentId) || reconciledEnvironmentId === environmentId)
				return;
			reconciledEnvironmentId = environmentId;
			const result = await tryCatch(swarmService.reconcileNodeAgents());
			if (!active || environmentId !== environmentStore.selected?.id || result.error !== null) return;
			await refresh();
		}
		const unsubscribeUser = userStore.onChange(() => {
			void reconcileSelectedEnvironment();
		});
		const unsubscribeEnvironment = environmentStore.subscribeSelected(() => {
			void reconcileSelectedEnvironment();
		});
		return () => {
			active = false;
			unsubscribeUser();
			unsubscribeEnvironment();
		};
	});

	const totalNodes = $derived(nodes?.pagination?.totalItems ?? nodes?.data?.length ?? 0);
	const uncoveredNodes = $derived(
		(nodes?.data ?? []).filter((node) => node.agent?.state !== 'connected' || node.agent?.connected === false)
	);
	const uncoveredNodeCount = $derived(uncoveredNodes.length);

	const actionButtons = $derived(
		createRefreshActionButtons({
			refreshLabel: m.common_refresh(),
			onRefresh: refresh,
			refreshing: isLoading.refresh
		})
	);

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

<ResourcePageLayout title={m.nodes()} subtitle={m.swarm_nodes_subtitle()} {actionButtons} {statCards} environmentPicker>
	{#snippet mainContent()}
		<div class="space-y-4">
			{#if uncoveredNodeCount > 0}
				<Alert.Root variant="warning-subtle">
					<AlertTriangleIcon class="size-4 text-warning" />
					<Alert.Title>{m.swarm_node_agent_warning_title()}</Alert.Title>
					<Alert.Description>{m.swarm_node_agent_warning_description({ count: uncoveredNodeCount })}</Alert.Description>
				</Alert.Root>
			{/if}

			<SwarmNodesTable bind:nodes bind:requestOptions />
		</div>
	{/snippet}
</ResourcePageLayout>