Skip to content

Modern Docker Management, Designed for Everyone

4.1 KB
Raw Blame
<script lang="ts">
	import * as ArcaneTooltip from '#lib/components/arcane-tooltip/index.js';
	import { InfoIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import type { VulnerabilityRiskBand, VulnerabilityScoreStatus } from '#lib/types/environment.js';
	import { getRiskBandDisplay } from '#lib/utils/vulnerability.js';

	interface Props {
		/** 0-100; values outside the range are clamped. */
		score: number;
		band: VulnerabilityRiskBand;
		status: VulnerabilityScoreStatus;
		/** Tooltip body explaining the score. */
		help?: string;
		/** Maximum width in px; the gauge shrinks to fit narrower containers. */
		size?: number;
		stroke?: number;
	}

	let { score, band, status, help = m.vuln_risk_score_help(), size = 260, stroke = 14 }: Props = $props();

	let measuredWidth = $state(0);
	const unavailable = $derived(status === 'unavailable');
	const clampedScore = $derived(unavailable ? 0 : Math.max(0, Math.min(100, score)));
	const width = $derived(measuredWidth > 0 ? Math.min(measuredWidth, size) : size);
	const radius = $derived((width - stroke) / 2);
	const svgHeight = $derived(radius + stroke);
	const arcPath = $derived(
		`M ${stroke / 2} ${radius + stroke / 2} A ${radius} ${radius} 0 0 1 ${width - stroke / 2} ${radius + stroke / 2}`
	);

	const bandStyle = $derived(getRiskBandDisplay(band, status));
	const large = $derived(width >= 360);
</script>

<div
	class="relative h-(--gauge-height) w-full max-w-(--gauge-max-width)"
	style="--gauge-max-width: {size}px; --gauge-height: {svgHeight}px;"
	bind:clientWidth={measuredWidth}
>
	<svg {width} height={svgHeight} viewBox="0 0 {width} {svgHeight}" aria-hidden="true">
		<path d={arcPath} fill="none" stroke="currentColor" stroke-width={stroke} stroke-linecap="round" class="text-muted" />
		{#if clampedScore > 0}
			<path
				d={arcPath}
				fill="none"
				stroke="currentColor"
				stroke-width={stroke}
				stroke-linecap="round"
				pathLength="100"
				stroke-dasharray="{clampedScore} 100"
				class="gauge-fill transition-all duration-500 ease-out {bandStyle.strokeClass}"
			/>
		{/if}
	</svg>
	<div class="absolute inset-x-0 bottom-0 flex flex-col items-center">
		<span class="flex items-baseline" style="--gauge-font-size: {Math.round(width * 0.25)}px;">
			{#if unavailable}
				<span
					class="text-(length:--gauge-font-size) leading-none font-semibold tracking-tight text-muted-foreground"
					aria-label={m.vuln_priority_unavailable()}>–</span
				>
			{:else}
				<!-- The visible number counts up via a CSS counter; the sr-only text carries the value for assistive tech. -->
				<span
					class="gauge-number text-(length:--gauge-font-size) leading-none font-semibold tracking-tight tabular-nums"
					style="--gauge-score: {clampedScore};"
					aria-hidden="true"
				></span>
				<span class="sr-only">{clampedScore}</span>
				<span class="text-muted-foreground {large ? 'text-xl' : 'text-base'}">{m.vuln_score_denominator()}</span>
			{/if}
		</span>
		<span class="mt-2 flex items-center gap-1.5 text-muted-foreground {large ? 'text-base' : 'text-sm'}">
			{bandStyle.label}
			<ArcaneTooltip.Root>
				<ArcaneTooltip.Trigger>
					<span class="inline-flex cursor-help items-center" aria-label={m.vuln_risk_score()}>
						<InfoIcon class="size-4 shrink-0" />
					</span>
				</ArcaneTooltip.Trigger>
				<ArcaneTooltip.Content class="max-w-64">
					<p class="mb-1 text-sm font-medium">{m.vuln_risk_score()}</p>
					<p class="text-xs">{help}</p>
				</ArcaneTooltip.Content>
			</ArcaneTooltip.Root>
		</span>
	</div>
</div>

<style>
	@property --gauge-score {
		syntax: '<integer>';
		initial-value: 0;
		inherits: false;
	}

	.gauge-number {
		counter-reset: gauge-score var(--gauge-score);
		animation: gauge-count 1.1s cubic-bezier(0.22, 1, 0.36, 1) both;
	}

	.gauge-number::after {
		content: counter(gauge-score);
	}

	.gauge-fill {
		animation: gauge-fill 1.1s cubic-bezier(0.22, 1, 0.36, 1) both;
	}

	@keyframes gauge-count {
		from {
			--gauge-score: 0;
		}
	}

	@keyframes gauge-fill {
		from {
			stroke-dasharray: 0 100;
		}
	}

	@media (prefers-reduced-motion: reduce) {
		.gauge-number,
		.gauge-fill {
			animation: none;
		}
	}
</style>