1import{m as z,cV as ve,cW as ke,r as p,cX as Q,s as g,u as X,a9 as B,aa as V,j as e,V as M,B as Y,bX as be,bM as Ne,L as me,H as F,C as Ce,a as Se,c0 as Te,cD as De,o as ne,R as Pe,eg as qe,ah as $e,c4 as Le,eG as J,h as Ae,Y as Ee,$ as Ue,a0 as Re,a1 as Me,a3 as C,a4 as re,a5 as ue,bz as P,bA as q,bB as $,bC as L,bD as h,G as Fe,T as he,U as xe,y as E,W as fe,ae as T,_ as Ie,bW as ze,b9 as Be,aJ as Ve,aK as Oe,aL as Ke,aM as Qe,dr as H,bl as Ye,p as He,q as Ge,t as ie,v as We,w as Xe,x as Je,P as Ze,c6 as es,c7 as ss,c8 as G,c9 as as}from"./index-k9gY-zCA.js";import{s as pe,f as ts}from"./preferences-zwD4sNOl.js";import{C as Z}from"./calendar-C66LcZZH.js";import{L as le}from"./link-dIEkwTAF.js";import{f as R}from"./format-ahHg-5y_.js";import{C as ns}from"./clipboard-list-C4dxMxzX.js";import{C as ge}from"./calendar-lnkcuNyu.js";import{P as oe}from"./paperclip-D-TeRozE.js";import{D as rs}from"./download-C6OemliB.js";import"./subDays-Drip29jU.js";function U(){const{user:s,activeRole:i}=z(),{data:d=[],isLoading:a}=ve(),{data:l=[],isLoading:t}=ke();return p.useMemo(()=>{const n=a||t;if(!s)return{loading:n,canView:!1,canCreate:!1,canEdit:!1,canDelete:!1};if(s.allRoles.includes("admin"))return{loading:n,canView:!0,canCreate:!0,canEdit:!0,canDelete:!0};const u=i==="staff"||i==="volunteer"?i:s.allRoles.includes("staff")?"staff":s.allRoles.includes("volunteer")?"volunteer":null;if(!u)return{loading:n,canView:!1,canCreate:!1,canEdit:!1,canDelete:!1};const r=[u];return{loading:n,canView:Q(d,l,s.id,r,"tasks.view"),canCreate:Q(d,l,s.id,r,"tasks.create"),canEdit:Q(d,l,s.id,r,"tasks.edit"),canDelete:Q(d,l,s.id,r,"tasks.delete")}},[i,a,d,s,t,l])}const is=["admin","staff","volunteer"];function ls(s){const i=new Set(s.map(d=>String(d).toLowerCase()));return i.has("admin")?"/admin":i.has("staff")||i.has("volunteer")?"/staff":"/admin"}function os(s,i){return`${ls(i)}/tasks?task=${s}`}async function ee(s,i){const d=Array.from(new Set(s.filter(Boolean)));if(d.length===0)return{};const{data:a,error:l}=await g.from("user_roles").select("user_id, role").in("user_id",d).in("role",is);if(l)throw l;const t=new Map;for(const u of a??[]){const r=t.get(u.user_id)??[];r.push(String(u.role)),t.set(u.user_id,r)}const n={};for(const u of d){const r=t.get(u)??[];n[u]=os(i,r)}return n}function W(s={}){const{user:i}=z(),{canView:d}=U();return X({queryKey:["tasks",s],enabled:!!i&&d,queryFn:async()=>{let a=g.from("tasks").select("*, creator:profiles!tasks_created_by_fkey(name, email), assignee:profiles!tasks_assignee_id_fkey(name, email)").order("created_at",{ascending:!1}).limit(200);if(s.tab==="my-tasks"?a=a.eq("assignee_id",i.id).neq("status","completed"):s.tab==="team-tasks"?a=a.neq("status","completed"):s.tab==="completed"&&(a=a.eq("status","completed")),s.assignee&&s.assignee!=="all"&&(s.assignee==="me"?a=a.eq("assignee_id",i.id):s.assignee==="unassigned"?a=a.is("assignee_id",null):a=a.eq("assignee_id",s.assignee)),s.status&&s.status!=="all"&&(a=a.eq("status",s.status)),s.priority&&s.priority!=="all"&&(a=a.eq("priority",s.priority)),s.category&&s.category!=="all"&&(a=a.eq("category",s.category)),s.dueDate&&s.dueDate!=="all"){const n=new Date;if(s.dueDate==="overdue")a=a.lt("due_date",n.toISOString().split("T")[0]).neq("status","completed");else if(s.dueDate==="today")a=a.eq("due_date",n.toISOString().split("T")[0]);else if(s.dueDate==="this-week"){const u=new Date(n);u.setDate(u.getDate()+7),a=a.gte("due_date",n.toISOString().split("T")[0]).lte("due_date",u.toISOString().split("T")[0])}else s.dueDate==="no-due-date"&&(a=a.is("due_date",null))}const{data:l,error:t}=await a;if(t)throw t;return l??[]}})}function cs(){const s=B(),{user:i}=z(),{canCreate:d}=U();return V({mutationFn:async a=>{if(!d)throw new Error("You do not have permission to create tasks.");const{data:l,error:t}=await g.from("tasks").insert({...a,created_by:i.id}).select().single();if(t)throw t;if(a.assignee_id&&a.assignee_id!==i.id){let n={};try{n=await ee([a.assignee_id],l.id)}catch{n={}}await pe(a.assignee_id,"task")&&await g.from("notifications").insert({user_id:a.assignee_id,type:"task",title:"New task assigned to you",message:a.title,link:n[a.assignee_id]??`/admin/tasks?task=${l.id}`,created_by:i.id,event_key:`task-assigned-${l.id}`})}return l},onSuccess:()=>{s.invalidateQueries({queryKey:["tasks"]}),s.invalidateQueries({queryKey:["admin-tasks"]})}})}function ds(){const s=B(),{user:i}=z(),{canEdit:d}=U();return V({mutationFn:async({id:a,...l})=>{if(!d)throw new Error("You do not have permission to edit tasks.");const t={...l};l.status==="completed"&&!l.completed_at?t.completed_at=new Date().toISOString():l.status&&l.status!=="completed"&&(t.completed_at=null),delete t.creator,delete t.assignee;const{data:n,error:u}=await g.from("tasks").update(t).eq("id",a).select().single();if(u)throw u;if(l.assignee_id&&l.assignee_id!==i.id){let r={};try{r=await ee([l.assignee_id],n.id)}catch{r={}}await pe(l.assignee_id,"task")&&await g.from("notifications").insert({user_id:l.assignee_id,type:"task",title:"Task assigned to you",message:n.title,link:r[l.assignee_id]??`/admin/tasks?task=${n.id}`,created_by:i.id,event_key:`task-reassigned-${n.id}-${Date.now()}`})}return n},onSuccess:()=>{s.invalidateQueries({queryKey:["tasks"]}),s.invalidateQueries({queryKey:["admin-tasks"]})}})}function ms(){const s=B(),{canDelete:i}=U();return V({mutationFn:async d=>{if(!i)throw new Error("You do not have permission to delete tasks.");const{error:a}=await g.from("tasks").delete().eq("id",d);if(a)throw a},onSuccess:()=>{s.invalidateQueries({queryKey:["tasks"]}),s.invalidateQueries({queryKey:["admin-tasks"]})}})}function je(s,i){if(!s||!i)return null;switch(s){case"donor":return`/admin/donors/${i}`;case"beneficiary":return`/admin/sponsorships/${i}`;case"campaign":return`/admin/campaigns/${i}`;case"sponsorship":return`/admin/sponsorships/${i}`;case"donation":return"/admin/financials/gifts";case"receipt":return"/admin/financials/receipts";case"message":return"/admin/messages/moderation";default:return null}}const ce={urgent:{label:"Urgent",className:"bg-destructive text-destructive-foreground"},high:{label:"High",className:"bg-destructive/80 text-destructive-foreground"},medium:{label:"Medium",className:"bg-amber-500 text-white"},low:{label:"Low",className:"bg-blue-500 text-white"}},de={open:{label:"Open",className:"border-blue-500 text-blue-600"},in_progress:{label:"In Progress",className:"border-amber-500 text-amber-600"},blocked:{label:"Blocked",className:"border-destructive text-destructive"},completed:{label:"Completed",className:"border-green-500 text-green-600"}};function us({task:s,onClick:i}){const d=ce[s.priority]??ce.medium,a=de[s.status]??de.open,l=s.due_date&&new Date(s.due_date)<new Date&&s.status!=="completed";return e.jsxs("button",{onClick:i,className:"w-full text-left p-4 rounded-2xl border bg-card hover:shadow-card-hover transition-all group",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h4",{className:M("font-medium text-sm truncate",s.status==="completed"&&"line-through text-muted-foreground"),children:s.title}),s.description&&e.jsx("p",{className:"text-xs text-muted-foreground mt-1 line-clamp-2",children:s.description})]}),e.jsx(Y,{className:M("text-[10px] shrink-0",d.className),children:d.label})]}),e.jsxs("div",{className:"flex items-center gap-3 mt-3 flex-wrap",children:[e.jsx(Y,{variant:"outline",className:M("text-[10px]",a.className),children:a.label}),s.category!=="general"&&e.jsxs("span",{className:"text-[10px] text-muted-foreground flex items-center gap-1",children:[e.jsx(be,{className:"h-3 w-3"}),s.category]}),s.due_date&&e.jsxs("span",{className:M("text-[10px] flex items-center gap-1",l?"text-destructive font-medium":"text-muted-foreground"),children:[e.jsx(Z,{className:"h-3 w-3"}),R(new Date(s.due_date),"MMM d")]}),s.assignee&&e.jsxs("span",{className:"text-[10px] text-muted-foreground flex items-center gap-1 ml-auto",children:[e.jsx(Ne,{className:"h-3 w-3"}),s.assignee.name]}),s.related_label&&(()=>{const t=je(s.related_type,s.related_id);return t?e.jsxs(me,{to:t,onClick:n=>n.stopPropagation(),className:"text-[10px] text-primary flex items-center gap-1 hover:underline",children:[e.jsx(le,{className:"h-3 w-3"}),s.related_label]}):e.jsxs("span",{className:"text-[10px] text-primary flex items-center gap-1",children:[e.jsx(le,{className:"h-3 w-3"}),s.related_label]})})()]})]})}function hs({tasks:s,isLoading:i,emptyMessage:d,onTaskClick:a}){return i?e.jsx("div",{className:"flex items-center justify-center py-16",children:e.jsx(F,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):s.length===0?e.jsx(Ce,{className:"border-dashed",children:e.jsxs(Se,{className:"flex flex-col items-center justify-center py-16 text-center",children:[e.jsx("div",{className:"rounded-full bg-muted p-4 mb-4",children:e.jsx(ns,{className:"h-8 w-8 text-muted-foreground"})}),e.jsx("h3",{className:"text-lg font-medium text-foreground mb-2",children:d}),e.jsx("p",{className:"text-sm text-muted-foreground max-w-md",children:"Create tasks to track work across your organization."})]})}):e.jsx("div",{className:"space-y-2",children:s.map(l=>e.jsx(us,{task:l,onClick:()=>a(l)},l.id))})}async function xs(s,i,d=a=>a){const a=i.trim(),t=Te(a).find(n=>n.length>=2);switch(s){case"donor":{let n=g.from("profiles").select("id, name, email, role, organization_contact_prefix, organization_contact_first_name, organization_contact_middle_name, organization_contact_last_name, organization_contact_suffix, organization_contact_preferred_name").not("role","in",Pe).order("updated_at",{ascending:!1}).order("created_at",{ascending:!1}).limit(40);t&&(n=n.or(qe(t)));const{data:u,error:r}=await n;if(r)throw r;return(a?(u??[]).filter(c=>ne(c,a)):u??[]).map(c=>({id:c.id,label:`${$e(c).labeled} (${Le(c.email,d("profiles.no_email"))})`}))}case"beneficiary":{let n=g.from("beneficiaries").select("id, name").limit(20);a&&(n=n.ilike("name",`%${a}%`));const{data:u}=await n;return(u??[]).map(r=>({id:r.id,label:r.name}))}case"campaign":{let n=g.from("campaigns").select("id, name").limit(20);a&&(n=n.ilike("name",`%${a}%`));const{data:u}=await n;return(u??[]).map(r=>({id:r.id,label:r.name}))}case"donation":{const n=g.from("donations").select("id, amount, currency, donor:profiles!donations_donor_id_fkey(name)").limit(20),{data:u}=await n;let r=(u??[]).map(o=>{var c,m;return{id:o.id,label:`${De(o.amount,o.currency)} â ${((c=o.donor)==null?void 0:c.name)??"Unknown"}`,_name:((m=o.donor)==null?void 0:m.name)??""}});return a&&(r=r.filter(o=>ne({name:o._name},a))),r.map(({id:o,label:c})=>({id:o,label:c}))}case"sponsorship":{const n=g.from("sponsorships").select("id, beneficiary:beneficiaries(name), sponsor:profiles!sponsorships_sponsor_id_fkey(name)").limit(20),{data:u}=await n;let r=(u??[]).map(o=>{var c,m,f,_;return{id:o.id,label:`${((c=o.sponsor)==null?void 0:c.name)??"?"} â ${((m=o.beneficiary)==null?void 0:m.name)??"?"}`,_search:`${((f=o.sponsor)==null?void 0:f.name)??""} ${((_=o.beneficiary)==null?void 0:_.name)??""}`.toLowerCase()}});return a&&(r=r.filter(o=>o._search.includes(a.toLowerCase()))),r.map(({id:o,label:c})=>({id:o,label:c}))}case"receipt":{let n=g.from("receipts").select("id, receipt_number, year").limit(20);a&&(n=n.ilike("receipt_number",`%${a}%`));const{data:u}=await n;return(u??[]).map(r=>({id:r.id,label:`${r.receipt_number} (${r.year})`}))}case"message":{const n=g.from("messages").select("id, body, sponsor:profiles!messages_sponsor_id_fkey(name)").limit(20),{data:u}=await n;let r=(u??[]).map(o=>{var c,m;return{id:o.id,label:`${((c=o.sponsor)==null?void 0:c.name)??"Unknown"}: ${(o.body??"").slice(0,40)}...`,_search:`${((m=o.sponsor)==null?void 0:m.name)??""} ${o.body??""}`.toLowerCase()}});return a&&(r=r.filter(o=>o._search.includes(a.toLowerCase()))),r.map(({id:o,label:c})=>({id:o,label:c}))}default:return[]}}
1const fs={general:null,donor:"donor",beneficiary:"beneficiary",campaign:"campaign",message:"message",gift:"donation",receipt:"receipt",sponsorship:"sponsorship",workflow:null},ps={donor:"tasks.select_supporter",beneficiary:"tasks.select_sponsorship",campaign:"tasks.select_campaign",message:"tasks.select_message",gift:"tasks.select_gift",receipt:"tasks.select_receipt",sponsorship:"tasks.select_sponsorship_link"};function gs({open:s,onOpenChange:i,canCreate:d=!0}){const a=cs(),{data:l=[]}=J(),{t}=Ae(),[n,u]=p.useState(""),[r,o]=p.useState(""),[c,m]=p.useState("medium"),[f,_]=p.useState("general"),[v,j]=p.useState(),[D,N]=p.useState("__unassigned__"),[k,b]=p.useState(null),[w,S]=p.useState(null),[O,x]=p.useState(""),[A,I]=p.useState([]),[se,ae]=p.useState(!1),K=fs[f]??null;p.useEffect(()=>{if(!K){I([]);return}ae(!0),xs(K,O,t).then(I).finally(()=>ae(!1))},[K,O,t]);const ye=y=>{_(y),b(null),S(null),x("")},_e=()=>{u(""),o(""),m("medium"),_("general"),j(void 0),N("__unassigned__"),b(null),S(null),x("")},we=async()=>{if(!n.trim())return;const y={title:n.trim(),description:r.trim()||void 0,priority:c,category:f,due_date:v?R(v,"yyyy-MM-dd"):null,assignee_id:D==="__unassigned__"?null:D||null,related_type:K,related_id:k,related_label:w};
1try{await a.mutateAsync(y),T.success(t("tasks.created")),_e(),i(!1)}catch(te){T.error(te instanceof Error?te.message:t("tasks.create_failed"))}};return e.jsx(Ee,{open:s,onOpenChange:i,children:e.jsxs(Ue,{className:"max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx(Re,{children:e.jsx(Me,{children:t("tasks.create_task")})}),e.jsxs("div",{className:"space-y-4 mt-2",children:[e.jsxs("div",{children:[e.jsx(C,{htmlFor:"title",children:t("tasks.title_required")}),e.jsx(re,{id:"title",value:n,onChange:y=>u(y.target.value),placeholder:t("tasks.title_placeholder")})]}),e.jsxs("div",{children:[e.jsx(C,{htmlFor:"desc",children:t("common.description")}),e.jsx(ue,{id:"desc",value:r,onChange:y=>o(y.target.value),placeholder:t("tasks.details_placeholder"),rows:3})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(C,{children:t("tasks.priority")}),e.jsxs(P,{value:c,onValueChange:y=>m(y),children:[e.jsx(q,{children:e.jsx($,{})}),e.jsxs(L,{children:[e.jsx(h,{value:"low",children:t("tasks.priority_low")}),e.jsx(h,{value:"medium",children:t("tasks.priority_medium")}),e.jsx(h,{value:"high",children:t("tasks.priority_high")}),e.jsx(h,{value:"urgent",children:t("tasks.priority_urgent")})]})]})]}),e.jsxs("div",{children:[e.jsx(C,{children:t("tasks.category")}),e.jsxs(P,{value:f,onValueChange:ye,children:[e.jsx(q,{children:e.jsx($,{})}),e.jsxs(L,{children:[e.jsx(h,{value:"general",children:t("tasks.category_general")}),e.jsx(h,{value:"donor",children:t("tasks.category_supporter")}),e.jsx(h,{value:"beneficiary",children:t("tasks.category_sponsorship")}),e.jsx(h,{value:"campaign",children:t("tasks.category_campaign")}),e.jsx(h,{value:"message",children:t("tasks.category_message")}),e.jsx(h,{value:"gift",children:t("tasks.category_gift")}),e.jsx(h,{value:"receipt",children:t("tasks.category_receipt")}),e.jsx(h,{value:"sponsorship",children:t("tasks.category_sponsorship_link")}),e.jsx(h,{value:"workflow",children:t("tasks.category_workflow")})]})]})]})]}),K&&e.jsxs("div",{children:[e.jsx(C,{className:"text-xs text-muted-foreground mb-1.5 block",children:t(ps[f]??"tasks.select_record")}),e.jsxs("div",{className:"relative mb-2",children:[e.jsx(Fe,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(re,{placeholder:t("tasks.type_to_search"),value:O,onChange:y=>x(y.target.value),className:"pl-8"})]}),se&&e.jsx(F,{className:"h-4 w-4 animate-spin text-muted-foreground"}),e.jsxs("div",{className:"max-h-32 overflow-y-auto space-y-1 border rounded-md p-1",children:[A.map(y=>e.jsx("button",{type:"button",onClick:()=>{b(y.id),S(y.label)},className:M("w-full text-left text-sm px-2 py-1.5 rounded-md hover:bg-muted transition-colors",k===y.id&&"bg-primary/10 text-primary font-medium"),children:y.label},y.id)),!se&&A.length===0&&e.jsx("p",{className:"text-xs text-muted-foreground py-1 px-2",children:t("common.no_results")})]}),w&&e.jsxs("p",{className:"text-xs text-muted-foreground mt-1.5",children:[t("tasks.linked_to")," ",e.jsx("span",{className:"font-medium text-foreground",children:w})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(C,{children:t("tasks.due_date")}),e.jsxs(he,{children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{variant:"outline",className:M("w-full justify-start text-left font-normal",!v&&"text-muted-foreground"),children:[e.jsx(Z,{className:"h-4 w-4 mr-2"}),v?R(v,"PPP"):t("tasks.pick_date")]})}),e.jsx(fe,{className:"w-auto p-0",align:"start",children:e.jsx(ge,{mode:"single",selected:v,onSelect:j,initialFocus:!0,className:"p-3 pointer-events-auto"})})]})]}),e.jsxs("div",{children:[e.jsx(C,{children:t("tasks.assignee")}),e.jsxs(P,{value:D,onValueChange:N,children:[e.jsx(q,{children:e.jsx($,{placeholder:t("tasks.unassigned")})}),e.jsxs(L,{children:[e.jsx(h,{value:"__unassigned__",children:t("tasks.unassigned")}),l.map(y=>e.jsx(h,{value:y.id,children:y.name},y.id))]})]})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(E,{variant:"outline",onClick:()=>i(!1),children:t("common.cancel")}),e.jsxs(E,{onClick:we,disabled:!d||!n.trim()||a.isPending,children:[a.isPending&&e.jsx(F,{className:"h-4 w-4 animate-spin mr-1"}
1),t("tasks.create_task")]})]})]})]})})}function js(s){const{canView:i}=U();return X({queryKey:["task-comments",s],enabled:!!s&&i,queryFn:async()=>{const{data:d,error:a}=await g.from("task_comments").select("*, author:profiles!task_comments_author_id_fkey(name, email)").eq("task_id",s).order("created_at",{ascending:!0});if(a)throw a;return d??[]}})}function ys(){const s=B(),{user:i}=z(),{canEdit:d}=U();return V({mutationFn:async({taskId:a,body:l})=>{if(!d)throw new Error("You do not have permission to comment on tasks.");const{data:t,error:n}=await g.from("task_comments").insert({task_id:a,author_id:i.id,body:l}).select().single();if(n)throw n;const u=/@\[.*?\]\(profile:([\w-]+)\)/g;let r;const o=new Set;for(;(r=u.exec(l))!==null;)r[1]!==i.id&&o.add(r[1]);if(o.size>0){const{data:c}=await g.from("tasks").select("title").eq("id",a).single(),m=Array.from(o);let f={};try{f=await ee(m,a)}catch{f={}}const _=m.map(j=>({user_id:j,type:"task",title:"You were mentioned in a task comment",message:(c==null?void 0:c.title)??"Task comment",link:f[j]??`/admin/tasks?task=${a}`,created_by:i.id,event_key:`task-mention-${t.id}-${j}`})),v=await ts(_);v.length>0&&await g.from("notifications").insert(v)}return t},onSuccess:(a,l)=>{s.invalidateQueries({queryKey:["task-comments",l.taskId]})}})}function _s({value:s,onChange:i,placeholder:d,className:a}){const[l,t]=p.useState(!1),[n,u]=p.useState([]),[r,o]=p.useState(""),[c,m]=p.useState(0),f=p.useRef(null);p.useEffect(()=>{if(!l)return;(async()=>{const{data:D}=await g.from("user_roles").select("user_id").in("role",["admin","staff","volunteer"]),N=[...new Set((D??[]).map(w=>w.user_id))];if(N.length===0){u([]);return}let k=g.from("profiles").select("id, name, email").in("id",N).order("updated_at",{ascending:!1}).limit(10);if(r){const w=`%${r}%`;k=k.or(`name.ilike.${w},email.ilike.${w}`)}const{data:b}=await k;u((b??[]).map(w=>({id:w.id,name:w.name,email:w.email})))})()},[l,r]);const _=j=>{const D=j.target.value,N=j.target.selectionStart;i(D),m(N);const k=D.slice(0,N),b=k.lastIndexOf("@");b>=0&&!k.slice(b).includes(" ")&&b===k.length-1||b>=0&&!k.slice(b+1).includes(" ")?(t(!0),o(k.slice(b+1))):t(!1)},v=j=>{const N=s.slice(0,c).lastIndexOf("@"),k=s.slice(0,N),b=s.slice(c),w=`@[${j.name}](profile:${j.id}) `;i(k+w+b),t(!1),setTimeout(()=>{var S;return(S=f.current)==null?void 0:S.focus()},50)};return e.jsxs("div",{className:"relative",children:[e.jsx(ue,{ref:f,value:s,onChange:_,placeholder:d,className:a,rows:3}),l&&n.length>0&&e.jsx("div",{className:"absolute z-50 bottom-full mb-1 left-0 w-64 bg-popover border rounded-lg shadow-lg p-1 max-h-40 overflow-y-auto",children:n.map(j=>e.jsxs("button",{type:"button",onClick:()=>v(j),className:"w-full text-left text-sm px-3 py-2 rounded-md hover:bg-muted transition-colors",children:[j.name,j.email?e.jsx("span",{className:"block text-xs text-muted-foreground",children:j.email}):null]},j.id))})]})}function ws({taskId:s,canComment:i=!0}){const{data:d=[],isLoading:a}=js(s),l=ys(),[t,n]=p.useState(""),u=async()=>{if(t.trim())try{await l.mutateAsync({taskId:s,body:t.trim()}),n("")}catch(o){T.error(o instanceof Error?o.message:"Failed to add comment")}},r=o=>o.replace(/@\[(.*?)\]\(profile:[\w-]+\)/g,(c,m)=>`@${m}`);return e.jsxs("div",{className:"space-y-4",children:[e.jsx("h4",{className:"text-sm font-medium",children:"Comments"}),a&&e.jsx(F,{className:"h-4 w-4 animate-spin"}),e.jsx("div",{className:"space-y-3 max-h-60 overflow-y-auto",children:d.map(o=>{var c;return e.jsxs("div",{className:"text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-0.5",children:[e.jsx("span",{className:"font-medium text-foreground",children:((c=o.author)==null?void 0:c.name)??"Unknown"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:R(new Date(o.created_at),"MMM d, h:mm a")})]}),e.jsx("p",{className:"text-muted-foreground whitespace-pre-wrap",children:r(o.body)})]},o.id)})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(_s,{value:t,onChange:n,placeholder:"Add a comment... Use @ to mention someone",className:"flex-1 min-h-[60px]"}),e.jsx(E,{size:"icon",onClick:u,disabled:!i||!t.trim()||l.isPending,className:"shrink-0 self-end",children:l.isPending?e.jsx(F,{className:"h-4 w-4 animate-spin"}):e.jsx(Ie,{className:"h-4 w-4"})})]})]})}function vs(s){const{canView:i}=U();return X({queryKey:["task-attachments",s],enabled:!!s&&i,queryFn:async()=>{const{data:d,error:a}=await g.from("task_attachments").select("*").eq("task_id",s).order("created_at",{ascending:!1});if(a)throw a;return d??[]}})}function ks(){const s=B(),{user:i}=z(),{canEdit:d}=U();return V({mutationFn:async({taskId:a,file:l})=>
1{if(!d)throw new Error("You do not have permission to upload task attachments.");if(l.size>5*1024*1024)throw new Error("File must be under 5MB");const t=l.name.split(".").pop(),n=`${a}/${crypto.randomUUID()}.${t}`,{error:u}=await g.storage.from("task-attachments").upload(n,l);if(u)throw u;const{data:r,error:o}=await g.from("task_attachments").insert({task_id:a,uploaded_by:i.id,file_name:l.name,file_url:n,file_size:l.size}).select().single();if(o)throw o;return r},onSuccess:(a,l)=>{s.invalidateQueries({queryKey:["task-attachments",l.taskId]})}})}function bs(){const s=B(),{canEdit:i}=U();return V({mutationFn:async({id:d,taskId:a,fileUrl:l})=>{if(!i)throw new Error("You do not have permission to remove task attachments.");await g.storage.from("task-attachments").remove([l]);const{error:t}=await g.from("task_attachments").delete().eq("id",d);if(t)throw t;return a},onSuccess:(d,a)=>{s.invalidateQueries({queryKey:["task-attachments",a.taskId]})}})}async function Ns(s){const{data:i,error:d}=await g.storage.from("task-attachments").createSignedUrl(s,3600);if(d)throw d;return i.signedUrl}function Cs(s){return s<1024?`${s} B`:s<1024*1024?`${(s/1024).toFixed(1)} KB`:`${(s/(1024*1024)).toFixed(1)} MB`}function Ss({taskId:s,canManage:i=!0}){const{data:d=[],isLoading:a}=vs(s),l=ks(),t=bs(),n=p.useRef(null),u=async c=>{var f;const m=(f=c.target.files)==null?void 0:f[0];if(m){if(m.size>5*1024*1024){T.error("File must be under 5MB");return}try{await l.mutateAsync({taskId:s,file:m}),T.success("File uploaded")}catch(_){T.error(_ instanceof Error?_.message:"Upload failed")}n.current&&(n.current.value="")}},r=async(c,m)=>{try{const f=await Ns(c),_=document.createElement("a");_.href=f,_.download=m,_.click()}catch{T.error("Download failed")}},o=async(c,m)=>{try{await t.mutateAsync({id:c,taskId:s,fileUrl:m}),T.success("Attachment removed")}catch(f){T.error(f instanceof Error?f.message:"Failed to remove attachment")}};return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("h4",{className:"text-sm font-medium flex items-center gap-1.5",children:[e.jsx(oe,{className:"h-4 w-4"})," Attachments"]}),e.jsxs(E,{variant:"outline",size:"sm",onClick:()=>{var c;return i&&((c=n.current)==null?void 0:c.click())},disabled:!i||l.isPending,children:[l.isPending?e.jsx(F,{className:"h-3 w-3 animate-spin mr-1"}):e.jsx(ze,{className:"h-3 w-3 mr-1"}),"Upload"]}),e.jsx("input",{ref:n,type:"file",className:"hidden",onChange:u})]}),a&&e.jsx(F,{className:"h-4 w-4 animate-spin"}),e.jsxs("div",{className:"space-y-2",children:[d.map(c=>e.jsxs("div",{className:"flex items-center gap-2 text-sm p-2 rounded-lg border bg-muted/30",children:[e.jsx(oe,{className:"h-3.5 w-3.5 text-muted-foreground shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"truncate text-foreground",children:c.file_name}),e.jsxs("p",{className:"text-[10px] text-muted-foreground",children:[Cs(c.file_size)," · ",R(new Date(c.created_at),"MMM d")]})]}),e.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7",onClick:()=>r(c.file_url,c.file_name),children:e.jsx(rs,{className:"h-3.5 w-3.5"})}),e.jsx(E,{variant:"ghost",size:"icon",className:"h-7 w-7 text-destructive",onClick:()=>o(c.id,c.file_url),disabled:!i,children:e.jsx(Be,{className:"h-3.5 w-3.5"})})]},c.id)),!a&&d.length===0&&e.jsx("p",{className:"text-xs text-muted-foreground",children:"No attachments yet"})]})]})}function Ts({task:s,open:i,onOpenChange:d,canEdit:a=!0,canDelete:l=!0}){var c;const t=ds(),n=ms(),{data:u=[]}=J();if(!s)return null;const r=async m=>{try{if(!a)return;await t.mutateAsync({id:s.id,...m})}catch(f){T.error(f instanceof Error?f.message:"Failed to update task")}},o=async()=>{if(confirm("Delete this task permanently?"))try{if(!l)return;await n.mutateAsync(s.id),d(!1),T.success("Task deleted")}catch(m){T.error(m instanceof Error?m.message:"Failed to delete task")}};return e.jsx(Ve,{open:i,onOpenChange:d,children:e.jsxs(Oe,{className:"w-full sm:max-w-lg overflow-y-auto",children:[e.jsx(Ke,{className:"pr-6",children:e.jsx(Qe,{className:"text-lg",children:s.title})}),e.jsxs("div",{className:"space-y-5 mt-4",children:[s.description&&e.jsx("p",{className:"text-sm text-muted-foreground whitespace-pre-wrap",children:s.description}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(C,{className:"text-xs text-muted-foreground mb-1.5 block",children:"Status"}),e.jsxs(P,{value:s.status,onValueChange:m=>r({status:m}),disabled:!a,children:[e.jsx(q,{children:e.jsx($,{})}),e.jsxs(L,{children:[e.jsx(h,{value:"open",children:"Open"}),e.jsx(h,{value:"in_progress",children:"In Progress"}),e.jsx(h,{value:"blocked",children:"Blocked"}),e.jsx(h,{value:"completed",children:"Completed"})]})]})]}),e.jsxs("div",{children:[e.jsx(C,{className:"text-xs text-muted-foreground mb-1.5 block",children:"Priority"}),e.jsxs(P,{value:s.priority,onValueChange:m=>r({priority:m}),disabled:!a,children:[e.jsx(q,{children:e.jsx($,{})}),e.jsxs(L,{children:[e.jsx(h,{value:"low",children:"Low"}),e.jsx(h,{value:"medium",children:"Medium"}),e.jsx(h,{value:"high",children:"High"}),e.jsx(h,{value:"urgent",children:"Urgent"})]})]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(C,{className:"text-xs text-muted-foreground mb-1.5 block",children:"Assignee"}),e.jsxs(P,{value:s.assignee_id??"__unassigned__",onValueChange:m=>
1r({assignee_id:m==="__unassigned__"?null:m}),disabled:!a,children:[e.jsx(q,{children:e.jsx($,{placeholder:"Unassigned"})}),e.jsxs(L,{children:[e.jsx(h,{value:"__unassigned__",children:"Unassigned"}),u.map(m=>e.jsx(h,{value:m.id,children:m.name},m.id))]})]})]}),e.jsxs("div",{children:[e.jsx(C,{className:"text-xs text-muted-foreground mb-1.5 block",children:"Due Date"}),e.jsxs(he,{children:[e.jsx(xe,{asChild:!0,children:e.jsxs(E,{variant:"outline",className:M("w-full justify-start text-left font-normal",!s.due_date&&"text-muted-foreground"),disabled:!a,children:[e.jsx(Z,{className:"h-4 w-4 mr-2"}),s.due_date?R(new Date(s.due_date),"PPP"):"No date"]})}),e.jsx(fe,{className:"w-auto p-0",align:"start",children:e.jsx(ge,{mode:"single",selected:s.due_date?new Date(s.due_date):void 0,onSelect:m=>a&&r({due_date:m?R(m,"yyyy-MM-dd"):null}),initialFocus:!0,className:"p-3 pointer-events-auto"})})]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(C,{className:"text-xs text-muted-foreground",children:"Category"}),e.jsx(Y,{variant:"outline",className:"capitalize",children:s.category})]}),s.related_label&&(()=>{const m=je(s.related_type??null,s.related_id??null);return e.jsxs("div",{className:"space-y-1",children:[e.jsx(C,{className:"text-xs text-muted-foreground",children:"Related To"}),e.jsxs("p",{className:"text-sm",children:[e.jsx(Y,{variant:"outline",className:"capitalize mr-1",children:s.related_type}),m?e.jsx(me,{to:m,className:"text-primary hover:underline",children:s.related_label}):s.related_label]})]})})(),e.jsxs("div",{className:"text-[10px] text-muted-foreground space-y-0.5",children:[e.jsxs("p",{children:["Created by ",((c=s.creator)==null?void 0:c.name)??"Unknown"," on ",R(new Date(s.created_at),"PPP")]}),s.completed_at&&e.jsxs("p",{children:["Completed ",R(new Date(s.completed_at),"PPP")]})]}),e.jsx(H,{}),e.jsx(Ss,{taskId:s.id,canManage:a}),e.jsx(H,{}),e.jsx(ws,{taskId:s.id,canComment:a}),e.jsx(H,{}),e.jsx(E,{variant:"destructive",size:"sm",onClick:o,className:"w-full",disabled:!l,children:"Delete Task"})]})]})})}function Fs({basePath:s="/admin"}){const[i]=Ye(),[d,a]=p.useState("my-tasks"),[l,t]=p.useState(!1),[n,u]=p.useState(null),[r,o]=p.useState({assignee:"all",status:"all",priority:"all",dueDate:"all",category:"all"}),c={tab:d,assignee:r.assignee,status:r.status,priority:r.priority,dueDate:r.dueDate,category:r.category},m=i.get("task"),{data:f}=W(m?{tab:"team-tasks"}:c),{data:_}=W(m?{tab:"completed"}:{tab:"completed"}),{data:v=[],isLoading:j}=W(c),{data:D=[]}=J(),{canCreate:N,canEdit:k,canDelete:b}=U(),w=p.useRef(null);p.useEffect(()=>{if(m&&m!==w.current){const A=[...f??[],..._??[]].find(I=>I.id===m);A&&(u(A),w.current=m,A.status==="completed"&&a("completed"))}},[m,f,_]),p.useEffect(()=>{if(n&&v.length>0){const x=v.find(A=>A.id===n.id);x&&u(x)}},[v]);const S=(x,A)=>{o(I=>({...I,[x]:A}))},O={"my-tasks":"You have no tasks assigned to you.","team-tasks":"No team tasks have been created yet.",completed:"No completed tasks to display."};return e.jsxs("div",{className:"space-y-6",children:[e.jsx(He,{children:e.jsxs(Ge,{children:[e.jsx(ie,{children:e.jsx(We,{href:s,children:"Dashboard"})}),e.jsx(Xe,{}),e.jsx(ie,{children:e.jsx(Je,{children:"Tasks"})})]})}),e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"section-title-h1 text-foreground",children:"Tasks"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Manage and track work across your organization"})]}),e.jsxs(E,{onClick:()=>N&&t(!0),className:"gap-2",disabled:!N,children:[e.jsx(Ze,{className:"h-4 w-4"})," Create Task"]})]}),e.jsxs(es,{value:d,onValueChange:x=>a(x),children:[e.jsxs(ss,{children:[e.jsx(G,{value:"my-tasks",children:"My Tasks"}),e.jsx(G,{value:"team-tasks",children:"Team Tasks"}),e.jsx(G,{value:"completed",children:"Completed"})]}),e.jsxs("div",{className:"flex flex-wrap gap-3 mt-4",children:[e.jsxs(P,{value:r.assignee,onValueChange:x=>S("assignee",x),children:[e.jsx(q,{className:"w-[160px]",children:e.jsx($,{placeholder:"Assignee"})}),e.jsxs(L,{children:[e.jsx(h,{value:"all",children:"All Assignees"}),e.jsx(h,{value:"me",children:"Assigned to Me"}),e.jsx(h,{value:"unassigned",children:"Unassigned"}),D.map(x=>e.jsx(h,{value:x.id,children:x.name},x.id))]})]}),e.jsxs(P,{value:r.status,onValueChange:x=>S("status",x),children:[e.jsx(q,{className:"w-[140px]",children:e.jsx($,{placeholder:"Status"})}),e.jsxs(L,{children:[e.jsx(h,{value:"all",children:"All Statuses"}),e.jsx(h,{value:"open",children:"Open"}),e.jsx(h,{value:"in_progress",children:"In Progress"}),e.jsx(h,{value:"blocked",children:"Blocked"})]})]}),e.jsxs(P,{value:r.priority,onValueChange:x=>S("priority",x),children:[e.jsx(q,{className:"w-[130px]",children:e.jsx($,{placeholder:"Priority"})}),e.jsxs(L,{children:[e.jsx(h,{value:"all",children:"All Priorities"}),e.jsx(h,{value:"urgent",children:"Urgent"}),e.jsx(h,{value:"high",children:"High"}),e.jsx(h,{value:"medium",children:"Medium"}),e.jsx(h,{value:"low",children:"Low"})]})]}),e.jsxs(P,{value:r.dueDate,onValueChange:x=>S("dueDate",x),children:[e.jsx(q,{className:"w-[150px]",children:e.jsx($,{placeholder:"Due Date"})}),e.jsxs(L,{children:[e.jsx(h,{value:"all",children:"All Due Dates"}),e.jsx(h,{value:"overdue",children:"Overdue"}),e.jsx(h,{value:"today",children:"Today"}),e.jsx(h,{value:"this-week",children:"This Week"}),e.jsx(h,{value:"no-due-date",children:"No Due Date"})]})]}),e.jsxs(P,{value:r.category,onValueChange:x=>S("category",x),children:[e.jsx(q,{className:"w-[150px]",children:e.jsx($,{placeholder:"Category"})}),e.jsxs(L,{children:[e.jsx(h,{value:"all",children:"All Categories"}),e.jsx(h,{value:"general",children:"General"}),e.jsx(h,{value:"donor",children:"Supporter"}),e.jsx(h,{value:"beneficiary",children:"Sponsorship"}),e.jsx(h,{value:"campaign",children:"Campaign"}),e.jsx(h,{value:"message",children:"Message"}),e.jsx(h,{value:"gift",children:"Gift"}),e.jsx(h,{value:"receipt",children:"Receipt"}),e.jsx(h,{value:"sponsorship",children:"Sponsorship"}),e.jsx(h,{value:"workflow",children:"Workflow"})]})]})]}),["my-tasks","team-tasks","completed"].map(x=>e.jsx(as,{value:x,className:"mt-6",children:e.jsx(hs,{tasks:v,isLoading:j,emptyMessage:O[x],onTaskClick:u})},x))]}),e.jsx(gs,{open:l,onOpenChange:t,canCreate:N}),e.jsx(Ts,{task:n,open:!!n,onOpenChange:x=>!x&&u(null),canEdit:k,canDelete:b})]})}
1export{Fs as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.