mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-11-30 00:07:37 +00:00
501 lines
19 KiB
TypeScript
501 lines
19 KiB
TypeScript
import { useRef, useState, type ComponentType } from "react";
|
|
import { Menu, MenuButton, MenuItem, MenuItems } from "@headlessui/react";
|
|
import { useSWRConfig } from "swr";
|
|
import { PlusIcon } from "@heroicons/react/20/solid";
|
|
import { EllipsisVerticalIcon } from "@heroicons/react/20/solid";
|
|
import {
|
|
ArrowPathIcon,
|
|
ArrowsRightLeftIcon,
|
|
ClockIcon,
|
|
CodeBracketIcon,
|
|
MagnifyingGlassIcon,
|
|
PauseIcon,
|
|
RocketLaunchIcon,
|
|
ShieldCheckIcon,
|
|
WrenchIcon,
|
|
} from "@heroicons/react/24/outline";
|
|
import { FilterButton } from "../components/runs-list/filter-button";
|
|
import type { Automation, AutomationListResponse, AutomationScheduleTrigger } from "@qltysh/fabro-api-client";
|
|
import { Link, useNavigate } from "react-router";
|
|
import { ApiError, apiData, automationsApi } from "../lib/api-client";
|
|
import {
|
|
RUN_TARGET_CHECKOUT_LABEL,
|
|
UNSUPPORTED_TARGET_LABEL,
|
|
findScheduleTriggers,
|
|
gitTarget,
|
|
hasEnabledApiTrigger,
|
|
workflowSourceSummary,
|
|
} from "../lib/automation";
|
|
import { useAutomations } from "../lib/queries";
|
|
import { queryKeys } from "../lib/query-keys";
|
|
import { ConfirmDialog, PRIMARY_BUTTON_CLASS } from "../components/ui";
|
|
import { EmptyState, ErrorState, LoadingState } from "../components/state";
|
|
import { useToast } from "../components/toast";
|
|
|
|
export function meta({}: any) {
|
|
return [{ title: "Automations — Fabro" }];
|
|
}
|
|
|
|
export const handle = { hideHeader: true };
|
|
|
|
function CreateAutomationButton() {
|
|
return (
|
|
<Link
|
|
to="/automations/new"
|
|
className="inline-flex shrink-0 items-center gap-1.5 rounded-md border border-mint/20 px-3 py-2 text-sm font-medium text-mint transition-colors hover:border-mint/50 hover:bg-mint/10 hover:text-fg"
|
|
>
|
|
<PlusIcon className="size-3.5" aria-hidden="true" />
|
|
Create Automation
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
interface AutomationRow {
|
|
record: Automation;
|
|
id: string;
|
|
revision: string;
|
|
name: string;
|
|
workflow: string;
|
|
repository: string;
|
|
environmentId: string | null;
|
|
workflowSource?: string;
|
|
schedules: AutomationScheduleTrigger[];
|
|
apiEnabled: boolean;
|
|
icon: ComponentType<{ className?: string }>;
|
|
color: string;
|
|
}
|
|
|
|
const slugIconMap: Record<string, ComponentType<{ className?: string }>> = {
|
|
fix_build: WrenchIcon,
|
|
implement: CodeBracketIcon,
|
|
sync_drift: ArrowsRightLeftIcon,
|
|
expand: RocketLaunchIcon,
|
|
security_scan: ShieldCheckIcon,
|
|
dep_audit: ClockIcon,
|
|
};
|
|
|
|
const slugColorMap: Record<string, string> = {
|
|
fix_build: "var(--color-amber)",
|
|
implement: "var(--color-teal-500)",
|
|
sync_drift: "var(--color-mint)",
|
|
expand: "var(--color-coral)",
|
|
security_scan: "var(--color-teal-500)",
|
|
dep_audit: "var(--color-amber)",
|
|
};
|
|
|
|
const MENU_ITEM_CLASS =
|
|
"flex w-full items-center gap-2 px-3 py-2 text-left text-sm text-fg-3 transition-colors data-focus:bg-overlay data-focus:text-fg data-focus:outline-hidden disabled:cursor-not-allowed disabled:opacity-60";
|
|
|
|
const MENU_ITEM_DANGER_CLASS =
|
|
"flex w-full items-center gap-2 px-3 py-2 text-left text-sm text-coral transition-colors data-focus:bg-coral/10 data-focus:text-coral data-focus:outline-hidden disabled:cursor-not-allowed disabled:opacity-60";
|
|
|
|
function mapAutomations(result: AutomationListResponse | undefined): AutomationRow[] {
|
|
const automations = result?.data ?? [];
|
|
return automations.map((a) => {
|
|
const target = gitTarget(a.target);
|
|
return {
|
|
record: a,
|
|
id: a.id,
|
|
revision: a.revision,
|
|
name: a.name,
|
|
workflow: a.workflow,
|
|
repository: target?.repo ?? UNSUPPORTED_TARGET_LABEL,
|
|
environmentId: a.environment_id,
|
|
workflowSource: a.workflow_source
|
|
? workflowSourceSummary(a.workflow_source)
|
|
: undefined,
|
|
schedules: findScheduleTriggers(a),
|
|
apiEnabled: hasEnabledApiTrigger(a),
|
|
icon: slugIconMap[a.workflow] ?? CodeBracketIcon,
|
|
color: slugColorMap[a.workflow] ?? "var(--color-teal-500)",
|
|
};
|
|
});
|
|
}
|
|
|
|
function PlayIcon({ className }: { className?: string }) {
|
|
return (
|
|
<svg viewBox="0 0 24 24" fill="currentColor" className={className} aria-hidden="true">
|
|
<path fillRule="evenodd" d="M4.5 5.653c0-1.427 1.529-2.33 2.779-1.643l11.54 6.347c1.295.712 1.295 2.573 0 3.286L7.28 19.99c-1.25.687-2.779-.217-2.779-1.643V5.653Z" clipRule="evenodd" />
|
|
</svg>
|
|
);
|
|
}
|
|
|
|
function AutomationCard({
|
|
automation,
|
|
busy,
|
|
running,
|
|
savingScheduleId,
|
|
onToggleSchedule,
|
|
onRun,
|
|
onDelete,
|
|
}: {
|
|
automation: AutomationRow;
|
|
busy: boolean;
|
|
running: boolean;
|
|
savingScheduleId?: string;
|
|
onToggleSchedule: (schedule: AutomationScheduleTrigger) => void;
|
|
onRun: () => void;
|
|
onDelete: () => void;
|
|
}) {
|
|
const Icon = automation.icon;
|
|
const runDisabled = busy || running || !automation.apiEnabled || automation.environmentId === null;
|
|
return (
|
|
<div className="group flex items-center gap-4 rounded-md border border-line bg-panel/80 p-4 transition-all duration-200 hover:border-line-strong hover:bg-panel hover:shadow-lg hover:shadow-black/20">
|
|
<div className="min-w-0 flex-1">
|
|
<Link to={`/automations/${automation.id}`} className="flex min-w-0 items-center gap-4">
|
|
<div
|
|
className="flex size-9 shrink-0 items-center justify-center rounded-md border bg-panel-alt/60"
|
|
style={{ borderColor: `color-mix(in srgb, ${automation.color} 20%, transparent)`, color: automation.color }}
|
|
>
|
|
<Icon className="size-4" />
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<span className="text-sm font-medium text-fg-2 group-hover:text-fg">{automation.name}</span>
|
|
<span className="font-mono text-xs text-fg-muted">{automation.workflow}</span>
|
|
</div>
|
|
<p className="mt-1 text-xs text-fg-muted">
|
|
Run target · {automation.repository}
|
|
<span className={automation.environmentId ? "" : " text-coral"}>
|
|
{" · "}{automation.environmentId ?? "environment required"}
|
|
</span>
|
|
</p>
|
|
<p className="mt-0.5 truncate text-xs text-fg-muted">
|
|
Workflow source · {automation.workflowSource ?? RUN_TARGET_CHECKOUT_LABEL}
|
|
</p>
|
|
</div>
|
|
</Link>
|
|
{automation.schedules.length > 0 && (
|
|
<div className="mt-3 flex flex-wrap gap-2 sm:pl-13">
|
|
{automation.schedules.map((schedule) => {
|
|
const saving = savingScheduleId === schedule.id;
|
|
const action = schedule.enabled ? "Pause" : "Resume";
|
|
return (
|
|
<div key={schedule.id} className="inline-flex max-w-full flex-wrap items-center gap-2 rounded-md border border-line px-2 py-1 text-xs">
|
|
<span className={`inline-flex min-w-0 flex-wrap items-center gap-1.5 ${schedule.enabled ? "text-teal-300" : "text-fg-3"}`}>
|
|
<ClockIcon className="size-3 shrink-0" aria-hidden="true" />
|
|
{automation.schedules.length > 1 && <span className="break-all">{schedule.id} ·</span>}
|
|
<span>{schedule.expression}</span>
|
|
{!schedule.enabled && <span>· Paused</span>}
|
|
</span>
|
|
<button
|
|
type="button"
|
|
onClick={() => onToggleSchedule(schedule)}
|
|
disabled={busy || running || savingScheduleId !== undefined}
|
|
aria-label={`${action} schedule ${schedule.id} for ${automation.name}`}
|
|
aria-busy={saving}
|
|
title={`${action} schedule`}
|
|
className="inline-flex items-center gap-1 rounded px-1.5 py-1 text-fg-2 transition-colors hover:bg-overlay hover:text-fg focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-teal-500 disabled:cursor-not-allowed disabled:opacity-60"
|
|
>
|
|
{saving ? (
|
|
<ArrowPathIcon className="size-3.5 animate-spin [animation-duration:450ms] motion-reduce:animate-none" aria-hidden="true" />
|
|
) : schedule.enabled ? (
|
|
<PauseIcon className="size-3.5" aria-hidden="true" />
|
|
) : (
|
|
<PlayIcon className="size-3.5" />
|
|
)}
|
|
{saving ? "Saving…" : action}
|
|
</button>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{automation.schedules.length === 0 && (
|
|
<button
|
|
type="button"
|
|
onClick={onRun}
|
|
disabled={runDisabled}
|
|
aria-label={running ? "Starting run…" : "Run automation"}
|
|
title={
|
|
running
|
|
? "Starting run..."
|
|
: automation.apiEnabled
|
|
? automation.environmentId
|
|
? "Run automation"
|
|
: "Select an environment before running this automation"
|
|
: "Enable the API trigger to run it"
|
|
}
|
|
className="flex size-8 shrink-0 items-center justify-center rounded-full border border-mint/20 text-mint transition-colors hover:border-mint/50 hover:bg-mint/10 hover:text-fg disabled:cursor-not-allowed disabled:opacity-60 disabled:hover:bg-transparent disabled:hover:text-mint"
|
|
>
|
|
{running ? (
|
|
<ArrowPathIcon className="size-3.5 animate-spin [animation-duration:450ms]" aria-hidden="true" />
|
|
) : (
|
|
<PlayIcon className="size-3.5" />
|
|
)}
|
|
</button>
|
|
)}
|
|
|
|
<RowMenu automation={automation} disabled={busy} onDelete={onDelete} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function RowMenu({
|
|
automation,
|
|
disabled,
|
|
onDelete,
|
|
}: {
|
|
automation: AutomationRow;
|
|
disabled: boolean;
|
|
onDelete: () => void;
|
|
}) {
|
|
return (
|
|
<Menu as="div" className="relative inline-block">
|
|
<MenuButton
|
|
type="button"
|
|
disabled={disabled}
|
|
aria-label={`Actions for ${automation.name}`}
|
|
title="Actions"
|
|
className="flex size-8 shrink-0 items-center justify-center rounded-md text-fg-muted transition-colors hover:bg-overlay hover:text-fg-3 disabled:cursor-not-allowed disabled:opacity-60"
|
|
>
|
|
<EllipsisVerticalIcon className="size-5" aria-hidden="true" />
|
|
</MenuButton>
|
|
<MenuItems
|
|
transition
|
|
anchor={{ to: "bottom end", gap: 4 }}
|
|
className="z-30 w-36 origin-top-right rounded-md bg-panel py-1 outline-1 -outline-offset-1 outline-line-strong transition data-closed:scale-95 data-closed:opacity-0 data-enter:duration-100 data-enter:ease-out data-leave:duration-75 data-leave:ease-in"
|
|
>
|
|
<MenuItem>
|
|
<Link
|
|
to={`/automations/${encodeURIComponent(automation.id)}/edit`}
|
|
className={MENU_ITEM_CLASS}
|
|
>
|
|
Edit
|
|
</Link>
|
|
</MenuItem>
|
|
<hr className="my-1 h-px border-0 bg-line" />
|
|
<MenuItem>
|
|
<button
|
|
type="button"
|
|
onClick={onDelete}
|
|
disabled={disabled}
|
|
className={MENU_ITEM_DANGER_CLASS}
|
|
>
|
|
Delete
|
|
</button>
|
|
</MenuItem>
|
|
</MenuItems>
|
|
</Menu>
|
|
);
|
|
}
|
|
|
|
type TriggerFilter = "all" | "scheduled" | "manual";
|
|
|
|
const TRIGGER_FILTER_OPTIONS: { value: TriggerFilter; label: string }[] = [
|
|
{ value: "all", label: "All triggers" },
|
|
{ value: "scheduled", label: "Scheduled" },
|
|
{ value: "manual", label: "Manual" },
|
|
];
|
|
|
|
export default function Automations() {
|
|
const { mutate } = useSWRConfig();
|
|
const toast = useToast();
|
|
const navigate = useNavigate();
|
|
const automationsQuery = useAutomations();
|
|
const automations = mapAutomations(automationsQuery.data);
|
|
const [query, setQuery] = useState("");
|
|
const [triggerFilter, setTriggerFilter] = useState<TriggerFilter>("all");
|
|
const [pendingDelete, setPendingDelete] = useState<AutomationRow | null>(null);
|
|
const [deleting, setDeleting] = useState(false);
|
|
const [runningId, setRunningId] = useState<string | null>(null);
|
|
const [savingSchedules, setSavingSchedules] = useState(new Map<string, string>());
|
|
const pendingScheduleUpdates = useRef(new Set<string>());
|
|
|
|
async function toggleSchedule(automation: AutomationRow, schedule: AutomationScheduleTrigger) {
|
|
if (pendingScheduleUpdates.current.has(automation.id)) return;
|
|
pendingScheduleUpdates.current.add(automation.id);
|
|
setSavingSchedules((pending) => new Map(pending).set(automation.id, schedule.id));
|
|
const enabled = !schedule.enabled;
|
|
const record = automation.record;
|
|
try {
|
|
const saved = await apiData(() => automationsApi.replaceAutomation(record.id, record.revision, {
|
|
name: record.name,
|
|
description: record.description,
|
|
environment_id: record.environment_id ?? "",
|
|
target: record.target,
|
|
workflow: record.workflow,
|
|
workflow_source: record.workflow_source,
|
|
triggers: record.triggers.map((trigger) =>
|
|
trigger.type === "schedule" && trigger.id === schedule.id
|
|
? { ...trigger, enabled }
|
|
: trigger,
|
|
),
|
|
}));
|
|
await mutate<AutomationListResponse>(queryKeys.automations.list(), (current = automationsQuery.data) => current && ({
|
|
...current,
|
|
data: current.data.map((item) => item.id === saved.id ? saved : item),
|
|
}), { revalidate: false });
|
|
await mutate(queryKeys.automations.detail(saved.id), saved, { revalidate: false });
|
|
toast.push({ message: `Schedule “${schedule.id}” ${enabled ? "resumed" : "paused"} for “${record.name}”.` });
|
|
} catch (cause) {
|
|
const conflict = cause instanceof ApiError && cause.status === 409;
|
|
toast.push({
|
|
tone: "error",
|
|
message: conflict
|
|
? "This automation changed since it was loaded. Reloading the latest settings; please try again."
|
|
: cause instanceof ApiError && cause.message
|
|
? cause.message
|
|
: `Couldn't ${enabled ? "resume" : "pause"} the schedule. Please try again.`,
|
|
});
|
|
if (conflict) {
|
|
await Promise.allSettled([
|
|
mutate(queryKeys.automations.list()),
|
|
mutate(queryKeys.automations.detail(record.id)),
|
|
]);
|
|
}
|
|
} finally {
|
|
pendingScheduleUpdates.current.delete(automation.id);
|
|
setSavingSchedules((pending) => {
|
|
const next = new Map(pending);
|
|
next.delete(automation.id);
|
|
return next;
|
|
});
|
|
}
|
|
}
|
|
|
|
async function runAutomation(automation: AutomationRow) {
|
|
if (runningId) return;
|
|
setRunningId(automation.id);
|
|
try {
|
|
const run = await apiData(() => automationsApi.createAutomationRun(automation.id));
|
|
toast.push({ message: `Started run for “${automation.name}”.` });
|
|
navigate(`/runs/${run.id}`);
|
|
} catch (cause) {
|
|
toast.push({
|
|
tone: "error",
|
|
message:
|
|
cause instanceof ApiError && cause.message
|
|
? cause.message
|
|
: "Couldn't start a run. Please try again.",
|
|
});
|
|
setRunningId(null);
|
|
}
|
|
}
|
|
|
|
const lowerQuery = query.toLowerCase();
|
|
const filtered = automations.filter(
|
|
(a) =>
|
|
(triggerFilter === "all" ||
|
|
(triggerFilter === "scheduled" && a.schedules.length > 0) ||
|
|
(triggerFilter === "manual" && a.schedules.length === 0)) &&
|
|
(a.name.toLowerCase().includes(lowerQuery) ||
|
|
a.workflow.toLowerCase().includes(lowerQuery) ||
|
|
a.repository.toLowerCase().includes(lowerQuery) ||
|
|
a.workflowSource?.toLowerCase().includes(lowerQuery)),
|
|
);
|
|
|
|
async function confirmDelete() {
|
|
if (!pendingDelete) return;
|
|
const { id, revision, name } = pendingDelete;
|
|
setDeleting(true);
|
|
try {
|
|
await apiData(() => automationsApi.deleteAutomation(id, revision));
|
|
await mutate(queryKeys.automations.list());
|
|
toast.push({ message: `Automation “${name}” deleted.` });
|
|
setPendingDelete(null);
|
|
} catch (cause) {
|
|
toast.push({
|
|
tone: "error",
|
|
message:
|
|
cause instanceof ApiError && cause.message
|
|
? cause.message
|
|
: "Couldn't delete the automation. Please try again.",
|
|
});
|
|
} finally {
|
|
setDeleting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
{automationsQuery.isLoading ? (
|
|
<LoadingState label="Loading automations…" />
|
|
) : automationsQuery.error ? (
|
|
<ErrorState
|
|
title="Couldn't load automations"
|
|
description="Something went wrong while loading your automations."
|
|
onRetry={() => mutate(queryKeys.automations.list())}
|
|
/>
|
|
) : automations.length === 0 ? (
|
|
<EmptyState
|
|
icon={RocketLaunchIcon}
|
|
title="Create your first automation"
|
|
description="Automations run a workflow on a schedule or on demand, so recurring work happens without you kicking it off by hand."
|
|
action={
|
|
<Link to="/automations/new" className={PRIMARY_BUTTON_CLASS}>
|
|
<PlusIcon className="size-4" aria-hidden="true" />
|
|
Create Automation
|
|
</Link>
|
|
}
|
|
/>
|
|
) : (
|
|
<>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<div className="relative w-64">
|
|
<MagnifyingGlassIcon className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-fg-muted" />
|
|
<input
|
|
type="text"
|
|
aria-label="Search automations"
|
|
placeholder="Search automations…"
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
className="w-full rounded-md border border-line bg-panel/80 py-2 pl-9 pr-3 text-sm text-fg-2 placeholder-fg-muted outline-none transition-colors focus:border-focus focus:ring-0"
|
|
/>
|
|
</div>
|
|
<FilterButton<TriggerFilter>
|
|
label="Trigger"
|
|
value={triggerFilter}
|
|
allValue="all"
|
|
options={TRIGGER_FILTER_OPTIONS}
|
|
onChange={(next) => setTriggerFilter(next)}
|
|
/>
|
|
<div className="ml-auto">
|
|
<CreateAutomationButton />
|
|
</div>
|
|
</div>
|
|
<div className="space-y-3">
|
|
{filtered.map((automation) => (
|
|
<AutomationCard
|
|
key={automation.id}
|
|
automation={automation}
|
|
busy={deleting || savingSchedules.has(automation.id) || (runningId !== null && runningId !== automation.id)}
|
|
running={runningId === automation.id}
|
|
savingScheduleId={savingSchedules.get(automation.id)}
|
|
onToggleSchedule={(schedule) => toggleSchedule(automation, schedule)}
|
|
onRun={() => runAutomation(automation)}
|
|
onDelete={() => setPendingDelete(automation)}
|
|
/>
|
|
))}
|
|
{filtered.length === 0 && (
|
|
<p className="py-8 text-center text-sm text-fg-muted">
|
|
No automations match your filters.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</>
|
|
)}
|
|
<ConfirmDialog
|
|
open={pendingDelete !== null}
|
|
title="Delete automation"
|
|
description={
|
|
<>
|
|
Delete{" "}
|
|
<span className="font-mono text-fg-2">{pendingDelete?.name}</span>? This
|
|
removes the automation and stops any scheduled triggers. Existing runs are not affected.
|
|
</>
|
|
}
|
|
confirmLabel="Delete"
|
|
pendingLabel="Deleting…"
|
|
pending={deleting}
|
|
onConfirm={confirmDelete}
|
|
onCancel={() => {
|
|
if (!deleting) setPendingDelete(null);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|