Skip to content

Modern Docker Management, Designed for Everyone

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

	import ArcaneTable from '#lib/components/arcane-table/arcane-table.svelte';
	import RelativeTimeCell from '#lib/components/arcane-table/cells/relative-time-cell.svelte';
	import type { ColumnSpec, MobileFieldVisibility } from '#lib/components/arcane-table/index.js';
	import { UniversalMobileCard } from '#lib/components/arcane-table/index.js';
	import RowActionsMenu from '#lib/components/arcane-table/row-actions-menu.svelte';
	import { openConfirmDialog } from '#lib/components/confirm-dialog/index.js';
	import EventDetailPanel from '#lib/components/events/event-detail-panel.svelte';
	import {
		eventSeverityIconVariant,
		eventSeverityLabel,
		eventSeverityVariant,
		eventTypeFilters,
		eventTypeIcon,
		eventTypeLabel
	} from '#lib/components/events/events-labels.js';
	import IfPermitted from '#lib/components/if-permitted.svelte';
	import { Badge } from '#lib/components/ui/badge/index.js';
	import * as DropdownMenu from '#lib/components/ui/dropdown-menu/index.js';
	import { TrashIcon, NotificationsIcon, TagIcon, EnvironmentsIcon, UserIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { eventService } from '#lib/services/event-service.js';
	import { environmentStore } from '#lib/stores/environment.store.svelte.js';
	import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
	import type { Event } from '#lib/types/shared.js';
	import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
	import { formatRelativeTime } from '#lib/utils/formatting.js';
	import { tryCatch } from '#lib/utils/try-catch.js';

	let {
		events = $bindable(),
		selectedIds = $bindable(),
		requestOptions = $bindable(),
		onRefreshData
	}: {
		events: Paginated<Event>;
		selectedIds: string[];
		requestOptions: SearchPaginationSortRequest;
		onRefreshData?: (options: SearchPaginationSortRequest) => Promise<void>;
	} = $props();

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

	async function refreshEvents(options: SearchPaginationSortRequest = requestOptions) {
		if (onRefreshData) {
			await onRefreshData(options);
			return;
		}
		events = await eventService.getEvents(options);
	}

	async function handleDeleteEvent(eventId: string, title: string) {
		const safeTitle = title?.trim() || m.common_unknown();
		openConfirmDialog({
			title: m.common_delete_title({ resource: 'event' }),
			message: m.events_delete_confirm_message({ title: safeTitle }),
			confirm: {
				label: m.common_delete(),
				destructive: true,
				action: async () => {
					isLoading.removing = true;
					await handleApiResultWithCallbacks({
						result: await tryCatch(eventService.delete(eventId)),
						message: m.events_delete_failed({ title: safeTitle }),
						setLoadingState: (value) => (isLoading.removing = value),
						onSuccess: async () => {
							toast.success(m.events_delete_success({ title: safeTitle }));
							await refreshEvents();
						}
					});
				}
			}
		});
	}

	function environmentName(environmentId: string): string {
		return environmentStore.available.find((e) => e.id === environmentId)?.name || environmentId;
	}

	const columns = [
		{
			accessorKey: 'title',
			title: m.events_col_event(),
			cell: SummaryCell
		},
		{
			accessorKey: 'severity',
			title: m.events_col_severity(),
			sortable: true,
			cell: SeverityCell
		},
		{
			accessorKey: 'type',
			title: m.common_type(),
			sortable: true,
			filterOptions: eventTypeFilters,
			cell: TypeCell
		},
		{
			id: 'resource',
			title: m.resource(),
			cell: ResourceCell
		},
		{
			id: 'environment',
			title: m.resource_environment_cap(),
			cell: EnvironmentCell
		},
		{
			accessorKey: 'username',
			title: m.common_user(),
			sortable: true,
			cell: UserCell
		},
		{
			accessorKey: 'timestamp',
			title: m.events_col_time(),
			sortable: true,
			cellComponent: RelativeTimeCell
		}
	] satisfies ColumnSpec<Event>[];

	const mobileFields = [
		{ id: 'severity', label: m.events_col_severity(), defaultVisible: true },
		{ id: 'type', label: m.common_type(), defaultVisible: true },
		{ id: 'resource', label: m.resource(), defaultVisible: true },
		{ id: 'environment', label: m.resource_environment_cap(), defaultVisible: true },
		{ id: 'username', label: m.common_user(), defaultVisible: true },
		{ id: 'timestamp', label: m.events_col_time(), defaultVisible: true }
	];

	let mobileFieldVisibility = $state<Record<string, boolean>>({});
</script>

{#snippet SummaryCell({ item }: { item: Event })}
	<div class="max-w-xl min-w-48 py-1 whitespace-normal">
		<p class="text-sm font-semibold break-words">{item.title}</p>
		{#if item.description}
			<p class="mt-0.5 line-clamp-2 text-xs text-muted-foreground" title={item.description}>{item.description}</p>
		{/if}
	</div>
{/snippet}

{#snippet SeverityCell({ value }: { value: unknown })}
	{@const severity = String(value ?? 'info')}
	<Badge variant={eventSeverityVariant(severity)} minWidth="20">{eventSeverityLabel(severity)}</Badge>
{/snippet}

{#snippet TypeCell({ value }: { value: unknown })}
	{@const type = String(value ?? '')}
	{@const TypeIcon = eventTypeIcon(type)}
	<div class="flex min-w-0 items-center gap-2">
		<TypeIcon class="size-4 shrink-0 text-muted-foreground" aria-hidden="true" />
		<span class="truncate text-sm" title={type}>{eventTypeLabel(type)}</span>
	</div>
{/snippet}

{#snippet ResourceCell({ item }: { item: Event })}
	{#if item.resourceName || item.resourceType}
		<div class="min-w-0">
			<p class="truncate text-sm" title={item.resourceName}>{item.resourceName ?? '—'}</p>
			{#if item.resourceType}
				<p class="text-xs text-muted-foreground capitalize">{item.resourceType}</p>
			{/if}
		</div>
	{:else}
		<span class="text-muted-foreground">—</span>
	{/if}
{/snippet}

{#snippet EnvironmentCell({ item }: { item: Event })}
	{#if item.environmentId}
		<Badge variant="gray">{environmentName(item.environmentId)}</Badge>
	{:else}
		<span class="text-muted-foreground">—</span>
	{/if}
{/snippet}

{#snippet UserCell({ value }: { value: unknown })}
	{#if String(value ?? '') === 'System'}
		<span class="text-sm text-muted-foreground italic">{m.system()}</span>
	{:else}
		<span class="text-sm">{String(value ?? '—')}</span>
	{/if}
{/snippet}

{#snippet ExpandedEvent({ item }: { item: Event })}
	<EventDetailPanel event={item} />
{/snippet}

{#snippet EventMobileCardSnippet({ item, mobileFieldVisibility }: { item: Event; mobileFieldVisibility: MobileFieldVisibility })}
	<UniversalMobileCard
		{item}
		icon={(item: Event) => ({
			component: NotificationsIcon,
			variant: eventSeverityIconVariant(item.severity)
		})}
		title={(item: Event) => item.title}
		subtitle={(item: Event) => ((mobileFieldVisibility['timestamp'] ?? true) ? formatRelativeTime(item.timestamp) : null)}
		badges={[
			(item: Event) =>
				(mobileFieldVisibility['severity'] ?? true)
					? {
							variant: eventSeverityVariant(item.severity),
							text: eventSeverityLabel(item.severity)
						}
					: null
		]}
		fields={[
			{
				label: m.common_type(),
				getValue: (item: Event) => eventTypeLabel(item.type),
				icon: TagIcon,
				iconVariant: 'gray' as const,
				show: mobileFieldVisibility['type'] ?? true
			},
			{
				label: m.resource(),
				getValue: (item: Event) => {
					if (!item.resourceType && !item.resourceName) return null;
					const parts = [item.resourceName || '—'];
					if (item.resourceType) parts.push(item.resourceType);
					return parts.join(' · ');
				},
				icon: EnvironmentsIcon,
				iconVariant: 'gray' as const,
				show: (mobileFieldVisibility['resource'] ?? true) && (!!item.resourceType || !!item.resourceName)
			},
			{
				label: m.resource_environment_cap(),
				getValue: (item: Event) => (item.environmentId ? environmentName(item.environmentId) : null),
				icon: EnvironmentsIcon,
				iconVariant: 'gray' as const,
				show: (mobileFieldVisibility['environment'] ?? true) && !!item.environmentId
			},
			{
				label: m.common_user(),
				getValue: (item: Event) => item.username,
				icon: UserIcon,
				iconVariant: 'gray' as const,
				show: (mobileFieldVisibility['username'] ?? true) && !!item.username
			}
		]}
		rowActions={RowActions}
	>
		{#if item.description}
			<p class="text-xs leading-relaxed break-words text-muted-foreground">{item.description}</p>
		{/if}
	</UniversalMobileCard>
{/snippet}

{#snippet RowActions({ item }: { item: Event })}
	<IfPermitted perm="events:delete">
		<RowActionsMenu>
			<DropdownMenu.Item
				variant="destructive"
				onclick={() => handleDeleteEvent(item.id, item.title)}
				disabled={isLoading.removing}
			>
				<TrashIcon class="size-4" />
				{m.common_delete()}
			</DropdownMenu.Item>
		</RowActionsMenu>
	</IfPermitted>
{/snippet}

<ArcaneTable
	persistKey="arcane-events-table"
	items={events}
	bind:requestOptions
	bind:selectedIds
	bind:mobileFieldVisibility
	onRefresh={async (options) => {
		requestOptions = options;
		await refreshEvents(options);
		return events;
	}}
	{columns}
	{mobileFields}
	rowActions={RowActions}
	mobileCard={EventMobileCardSnippet}
	expandedRowContent={ExpandedEvent}
/>