diff --git a/src/components/tasks/apply-workflow-picker-dialog.tsx b/src/components/tasks/apply-workflow-picker-dialog.tsx new file mode 100644 index 0000000..cf84d08 --- /dev/null +++ b/src/components/tasks/apply-workflow-picker-dialog.tsx @@ -0,0 +1,205 @@ +import { useState, useEffect } from "react"; +import { supabase } from "@/integrations/supabase/client"; +import { useAuth } from "@/lib/auth"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogFooter, +} from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Label } from "@/components/ui/label"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; +import { Calendar } from "@/components/ui/calendar"; +import { CalendarIcon, Loader2 } from "lucide-react"; +import { format, addDays } from "date-fns"; +import { toast } from "sonner"; +import { createNotifications } from "@/lib/notifications"; + +interface Template { + id: string; + name: string; +} +interface CaseRow { + id: string; + case_number: string; + title: string; +} + +/** + * Standalone "apply workflow" dialog for the Tasks page — lets the user + * pick BOTH a case and a template before sequentially spawning the first task. + */ +export function ApplyWorkflowPickerDialog({ + open, + onOpenChange, + onApplied, +}: { + open: boolean; + onOpenChange: (v: boolean) => void; + onApplied?: () => void; +}) { + const { user } = useAuth(); + const [templates, setTemplates] = useState([]); + const [cases, setCases] = useState([]); + const [templateId, setTemplateId] = useState(null); + const [caseId, setCaseId] = useState(null); + const [startDate, setStartDate] = useState(new Date()); + const [submitting, setSubmitting] = useState(false); + + useEffect(() => { + if (!open) return; + setTemplateId(null); + setCaseId(null); + setStartDate(new Date()); + Promise.all([ + supabase.from("workflow_templates").select("id, name").order("name"), + supabase + .from("cases") + .select("id, case_number, title") + .is("archived_at", null) + .order("case_number"), + ]).then(([t, c]) => { + setTemplates((t.data ?? []) as Template[]); + setCases((c.data ?? []) as CaseRow[]); + }); + }, [open]); + + const apply = async () => { + if (!user || !templateId || !caseId) return; + setSubmitting(true); + const { data: tpl } = await supabase + .from("workflow_template_tasks") + .select("*") + .eq("template_id", templateId) + .order("sort_order"); + const items = tpl ?? []; + if (!items.length) { + setSubmitting(false); + toast.error("Template has no tasks."); + return; + } + const first = items[0]; + const { data: created, error } = await supabase + .from("tasks") + .insert({ + title: first.title, + description: first.description, + priority: first.priority, + due_date: format(addDays(startDate, first.days_from_start), "yyyy-MM-dd"), + case_id: caseId, + workflow_template_id: templateId, + template_task_id: first.id, + created_by: user.id, + sort_order: 0, + }) + .select("id") + .single(); + if (error || !created) { + setSubmitting(false); + toast.error(error?.message || "Failed to apply"); + return; + } + if (first.default_assignee_id) { + await supabase.from("task_assignees").insert({ + task_id: created.id, + user_id: first.default_assignee_id, + assigned_by: user.id, + }); + if (first.default_assignee_id !== user.id) { + await createNotifications([ + { + user_id: first.default_assignee_id, + kind: "task_assigned", + title: `Assigned: ${first.title}`, + link: `/tasks?task=${created.id}`, + task_id: created.id, + case_id: caseId, + created_by: user.id, + }, + ]); + } + } + await supabase.from("task_history").insert({ + task_id: created.id, + actor_id: user.id, + event: "created_from_workflow", + detail: { template_id: templateId, sequential: true, total_steps: items.length }, + }); + setSubmitting(false); + toast.success( + `Workflow started — ${items.length} step${items.length === 1 ? "" : "s"} will run sequentially.`, + ); + onApplied?.(); + onOpenChange(false); + }; + + return ( + + + + Apply workflow template + +
+
+ + +
+
+ + +
+
+ + + + + + + d && setStartDate(d)} /> + + +
+
+ + + + +
+
+ ); +}