Skip to content

Modern Docker Management, Designed for Everyone

6.5 KB
Raw Blame
<script lang="ts">
	import type { Snippet } from 'svelte';

	import * as ArcaneTooltip from '#lib/components/arcane-tooltip/index.js';
	import { Badge } from '#lib/components/ui/badge/index.js';
	import { Button } from '#lib/components/ui/button/index.js';
	import * as DropdownMenu from '#lib/components/ui/dropdown-menu/index.js';
	import { Spinner } from '#lib/components/ui/spinner/index.js';
	import { useJobActions } from '#lib/hooks/use-job-actions.svelte.js';
	import { AlertTriangleIcon, EditIcon, EllipsisIcon, RestartIcon, StartIcon, ClockIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import type { JobStatus } from '#lib/types/settings.js';
	import { formatDateTimeShort, formatRelativeTime } from '#lib/utils/formatting.js';

	import JobScheduleDialog from './job-schedule-dialog.svelte';
	import { jobStatusLabel, jobNameLabel, jobStatusTone } from './job-status';

	let {
		job,
		environmentId = '0',
		isAgent = false,
		durableRuns = false,
		enabledOverride,
		headerAccessory,
		onSelect,
		onScheduleUpdate
	}: {
		job: JobStatus;
		environmentId?: string;
		isAgent?: boolean;
		durableRuns?: boolean;
		enabledOverride?: boolean;
		headerAccessory?: Snippet;
		onSelect?: (job: JobStatus, section: 'overview' | 'history') => void;
		onScheduleUpdate?: () => void;
	} = $props();

	let showScheduleDialog = $state(false);
	const jobActions = useJobActions(() => ({ job, environmentId, isAgent, durableRuns, enabledOverride, onScheduleUpdate }));

	const hasError = $derived(!!job.lastError || !!job.workerHealth?.lastError);
	const isActive = $derived(['running', 'retrying', 'queued'].includes(jobActions.run?.status ?? ''));

	const statusBadge = $derived.by(() => {
		if (!jobActions.isEnabled) return { tone: 'gray' as const, label: m.common_disabled() };
		if (jobActions.run) return { tone: jobStatusTone(jobActions.run.status), label: jobStatusLabel(jobActions.run.status) };
		if (job.isContinuous) return { tone: jobStatusTone(job.workerHealth?.status), label: m.jobs_continuous() };
		return null;
	});
	const lastRunAt = $derived(jobActions.run?.finishedAt ?? jobActions.run?.updatedAt ?? job.lastSuccess);
	const showNextRun = $derived(jobActions.isEnabled && !!job.nextRun && (!job.isContinuous || !!job.settingsKey));

	function select(section: 'overview' | 'history' = 'overview') {
		onSelect?.(job, section);
	}

	// Controls inside the row opt out of row selection via data-row-control.
	function handleRowClick(event: MouseEvent) {
		if ((event.target as HTMLElement | null)?.closest('[data-row-control]')) return;
		select();
	}

	function handleRowKeydown(event: KeyboardEvent) {
		if (event.target !== event.currentTarget) return;
		if (event.key !== 'Enter' && event.key !== ' ') return;
		event.preventDefault();
		select();
	}
</script>

<div
	role="button"
	tabindex="0"
	class="group flex min-w-0 cursor-pointer items-center gap-3 px-4 py-3 transition-colors outline-none hover:bg-muted/40 focus-visible:bg-muted/40 sm:gap-4"
	onclick={handleRowClick}
	onkeydown={handleRowKeydown}
>
	{#if headerAccessory}
		<div class="flex shrink-0 items-center" data-row-control>
			{@render headerAccessory()}
		</div>
	{/if}

	<div class="flex min-w-0 flex-1 flex-col gap-0.5">
		<div class="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1">
			<span class="truncate text-sm font-medium">{jobNameLabel(job)}</span>
			{#if statusBadge}
				<Badge variant={statusBadge.tone} size="sm">
					{#if isActive}<Spinner class="size-2.5" />{/if}
					{statusBadge.label}
				</Badge>
			{/if}
			{#if hasError}
				<ArcaneTooltip.Root>
					<ArcaneTooltip.Trigger>
						<AlertTriangleIcon class="size-3.5 text-destructive" aria-label={m.jobs_last_error()} />
					</ArcaneTooltip.Trigger>
					<ArcaneTooltip.Content class="max-w-80 break-words"
						>{job.lastError ?? job.workerHealth?.lastError}</ArcaneTooltip.Content
					>
				</ArcaneTooltip.Root>
			{/if}
			{#if job.children?.length}
				<Badge variant="gray" size="sm">{m.jobs_target_runs({ count: job.children.length })}</Badge>
			{/if}
		</div>
		{#if jobActions.description}
			<p class="truncate text-xs text-muted-foreground">{jobActions.description}</p>
		{/if}
	</div>

	<div class="hidden shrink-0 flex-col items-end gap-0.5 text-xs text-muted-foreground tabular-nums md:flex">
		{#if lastRunAt}
			<span title={formatDateTimeShort(lastRunAt)}>{m.jobs_last_run()}: {formatRelativeTime(lastRunAt)}</span>
		{:else if jobActions.isEnabled}
			<span>{m.jobs_never_run()}</span>
		{/if}
		{#if showNextRun}
			<span class="flex items-center gap-1" title={formatDateTimeShort(job.nextRun)}>
				<ClockIcon class="size-3" />
				{m.jobs_next_run()}: {formatRelativeTime(job.nextRun)}
			</span>
		{/if}
	</div>

	<div class="flex shrink-0 items-center gap-1" data-row-control>
		{#if jobActions.isEnabled && job.canRunManually}
			<Button variant="outline" size="sm" onclick={() => jobActions.runMutation.mutate(job.id)} disabled={!jobActions.canRun}>
				{#if jobActions.runMutation.isPending}<Spinner data-icon="inline-start" />{:else}<StartIcon
						data-icon="inline-start"
					/>{/if}
				<span class="hidden sm:inline">{job.children?.length ? m.jobs_run_all() : m.jobs_run_now()}</span>
			</Button>
		{/if}
		<DropdownMenu.Root>
			<DropdownMenu.Trigger>
				{#snippet child({ props })}
					<Button {...props} variant="ghost" size="icon" class="size-8" aria-label={m.common_actions()}>
						<EllipsisIcon class="size-4" />
					</Button>
				{/snippet}
			</DropdownMenu.Trigger>
			<DropdownMenu.Content align="end" class="min-w-44">
				<DropdownMenu.Item onclick={() => select('overview')}>{m.common_view_details()}</DropdownMenu.Item>
				{#if durableRuns}
					<DropdownMenu.Item onclick={() => select('history')}>{m.jobs_run_history()}</DropdownMenu.Item>
				{/if}
				{#if jobActions.canEditSchedule || jobActions.canRestartWorker}
					<DropdownMenu.Separator />
				{/if}
				{#if jobActions.canEditSchedule}
					<DropdownMenu.Item onclick={() => (showScheduleDialog = true)}>
						<EditIcon class="size-4" />
						{m.jobs_edit_schedule()}
					</DropdownMenu.Item>
				{/if}
				{#if jobActions.canRestartWorker}
					<DropdownMenu.Item disabled={jobActions.restartMutation.isPending} onclick={() => jobActions.restartMutation.mutate()}>
						<RestartIcon class="size-4" />
						{m.jobs_restart_worker()}
					</DropdownMenu.Item>
				{/if}
			</DropdownMenu.Content>
		</DropdownMenu.Root>
	</div>
</div>

{#if showScheduleDialog}
	<JobScheduleDialog
		{job}
		{environmentId}
		bind:open={showScheduleDialog}
		onUpdate={() => {
			showScheduleDialog = false;
			onScheduleUpdate?.();
		}}
	/>
{/if}