Skip to content

The only tool you need to know what is happening and how to fix it.

7.5 KB
Raw Blame
<script lang="ts">
	import { resolve } from '$app/paths';
	import { goto } from '$app/navigation';
	import { Button } from '$lib/components/ui/button';
	import * as Card from '$lib/components/ui/card';
	import * as Tabs from '$lib/components/ui/tabs';
	import { ArrowRight } from '@lucide/svelte';
	import type { ExceptionOccurrence, LinkedTrace, SessionRecording } from '$lib/types/exceptions';
	import { formatDuration, getStatusColor, formatDateTime } from '$lib/utils/formatters';
	import { getTimezone } from '$lib/state/timezone.svelte';
	import { AttributesGrid } from '$lib/components/ui/attributes-grid';
	import { LabelValue } from '../ui/label-value';
	import { projectsState, isFrontendFramework } from '$lib/state/projects.svelte';
	import SessionReplay from './session-replay.svelte';
	import SessionLogsTable from './session-logs-table.svelte';
	import SessionActionsTable from './session-actions-table.svelte';
	import DistributedTraceCard from '$lib/components/distributed-trace/distributed-trace-card.svelte';

	interface Props {
		occurrence: ExceptionOccurrence;
		linkedTrace: LinkedTrace | null;
		sessionRecording?: SessionRecording | null;
		sessionId?: string | null;
		title?: string;
		description?: string;
		timezone?: string;
	}

	let {
		occurrence,
		linkedTrace,
		sessionRecording = null,
		sessionId = null,
		title = 'Event',
		description = 'Details for this specific occurrence',
		timezone
	}: Props = $props();

	const RELATED_LABELS = {
		endpoint: { label: 'Endpoint', route: 'endpoints' },
		task: { label: 'Task', route: 'tasks' },
		ai_trace: { label: 'AI Trace', route: 'ai-traces' }
	} as const;

	const tz = $derived(timezone ?? getTimezone());
	const isFrontend = $derived(
		projectsState.currentProject?.framework
			? isFrontendFramework(projectsState.currentProject.framework)
			: false
	);

	const recordingEvents = $derived(sessionRecording?.events ?? null);
	const hasReplay = $derived(!!recordingEvents && recordingEvents.length > 0);
	const logs = $derived(sessionRecording?.logs ?? []);
	const actions = $derived(sessionRecording?.actions ?? []);
	const hasLogs = $derived(logs.length > 0);
	const hasActions = $derived(actions.length > 0);
	// Logs/actions tables are only meaningful for frontend-framework projects.
	// Backend / OTel exception pages must look identical to before.
	const showSessionContext = $derived(isFrontend && (hasLogs || hasActions));
	const defaultTab = $derived(hasLogs ? 'logs' : 'actions');

	let currentTimeMs = $state(0);
	let replayRef = $state<{ seek: (ms: number) => void } | null>(null);

	function handleSeek(offsetMs: number) {
		replayRef?.seek(offsetMs);
	}
</script>

