forked from getarcaneapp/arcane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob-card.svelte
More file actions
139 lines (128 loc) · 4.16 KB
/
job-card.svelte
File metadata and controls
139 lines (128 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script lang="ts">
import { StartIcon, EditIcon, ClockIcon } from '$lib/icons';
import { m } from '$lib/paraglide/messages';
import { Badge } from '$lib/components/ui/badge';
import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card';
import { Spinner } from '$lib/components/ui/spinner';
import { jobScheduleService } from '$lib/services/job-schedule-service';
import { formatDistanceToNow } from 'date-fns';
import type { Snippet } from 'svelte';
import type { JobStatus } from '$lib/types/job-schedule.type';
import JobScheduleDialog from './job-schedule-dialog.svelte';
import { createMutation } from '@tanstack/svelte-query';
let {
job,
environmentId = '0',
isAgent = false,
onScheduleUpdate,
children,
enabledOverride,
headerAccessory
}: {
job: JobStatus;
environmentId?: string;
isAgent?: boolean;
onScheduleUpdate?: () => void;
children?: Snippet;
enabledOverride?: boolean;
headerAccessory?: Snippet;
} = $props();
let showScheduleDialog = $state(false);
const runJobMutation = createMutation(() => ({
mutationFn: () => jobScheduleService.runJob(job.id, environmentId)
}));
const isRunning = $derived(runJobMutation.isPending);
const nextRunText = $derived.by(() => {
if (!job.nextRun) return null;
const nextRunDate = new Date(job.nextRun);
const relative = formatDistanceToNow(nextRunDate, { addSuffix: true });
const absolute = nextRunDate.toLocaleString();
return `${relative} (${absolute})`;
});
const isEnabled = $derived.by(() => enabledOverride ?? job.enabled);
const canRun = $derived(isEnabled && job.canRunManually && !isRunning && !(isAgent && job.managerOnly));
function runJobNow() {
if (!canRun) return;
runJobMutation.mutate();
}
function openScheduleDialog() {
showScheduleDialog = true;
}
function handleScheduleUpdated() {
showScheduleDialog = false;
onScheduleUpdate?.();
}
</script>
<Card.Root class="bg-background/60 h-full rounded-xl border border-white/10 shadow-none backdrop-blur-md">
<Card.Header class="flex flex-row items-center justify-between space-y-0 px-4 py-4">
<div class="flex flex-col gap-1.5">
<div class="flex items-center gap-2">
<Card.Title class="text-sm leading-none font-medium">{job.name}</Card.Title>
{#if job.isContinuous}
<Badge variant="outline" class="h-4.5 border-white/10 px-1.5 text-[0.6rem] font-medium">{m.jobs_continuous()}</Badge>
{/if}
</div>
<Card.Description class="text-muted-foreground/80 line-clamp-1 text-xs">{job.description}</Card.Description>
</div>
<div class="flex items-center gap-2">
{#if headerAccessory}
{@render headerAccessory()}
{/if}
{#if isEnabled && !job.isContinuous && job.settingsKey}
<Button
variant="ghost"
size="icon"
onclick={openScheduleDialog}
disabled={isAgent && job.managerOnly}
class="size-7 opacity-75 hover:bg-white/5 hover:opacity-100"
>
<EditIcon class="size-3.5" />
</Button>
{/if}
{#if isEnabled && job.canRunManually}
<Button
variant="outline"
size="sm"
onclick={runJobNow}
disabled={!canRun}
class="h-7 border-white/10 bg-transparent px-2.5 text-xs font-medium shadow-none hover:bg-white/5"
>
{#if isRunning}
<Spinner class="mr-1.5 size-3" />
{m.common_running()}
{:else}
<StartIcon class="mr-1.5 size-3.5" />
{m.jobs_run_now()}
{/if}
</Button>
{/if}
</div>
</Card.Header>
<Card.Content class="space-y-2 px-4 pt-0 pb-4">
{#if isEnabled}
<div class="text-muted-foreground grid gap-2 text-xs">
{#if !job.isContinuous}
<div class="flex items-center gap-2">
<ClockIcon class="h-3.5 w-3.5" />
<span>{m.jobs_schedule()}:</span>
<code class="bg-muted rounded px-2 py-0.5 text-[0.65rem]">{job.schedule}</code>
</div>
{#if nextRunText}
<div>
{m.jobs_next_run()}: {nextRunText}
</div>
{/if}
{:else}
<div>{m.jobs_continuous()}</div>
{/if}
</div>
{/if}
{#if children}
{@render children()}
{/if}
</Card.Content>
</Card.Root>
{#if showScheduleDialog}
<JobScheduleDialog {job} {environmentId} bind:open={showScheduleDialog} onUpdate={handleScheduleUpdated} />
{/if}