feat: redesign workflow list with enriched data
- API returns node counts, total runs, and last run status per workflow - List view with status icon, trigger badges, stats, and last run info - Human-readable schedule labels (Every 6h, Every 30m) - Last run shows status icon, asset count, and relative time - Actions appear on hover for cleaner default state - Improved empty state
This commit is contained in:
1 parent
d0a7894a25
commit
6dff749479
2 files changed
+184
-63
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { appDb } from "@/db";
|
||||
import { workflows } from "@/db/schema/workflows.schema";
|
||||
import { workflows, workflowNodes, workflowRuns } from "@/db/schema/workflows.schema";
|
||||
import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
import { eq, desc, count, sql } from "drizzle-orm";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
@@ -15,7 +15,37 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
.from(workflows)
|
||||
.where(eq(workflows.ownerId, currentUser.id))
|
||||
.orderBy(desc(workflows.updatedAt));
|
||||
return res.status(200).json(rows);
|
||||
|
||||
// Enrich with node counts and last run info
|
||||
const enriched = await Promise.all(rows.map(async (w) => {
|
||||
const nodes = await appDb
|
||||
.select({ type: workflowNodes.type })
|
||||
.from(workflowNodes)
|
||||
.where(eq(workflowNodes.workflowId, w.id));
|
||||
|
||||
const [lastRun] = await appDb
|
||||
.select()
|
||||
.from(workflowRuns)
|
||||
.where(eq(workflowRuns.workflowId, w.id))
|
||||
.orderBy(desc(workflowRuns.startedAt))
|
||||
.limit(1);
|
||||
|
||||
const [runCountRow] = await appDb
|
||||
.select({ count: count() })
|
||||
.from(workflowRuns)
|
||||
.where(eq(workflowRuns.workflowId, w.id));
|
||||
|
||||
return {
|
||||
...w,
|
||||
nodeCount: nodes.length,
|
||||
triggerCount: nodes.filter(n => n.type === "trigger").length,
|
||||
actionCount: nodes.filter(n => n.type === "action").length,
|
||||
totalRuns: runCountRow?.count || 0,
|
||||
lastRun: lastRun || null,
|
||||
};
|
||||
}));
|
||||
|
||||
return res.status(200).json(enriched);
|
||||
}
|
||||
|
||||
if (req.method === "POST") {
|
||||
|
||||
+151
-60
@@ -1,21 +1,74 @@
|
||||
import PageLayout from "@/components/layouts/PageLayout";
|
||||
import Header from "@/components/shared/Header";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { AlertDialog } from "@/components/ui/alert-dialog";
|
||||
import Loader from "@/components/ui/loader";
|
||||
import { listWorkflows, createWorkflow, deleteWorkflow, exportWorkflow, importWorkflow } from "@/handlers/api/workflow.handler";
|
||||
import { IWorkflow } from "@/types/workflow";
|
||||
import { Plus, Download, Upload, Trash2, Pencil, Workflow } from "lucide-react";
|
||||
import { IWorkflow, IWorkflowRun } from "@/types/workflow";
|
||||
import {
|
||||
Plus, Download, Upload, Trash2, Workflow,
|
||||
Clock, Webhook, GitBranch, Zap, Play,
|
||||
CheckCircle, XCircle, AlertCircle,
|
||||
} from "lucide-react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useRouter } from "next/router";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
import { format } from "date-fns";
|
||||
import hotToast from "react-hot-toast";
|
||||
import { formatDistanceToNow, format } from "date-fns";
|
||||
|
||||
interface IWorkflowEnriched extends IWorkflow {
|
||||
nodeCount: number;
|
||||
triggerCount: number;
|
||||
actionCount: number;
|
||||
totalRuns: number;
|
||||
lastRun: IWorkflowRun | null;
|
||||
}
|
||||
|
||||
function parseCronLabel(cron: string): string {
|
||||
if (!cron) return "";
|
||||
const parts = cron.split(" ");
|
||||
if (parts.length !== 5) return cron;
|
||||
if (parts[0].startsWith("*/") && parts[1] === "*") return `Every ${parts[0].slice(2)}m`;
|
||||
if (parts[0] === "0" && parts[1].startsWith("*/")) return `Every ${parts[1].slice(2)}h`;
|
||||
if (parts[0] === "0" && parts[1] === "0" && parts[2].startsWith("*/")) return `Every ${parts[2].slice(2)}d`;
|
||||
return cron;
|
||||
}
|
||||
|
||||
function LastRunStatus({ run }: { run: IWorkflowRun | null }) {
|
||||
if (!run) return <span className="text-xs text-muted-foreground">Never run</span>;
|
||||
|
||||
const result = run.result ? (typeof run.result === "string" ? JSON.parse(run.result) : run.result) : {};
|
||||
const timeAgo = run.startedAt ? formatDistanceToNow(new Date(run.startedAt), { addSuffix: true }) : "";
|
||||
|
||||
if (run.status === "completed") {
|
||||
return (
|
||||
<div className="flex items-center gap-1.5">
|
||||
<CheckCircle className="h-3.5 w-3.5 text-green-500" />
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{result.matchedAssets || 0} assets {timeAgo}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (run.status === "failed") {
|
||||
return (
|
||||
<div className="flex items-center gap-1.5">
|
||||
<XCircle className="h-3.5 w-3.5 text-destructive" />
|
||||
<span className="text-xs text-destructive">Failed {timeAgo}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="flex items-center gap-1.5">
|
||||
<AlertCircle className="h-3.5 w-3.5 text-yellow-500 animate-pulse" />
|
||||
<span className="text-xs text-muted-foreground">Running...</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function WorkflowsPage() {
|
||||
const router = useRouter();
|
||||
const [workflows, setWorkflows] = useState<IWorkflow[]>([]);
|
||||
const [workflows, setWorkflows] = useState<IWorkflowEnriched[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
@@ -23,9 +76,9 @@ export default function WorkflowsPage() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await listWorkflows();
|
||||
setWorkflows(data);
|
||||
setWorkflows(data as IWorkflowEnriched[]);
|
||||
} catch {
|
||||
toast({ title: "Error", description: "Failed to load workflows", variant: "destructive" });
|
||||
hotToast.error("Failed to load workflows");
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
@@ -39,7 +92,7 @@ export default function WorkflowsPage() {
|
||||
const workflow = await createWorkflow({ name: "Untitled Workflow" });
|
||||
router.push(`/workflows/${workflow.id}`);
|
||||
} catch {
|
||||
toast({ title: "Error", description: "Failed to create workflow", variant: "destructive" });
|
||||
hotToast.error("Failed to create workflow");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,9 +100,9 @@ export default function WorkflowsPage() {
|
||||
try {
|
||||
await deleteWorkflow(id);
|
||||
setWorkflows((prev) => prev.filter((w) => w.id !== id));
|
||||
toast({ title: "Deleted", description: "Workflow deleted" });
|
||||
hotToast.success("Workflow deleted");
|
||||
} catch {
|
||||
toast({ title: "Error", description: "Failed to delete workflow", variant: "destructive" });
|
||||
hotToast.error("Failed to delete workflow");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -64,7 +117,7 @@ export default function WorkflowsPage() {
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
} catch {
|
||||
toast({ title: "Error", description: "Failed to export workflow", variant: "destructive" });
|
||||
hotToast.error("Failed to export workflow");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -75,10 +128,10 @@ export default function WorkflowsPage() {
|
||||
const text = await file.text();
|
||||
const data = JSON.parse(text);
|
||||
const workflow = await importWorkflow(data);
|
||||
setWorkflows((prev) => [workflow, ...prev]);
|
||||
toast({ title: "Imported", description: `Workflow "${workflow.name}" imported` });
|
||||
setWorkflows((prev) => [workflow as IWorkflowEnriched, ...prev]);
|
||||
hotToast.success(`Workflow "${workflow.name}" imported`);
|
||||
} catch {
|
||||
toast({ title: "Error", description: "Failed to import workflow. Check the file format.", variant: "destructive" });
|
||||
hotToast.error("Failed to import workflow");
|
||||
}
|
||||
if (fileInputRef.current) fileInputRef.current.value = "";
|
||||
};
|
||||
@@ -112,10 +165,12 @@ export default function WorkflowsPage() {
|
||||
<div className="flex justify-center py-12"><Loader /></div>
|
||||
) : workflows.length === 0 ? (
|
||||
<div className="flex flex-col items-center justify-center py-20 text-center">
|
||||
<Workflow className="h-12 w-12 text-muted-foreground mb-4" />
|
||||
<h3 className="text-lg font-medium mb-2">No workflows yet</h3>
|
||||
<p className="text-sm text-muted-foreground mb-4">
|
||||
Create your first workflow to start automating your library organization.
|
||||
<div className="rounded-full bg-muted p-4 mb-4">
|
||||
<Workflow className="h-8 w-8 text-muted-foreground" />
|
||||
</div>
|
||||
<h3 className="text-lg font-medium mb-1">No workflows yet</h3>
|
||||
<p className="text-sm text-muted-foreground mb-6 max-w-sm">
|
||||
Automate your library with visual workflows. Create rules to organize, tag, and manage assets automatically.
|
||||
</p>
|
||||
<Button onClick={handleCreate}>
|
||||
<Plus className="h-4 w-4 mr-1" />
|
||||
@@ -123,50 +178,86 @@ export default function WorkflowsPage() {
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 p-4">
|
||||
<div className="p-4 space-y-2">
|
||||
{workflows.map((w) => (
|
||||
<Card key={w.id} className="flex flex-col">
|
||||
<CardHeader className="pb-2">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex-1 min-w-0">
|
||||
<CardTitle className="text-base truncate">{w.name}</CardTitle>
|
||||
{w.description && (
|
||||
<CardDescription className="mt-1 line-clamp-2">{w.description}</CardDescription>
|
||||
)}
|
||||
</div>
|
||||
<Badge variant={w.enabled ? "default" : "secondary"} className="ml-2 shrink-0">
|
||||
{w.enabled ? "Enabled" : "Disabled"}
|
||||
</Badge>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="flex-1 flex flex-col justify-end gap-3">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
{w.cronSchedule && <Badge variant="outline" className="text-xs">Scheduled</Badge>}
|
||||
{w.webhookToken && <Badge variant="outline" className="text-xs">Webhook</Badge>}
|
||||
<span className="ml-auto">
|
||||
{w.updatedAt ? format(new Date(w.updatedAt), "MMM d, yyyy") : ""}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
key={w.id}
|
||||
className="group flex items-center gap-4 px-4 py-3 rounded-lg border bg-card hover:bg-accent/50 transition-all cursor-pointer"
|
||||
onClick={() => router.push(`/workflows/${w.id}`)}
|
||||
>
|
||||
{/* Status indicator */}
|
||||
<div className={`h-9 w-9 rounded-lg flex items-center justify-center shrink-0 ${
|
||||
w.enabled ? "bg-green-500/10 text-green-500" : "bg-muted text-muted-foreground"
|
||||
}`}>
|
||||
<Zap className="h-4 w-4" />
|
||||
</div>
|
||||
|
||||
{/* Main info */}
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="outline" size="sm" className="flex-1" onClick={() => router.push(`/workflows/${w.id}`)}>
|
||||
<Pencil className="h-3.5 w-3.5 mr-1" />
|
||||
Edit
|
||||
</Button>
|
||||
<Button variant="ghost" size="sm" onClick={() => handleExport(w.id, w.name)}>
|
||||
<Download className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<AlertDialog
|
||||
title="Delete workflow?"
|
||||
description={`This will permanently delete "${w.name}" and all its nodes, edges, and run history.`}
|
||||
onConfirm={() => handleDelete(w.id)}
|
||||
>
|
||||
<Button variant="ghost" size="sm">
|
||||
<Trash2 className="h-3.5 w-3.5 text-destructive" />
|
||||
</Button>
|
||||
</AlertDialog>
|
||||
<span className="text-sm font-medium truncate">{w.name}</span>
|
||||
{w.enabled ? (
|
||||
<span className="h-2 w-2 rounded-full bg-green-500 shrink-0" title="Enabled" />
|
||||
) : (
|
||||
<span className="h-2 w-2 rounded-full bg-muted-foreground/30 shrink-0" title="Disabled" />
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
{w.description ? (
|
||||
<p className="text-xs text-muted-foreground truncate mt-0.5">{w.description}</p>
|
||||
) : (
|
||||
<p className="text-xs text-muted-foreground/50 mt-0.5">No description</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Triggers */}
|
||||
<div className="hidden md:flex items-center gap-1.5 shrink-0">
|
||||
{w.cronSchedule && (
|
||||
<Badge variant="outline" className="text-[10px] h-5 gap-1 font-normal">
|
||||
<Clock className="h-2.5 w-2.5" />
|
||||
{parseCronLabel(w.cronSchedule)}
|
||||
</Badge>
|
||||
)}
|
||||
{w.webhookToken && (
|
||||
<Badge variant="outline" className="text-[10px] h-5 gap-1 font-normal">
|
||||
<Webhook className="h-2.5 w-2.5" />
|
||||
Webhook
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Stats */}
|
||||
<div className="hidden lg:flex items-center gap-3 shrink-0 text-xs text-muted-foreground">
|
||||
<div className="flex items-center gap-1" title="Nodes">
|
||||
<GitBranch className="h-3 w-3" />
|
||||
<span>{w.nodeCount || 0}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1" title="Total runs">
|
||||
<Play className="h-3 w-3" />
|
||||
<span>{w.totalRuns || 0}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Last run */}
|
||||
<div className="hidden sm:block shrink-0 min-w-[140px] text-right">
|
||||
<LastRunStatus run={w.lastRun} />
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-0.5 shrink-0 opacity-0 group-hover:opacity-100 transition-opacity" onClick={(e) => e.stopPropagation()}>
|
||||
<Button variant="ghost" size="sm" className="h-7 w-7 p-0" title="Export" onClick={() => handleExport(w.id, w.name)}>
|
||||
<Download className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<AlertDialog
|
||||
title="Delete workflow?"
|
||||
description={`This will permanently delete "${w.name}" and all its nodes, edges, and run history.`}
|
||||
onConfirm={() => handleDelete(w.id)}
|
||||
>
|
||||
<Button variant="ghost" size="sm" className="h-7 w-7 p-0" title="Delete">
|
||||
<Trash2 className="h-3.5 w-3.5 text-destructive" />
|
||||
</Button>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user