diff --git a/src/pages/api/workflows/index.ts b/src/pages/api/workflows/index.ts index 3245468..c0c5e9b 100644 --- a/src/pages/api/workflows/index.ts +++ b/src/pages/api/workflows/index.ts @@ -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") { diff --git a/src/pages/workflows/index.tsx b/src/pages/workflows/index.tsx index 188a1b2..e204cf9 100644 --- a/src/pages/workflows/index.tsx +++ b/src/pages/workflows/index.tsx @@ -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 Never run; + + 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 ( +
- Create your first workflow to start automating your library organization. +
+ Automate your library with visual workflows. Create rules to organize, tag, and manage assets automatically.
{w.description}
+ ) : ( +No description
+ )} +