Skip to content

Modern Docker Management, Designed for Everyone

8.8 KB
Raw Blame
<script lang="ts" generics="T">
	import type { Snippet, Component } from 'svelte';

	import EnvironmentCell from '#lib/components/arcane-table/cells/environment-cell.svelte';
	import { Badge } from '#lib/components/ui/badge/index.js';
	import * as Card from '#lib/components/ui/card/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { environmentStore } from '#lib/stores/environment.store.svelte.js';
	import { cn } from '#lib/utils.js';

	type IconVariant = 'emerald' | 'red' | 'amber' | 'blue' | 'purple' | 'gray' | 'sky' | 'orange';
	type BadgeVariant = 'green' | 'red' | 'amber' | 'blue' | 'purple' | 'gray' | 'orange';

	interface IconConfig {
		component: Component<any>;
		variant: IconVariant;
		imageUrl?: string;
		alt?: string;
	}

	interface BadgeConfig {
		variant: BadgeVariant;
		text: string;
	}

	type FieldType = 'text' | 'badge' | 'date' | 'mono' | 'component';

	interface FieldDefinition<T> {
		label: string;
		getValue: (item: T) => any;
		type?: FieldType;
		icon?: Component<any>;
		iconVariant?: IconVariant;
		badgeVariant?: BadgeVariant;
		component?: Snippet<[value: any]>;
		show?: boolean;
	}

	interface FooterConfig<T> {
		label: string;
		getValue: (item: T) => string;
		icon: Component<any>;
	}

	let {
		item,
		icon,
		title,
		subtitle,
		badges = [],
		fields = [],
		footer,
		rowActions,
		children,
		compact = false,
		class: className = '',
		onclick
	}: {
		item: T;
		icon: IconConfig | ((item: T) => IconConfig);
		title: (item: T) => string;
		subtitle?: (item: T) => string | null;
		badges?: (BadgeConfig | ((item: T) => BadgeConfig | null))[];
		fields?: FieldDefinition<T>[];
		footer?: FooterConfig<T>;
		rowActions?: Snippet<[{ item: T }]>;
		children?: Snippet;
		compact?: boolean;
		class?: string;
		onclick?: (item: T) => void;
	} = $props();

	const sizeClasses = $derived(
		compact
			? { iconContainer: 'size-10', image: 'size-5', icon: 'size-4', title: 'text-sm', subtitle: 'text-3xs' }
			: { iconContainer: 'size-11', image: 'size-6', icon: 'size-5', title: 'text-base', subtitle: 'text-xs' }
	);

	const resolvedIcon = $derived(typeof icon === 'function' ? icon(item) : icon);
	const resolvedBadges = $derived(
		badges.map((b) => (typeof b === 'function' ? b(item) : b)).filter((b): b is BadgeConfig => b !== null)
	);

	let errorImageUrl = $state<string | undefined>(undefined);
	const validImageUrl = $derived(
		resolvedIcon?.imageUrl && resolvedIcon.imageUrl !== errorImageUrl ? resolvedIcon.imageUrl : null
	);

	// Merged rows carry their environment; show it while several environments are shown.
	const environmentId = $derived(
		environmentStore.shown.length > 1 ? (item as { environmentId?: string }).environmentId : undefined
	);
	const environmentField = $derived<FieldDefinition<T>[]>(
		environmentId
			? [{ label: m.resource_environment_cap(), getValue: () => environmentId, type: 'component', component: EnvironmentValue }]
			: []
	);
	const visibleFields = $derived([...environmentField, ...fields.filter((f) => f.show !== false)]);
	const subtitleValue = $derived(subtitle?.(item));
	const hasSubtitle = $derived(!!subtitleValue);

	function getIconBgClass(variant: IconVariant): string {
		const map: Record<IconVariant, string> = {
			emerald: 'bg-success/8',
			red: 'bg-destructive/8',
			amber: 'bg-warning/8',
			blue: 'bg-info/8',
			purple: 'bg-purple/8',
			gray: 'bg-muted/40',
			sky: 'bg-sky/8',
			orange: 'bg-orange/8'
		};
		return map[variant];
	}

	function getIconTextClass(variant: IconVariant): string {
		const map: Record<IconVariant, string> = {
			emerald: 'text-success',
			red: 'text-destructive',
			amber: 'text-warning',
			blue: 'text-info',
			purple: 'text-purple',
			gray: 'text-muted-foreground',
			sky: 'text-sky',
			orange: 'text-orange'
		};
		return map[variant];
	}
</script>