<!-- Session Replay -->
{#if hasReplay}
	<Card.Root class="pb-0">
		<Card.Header class="flex flex-row items-center justify-between gap-2">
			<Card.Title>Session Replay</Card.Title>
			{#if sessionId}
				<Button variant="outline" size="sm" onclick={() => goto(resolve(`/sessions/${sessionId}`))}>
					View full session
					<ArrowRight class="size-4" />
				</Button>
			{/if}
		</Card.Header>
		<Card.Content class="p-0">
			{#key recordingEvents}
				<SessionReplay
					bind:this={replayRef}
					events={recordingEvents}
					onTimeUpdate={(ms) => (currentTimeMs = ms)}
				/>
			{/key}
		</Card.Content>
	</Card.Root>
{:else if sessionId}
	<Card.Root>
		<Card.Header class="flex flex-row items-center justify-between gap-2">
			<Card.Title>Session</Card.Title>
			<Button variant="outline" size="sm" onclick={() => goto(resolve(`/sessions/${sessionId}`))}>
				View full session
				<ArrowRight class="size-4" />
			</Button>
		</Card.Header>
		<Card.Content class="text-sm text-muted-foreground">
			This exception is linked to a recorded user session.
		</Card.Content>
	</Card.Root>
{/if}

<!-- Session Context (logs + actions). Frontend projects only. -->
{#if showSessionContext}
	<Card.Root>
		<Card.Header>
			<Card.Title>Session Context</Card.Title>
			<Card.Description>
				The last seconds of console output and recorded actions before this exception. Times are
				offsets from the start of the recording.
			</Card.Description>
		</Card.Header>
		<Card.Content>
			<Tabs.Root value={defaultTab}>
				<Tabs.List class="mb-4">
					{#if hasLogs}
						<Tabs.Trigger value="logs">Logs ({logs.length})</Tabs.Trigger>
					{/if}
					{#if hasActions}
						<Tabs.Trigger value="actions">Actions ({actions.length})</Tabs.Trigger>
					{/if}
				</Tabs.List>
				{#if hasLogs}
					<Tabs.Content value="logs">
						<SessionLogsTable
							{logs}
							startedAt={sessionRecording?.startedAt}
							{currentTimeMs}
							onSeek={handleSeek}
						/>
					</Tabs.Content>
				{/if}
				{#if hasActions}
					<Tabs.Content value="actions">
						<SessionActionsTable
							{actions}
							startedAt={sessionRecording?.startedAt}
							{currentTimeMs}
							onSeek={handleSeek}
						/>
					</Tabs.Content>
				{/if}
			</Tabs.Root>
		</Card.Content>
	</Card.Root>
{/if}

<Card.Root>
	<Card.Header>
		<Card.Title>{title}</Card.Title>
		<Card.Description>{description}</Card.Description>
	</Card.Header>
	<Card.Content class="space-y-6">
		<!-- Event Details -->
		<div class="grid grid-cols-2 gap-4 {isFrontend ? 'md:grid-cols-2' : 'md:grid-cols-4'}">
			<div class="space-y-1">
				<p class="text-sm text-muted-foreground">Recorded At</p>
				<p class="font-mono text-sm">{formatDateTime(occurrence.recordedAt, { timezone: tz })}</p>
			</div>
			{#if !isFrontend}
				<div class="space-y-1">
					<p class="text-sm text-muted-foreground">Server</p>
					<p class="font-mono text-sm">{occurrence.serverName || '-'}</p>
				</div>
			{/if}
			<div class="space-y-1">
				<p class="text-sm text-muted-foreground">Version</p>
				<p class="font-mono text-sm">{occurrence.appVersion || '-'}</p>
			</div>
			{#if !isFrontend}
				<div class="space-y-1">
					<p class="text-sm text-muted-foreground">Trace</p>
					<p class="font-mono text-sm">{occurrence.traceId || '-'}</p>
				</div>
			{/if}
		</div>

		<!-- AttributesGrid -->
		{#if occurrence.attributes && Object.keys(occurrence.attributes).length > 0}
			<hr class="border-border" />
			<div>
				<p class="mb-3 text-sm font-medium">Attributes</p>
				<AttributesGrid attributes={occurrence.attributes} />
			</div>
		{/if}

		<!-- Related Trace -->
		{#if linkedTrace}
			{@const related = RELATED_LABELS[linkedTrace.traceType]}
			<hr class="border-border" />
			<div>
				<p class="mb-3 text-sm font-medium">Related {related.label}</p>
				<div class="mb-4 grid grid-cols-2 gap-4 md:grid-cols-4">
					<LabelValue label={related.label} value={linkedTrace.endpoint} mono />
					{#if linkedTrace.traceType === 'endpoint'}
						<LabelValue
							label="Status"
							value={linkedTrace.statusCode}
							mono
							large
							valueClass={getStatusColor(linkedTrace.statusCode)}
						/>
					{/if}
					<LabelValue label="Duration" value={formatDuration(linkedTrace.duration)} mono large />
					<LabelValue
						label="Recorded At"
						value={formatDateTime(linkedTrace.recordedAt, { timezone })}
						mono
					/>
				</div>
				<Button
					variant="outline"
					size="sm"
					onclick={() =>
						goto(
							resolve(
								`/${related.route}/${encodeURIComponent(linkedTrace.endpoint)}/${linkedTrace.id}?preset=24h&t=${encodeURIComponent(linkedTrace.recordedAt)}`
							)
						)}
				>
					View {related.label} Details
					<ArrowRight class="ml-2 h-4 w-4" />
				</Button>
			</div>
		{/if}
	</Card.Content>
</Card.Root>

{#if occurrence.traceId}
	<DistributedTraceCard
		traceId={occurrence.traceId}
		currentExceptionHash={occurrence.exceptionHash}
		recordedAt={linkedTrace?.recordedAt ?? occurrence.recordedAt}
	/>
{/if}