Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | "use client"; import { useState, useEffect, useCallback } from "react"; import { Button } from "@/components/ui"; import { Icon } from "@/components/ui/icons"; import ConfirmDialog from "@/components/ui/ConfirmDialog"; import toast from "react-hot-toast"; import WorkflowBuilder from "@/components/features/admin/workflows/WorkflowBuilder"; interface WorkflowStats { totalExecutions: number; completedExecutions: number; failedExecutions: number; activeExecutions: number; completionRate: number; lastExecutedAt: string | null; } interface Workflow { id: number; name: string; description: string | null; trigger: string; triggerConfig: Record<string, unknown> | null; stepsCount: number; isActive: boolean; isDraft: boolean; version: number; stats: WorkflowStats | null; createdAt: string; updatedAt: string; } const TRIGGER_LABELS: Record<string, string> = { USER_SIGNUP: "User Sign Up", FIRST_PURCHASE: "First Purchase", CART_ABANDONED: "Cart Abandoned", BROWSE_ABANDONED: "Browse Abandoned", ORDER_COMPLETED: "Order Completed", ORDER_SHIPPED: "Order Shipped", ORDER_DELIVERED: "Order Delivered", REVIEW_POSTED: "Review Posted", BIRTHDAY: "Birthday", ANNIVERSARY: "Anniversary", POINTS_EARNED: "Points Earned", TIER_UPGRADED: "Tier Upgraded", INACTIVE_DAYS: "Inactive Days", SEGMENT_ENTERED: "Segment Entered", SEGMENT_EXITED: "Segment Exited", MANUAL: "Manual", SCHEDULED: "Scheduled"}; export default function AdminWorkflowsPage() { const [workflows, setWorkflows] = useState<Workflow[]>([]); const [loading, setLoading] = useState(true); const [showBuilder, setShowBuilder] = useState(false); const [editingWorkflow, setEditingWorkflow] = useState<Workflow | null>(null); const [deleteWorkflowId, setDeleteWorkflowId] = useState<number | null>(null); const [deleting, setDeleting] = useState(false); const fetchWorkflows = useCallback(async () => { try { setLoading(true); const res = await fetch("/api/admin/workflows?includeStats=true"); const result = await res.json(); if (result.success) { setWorkflows(result.data); } else { toast.error("Failed to load workflows"); } } catch { toast.error("Failed to load workflows"); } finally { setLoading(false); } }, []); useEffect(() => { fetchWorkflows(); }, [fetchWorkflows]); const handleCreate = () => { setEditingWorkflow(null); setShowBuilder(true); }; const handleEdit = (workflow: Workflow) => { setEditingWorkflow(workflow); setShowBuilder(true); }; const handleSave = async () => { setShowBuilder(false); setEditingWorkflow(null); fetchWorkflows(); }; const handleDelete = async () => { if (!deleteWorkflowId) return; setDeleting(true); try { const res = await fetch(`/api/admin/workflows/${deleteWorkflowId}`, { method: "DELETE"}); const result = await res.json(); if (result.success) { toast.success("Workflow deleted"); setDeleteWorkflowId(null); fetchWorkflows(); } else { toast.error(result.error || "Failed to delete workflow"); } } catch { toast.error("Failed to delete workflow"); } finally { setDeleting(false); } }; const handleToggleActive = async (workflow: Workflow) => { if (workflow.isDraft) { toast.error("Cannot activate a draft workflow. Publish it first."); return; } try { const res = await fetch(`/api/admin/workflows/${workflow.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ isActive: !workflow.isActive })}); const result = await res.json(); if (result.success) { toast.success( `Workflow ${workflow.isActive ? "deactivated" : "activated"}` ); fetchWorkflows(); } else { toast.error(result.error || "Failed to update workflow"); } } catch { toast.error("Failed to update workflow"); } }; if (showBuilder) { return ( <div className="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="mb-6"> <Button variant="ghost" onClick={() => { setShowBuilder(false); setEditingWorkflow(null); }} leftIcon={<Icon name="chevron-left" size={16} />} > Back to Workflows </Button> </div> <WorkflowBuilder workflowId={editingWorkflow?.id} onSave={handleSave} onCancel={() => { setShowBuilder(false); setEditingWorkflow(null); }} /> </div> ); } return ( <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> {/* Header */} <div className="flex justify-between items-center mb-8"> <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-white"> Marketing Workflows </h1> <p className="text-gray-600 dark:text-gray-400 mt-1"> Create automated marketing sequences triggered by customer actions </p> </div> <Button variant="primary" onClick={handleCreate}> <Icon name="plus" size={16} className="mr-2" /> Create Workflow </Button> </div> {/* Workflows List */} {loading ? ( <div className="flex items-center justify-center py-12"> <Icon name="reload" size={32} className="animate-spin text-primary" /> </div> ) : workflows.length === 0 ? ( <div className="bg-white dark:bg-gray-800 rounded-lg shadow p-12 text-center"> <Icon name="bolt" size={48} className="mx-auto mb-4 text-gray-400 dark:text-gray-600" /> <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2"> No Workflows Yet </h3> <p className="text-gray-600 dark:text-gray-400 mb-6"> Create your first automated marketing workflow to engage customers. </p> <Button variant="primary" onClick={handleCreate}> Create Your First Workflow </Button> </div> ) : ( <div className="grid gap-4"> {workflows.map((workflow) => ( <div key={workflow.id} className={`bg-white dark:bg-gray-800 rounded-lg shadow border-l-4 ${ workflow.isActive ? "border-green-500" : workflow.isDraft ? "border-yellow-500" : "border-gray-300 dark:border-gray-600" }`} > <div className="p-6"> <div className="flex items-start justify-between"> <div className="flex-1"> <div className="flex items-center gap-3 mb-2"> <h3 className="text-lg font-semibold text-gray-900 dark:text-white"> {workflow.name} </h3> {workflow.isDraft && ( <span className="px-2 py-0.5 text-xs bg-yellow-100 dark:bg-yellow-900/30 text-yellow-600 dark:text-yellow-400 rounded"> Draft </span> )} {!workflow.isActive && !workflow.isDraft && ( <span className="px-2 py-0.5 text-xs bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-400 rounded"> Inactive </span> )} </div> {workflow.description && ( <p className="text-gray-600 dark:text-gray-400 text-sm mb-4"> {workflow.description} </p> )} {/* Trigger & Stats */} <div className="flex items-center gap-6 text-sm"> <div className="flex items-center gap-2"> <span className="px-2 py-1 bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400 rounded text-xs font-medium"> {TRIGGER_LABELS[workflow.trigger] || workflow.trigger} </span> </div> <div className="flex items-center gap-2 text-gray-600 dark:text-gray-400"> <Icon name="package" size={16} /> <span>{workflow.stepsCount} steps</span> </div> {workflow.stats && ( <> <div className="flex items-center gap-2 text-gray-600 dark:text-gray-400"> <Icon name="bolt" size={16} /> <span>{workflow.stats.totalExecutions} runs</span> </div> {workflow.stats.completionRate > 0 && ( <div className="flex items-center gap-2 text-green-600 dark:text-green-400"> <Icon name="check-circle" size={16} /> <span> {workflow.stats.completionRate.toFixed(0)}% completion </span> </div> )} </> )} </div> </div> {/* Actions */} <div className="flex items-center gap-2 ml-4"> <button onClick={() => handleToggleActive(workflow)} className={`p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 ${ workflow.isActive ? "text-green-600" : "text-gray-400" }`} title={workflow.isActive ? "Deactivate" : "Activate"} disabled={workflow.isDraft} > <Icon name={workflow.isActive ? "check-circle" : "x-circle"} size={20} /> </button> <button onClick={() => handleEdit(workflow)} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 text-blue-600" title="Edit" > <Icon name="edit" size={20} /> </button> <button onClick={() => setDeleteWorkflowId(workflow.id)} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-gray-700 text-red-600" title="Delete" disabled={ workflow.stats !== null && workflow.stats.activeExecutions > 0 } > <Icon name="trash" size={20} /> </button> </div> </div> </div> </div> ))} </div> )} {/* Delete Confirmation */} <ConfirmDialog isOpen={deleteWorkflowId !== null} onCancel={() => setDeleteWorkflowId(null)} onConfirm={handleDelete} title="Delete Workflow" message="Are you sure you want to delete this workflow? This action cannot be undone." confirmText={deleting ? "Deleting..." : "Delete"} variant="danger" /> </div> ); } |