{#snippet EnvironmentValue(value: string)}
	<EnvironmentCell {value} />
{/snippet}

{#snippet FieldValue(field: FieldDefinition<T>, value: any, isCompact: boolean)}
	{#if field.type === 'badge' && field.badgeVariant}
		<Badge variant={field.badgeVariant} size="sm" minWidth="20">{String(value)}</Badge>
	{:else if field.type === 'mono'}
		<span class={isCompact ? 'truncate font-mono text-xs leading-tight text-muted-foreground' : 'font-mono text-xs'}>{value}</span
		>
	{:else if field.type === 'component' && field.component}
		{#if isCompact}
			<span class="min-w-0 flex-1 text-xs leading-tight text-muted-foreground">
				{@render field.component(value)}
			</span>
		{:else}
			{@render field.component(value)}
		{/if}
	{:else if isCompact}
		<span class="min-w-0 flex-1 truncate text-xs leading-tight text-muted-foreground">
			{value}
		</span>
	{:else}
		{value}
	{/if}
{/snippet}

{#snippet cardHeader()}
	<div class={cn('flex gap-3', hasSubtitle ? 'items-start' : 'items-center')}>
		{#if resolvedIcon}
			{@const IconComponent = resolvedIcon.component}
			<div
				class={cn(
					'flex shrink-0 items-center justify-center rounded-xl ring-1 transition-transform duration-200 ring-inset group-hover:scale-105',
					sizeClasses.iconContainer,
					getIconBgClass(resolvedIcon.variant),
					'ring-white/5'
				)}
			>
				{#if validImageUrl}
					<img
						src={validImageUrl}
						alt={resolvedIcon.alt ?? ''}
						loading="lazy"
						decoding="async"
						referrerpolicy="no-referrer"
						class={cn('object-contain', sizeClasses.image)}
						onerror={() => (errorImageUrl = resolvedIcon?.imageUrl)}
					/>
				{:else}
					<IconComponent class={cn(getIconTextClass(resolvedIcon.variant), sizeClasses.icon)} />
				{/if}
			</div>
		{/if}
		<div class="min-w-0 flex-1">
			<h3 class={cn('line-clamp-2 leading-tight font-semibold wrap-break-word', sizeClasses.title)} title={title(item)}>
				{title(item)}
			</h3>
			{#if hasSubtitle}
				<p class={cn('mt-0.5 truncate font-mono text-muted-foreground', sizeClasses.subtitle)}>
					{subtitleValue}
				</p>
			{/if}
		</div>
		{#if rowActions}
			{@render rowActions({ item })}
		{/if}
	</div>
{/snippet}

{#snippet expandedField(field: FieldDefinition<T>)}
	{@const value = field.getValue(item)}
	{#if value !== null && value !== undefined}
		<div class="flex min-w-0 flex-1 basis-35 items-center gap-2.5">
			{#if field.icon}
				{@const FieldIcon = field.icon}
				<div
					class={cn(
						'flex size-7 shrink-0 items-center justify-center rounded-lg ring-1 ring-white/5 ring-inset',
						field.iconVariant ? getIconBgClass(field.iconVariant) : 'bg-muted/40'
					)}
				>
					<FieldIcon class={cn(field.iconVariant ? getIconTextClass(field.iconVariant) : 'text-muted-foreground', 'size-3.5')} />
				</div>
			{/if}
			<div class="min-w-0 flex-1">
				<div class="text-2xs font-medium tracking-wide text-muted-foreground uppercase">
					{field.label}
				</div>
				<div class="truncate text-sm leading-snug font-medium">
					{@render FieldValue(field, value, false)}
				</div>
			</div>
		</div>
	{/if}
{/snippet}

{#snippet additionalFields()}
	<!-- Additional Fields -->
	{#if visibleFields.length > 0}
		{#if !compact}
			<div class="flex flex-wrap gap-x-4 gap-y-2.5">
				{#each visibleFields as field (field.label)}
					{@render expandedField(field)}
				{/each}
			</div>
		{:else}
			{#each visibleFields as field (field.label)}
				{@const value = field.getValue(item)}
				{#if value !== null && value !== undefined}
					<div class="flex items-baseline gap-2">
						<span class="text-2xs font-semibold tracking-wide text-muted-foreground uppercase">{field.label}:</span>
						{@render FieldValue(field, value, true)}
					</div>
				{/if}
			{/each}
		{/if}
	{/if}
{/snippet}

<div class={cn('group relative w-full px-3 py-2', className)}>
	<Card.Root
		variant="subtle"
		size="sm"
		interactive={!!onclick}
		class="overflow-hidden text-left"
		onclick={onclick ? () => onclick(item) : undefined}
	>
		<Card.Content>
			<div class={compact ? 'flex flex-col gap-2.5 text-left' : 'flex flex-col gap-3 text-left'}>
				{@render cardHeader()}

				{#if resolvedBadges.length > 0}
					<div class="flex flex-wrap items-center gap-1">
						{#each resolvedBadges as badge (`${badge.variant}:${badge.text}`)}
							<Badge variant={badge.variant} size="sm" minWidth="20">{badge.text}</Badge>
						{/each}
					</div>
				{/if}

				{@render additionalFields()}

				<!-- Custom children content -->
				{#if children}
					{@render children()}
				{/if}
			</div>
		</Card.Content>

		{#if !compact && footer}
			{@const footerValue = footer.getValue(item)}
			{#if footerValue}
				{@const FooterIcon = footer.icon}
				<div class="flex items-center gap-2.5 border-t border-border/40 bg-muted/30 px-3.5 py-2.5">
					<FooterIcon class="size-4 text-muted-foreground" />
					<span class="text-2xs font-medium tracking-wide text-muted-foreground uppercase">
						{footer.label}
					</span>
					<span class="ml-auto font-mono text-xs font-medium text-foreground">
						{footerValue}
					</span>
				</div>
			{/if}
		{/if}
	</Card.Root>
</div>