|
| 1 | +import { useEffect, useLayoutEffect, useRef, useState } from "react"; |
| 2 | +import { useKgPlanning } from "../store"; |
| 3 | +import { callMethod, fetchPrivateFileUrl } from "../bridge"; |
| 4 | +import type { EmployeeData } from "../types"; |
| 5 | + |
| 6 | +interface Props { |
| 7 | + taskName: string; |
| 8 | + taskSubject: string; |
| 9 | + assignedEmployees: string[]; |
| 10 | + anchor: { x: number; y: number }; |
| 11 | + onClose: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Inline popup for adding/removing assignees on a Task via Frappe's |
| 16 | + * `frappe.desk.form.assign_to` endpoints. Positioned against a viewport |
| 17 | + * anchor with edge-clamping so it stays fully visible when opened near |
| 18 | + * the right or bottom edge of the grid. |
| 19 | + */ |
| 20 | +export default function AssignPopup({ taskName, taskSubject, assignedEmployees, anchor, onClose }: Props) { |
| 21 | + const { data, reload } = useKgPlanning(); |
| 22 | + const employees = data?.employees ?? []; |
| 23 | + const rootRef = useRef<HTMLDivElement | null>(null); |
| 24 | + const [busy, setBusy] = useState<string | null>(null); |
| 25 | + const [assigned, setAssigned] = useState<Set<string>>(new Set(assignedEmployees)); |
| 26 | + const [pos, setPos] = useState<{ left: number; top: number }>({ left: anchor.x, top: anchor.y }); |
| 27 | + |
| 28 | + // Sync local optimistic set if the parent-provided list changes under us |
| 29 | + // (e.g. after reload() completes). |
| 30 | + useEffect(() => { |
| 31 | + setAssigned(new Set(assignedEmployees)); |
| 32 | + }, [assignedEmployees]); |
| 33 | + |
| 34 | + // Outside-click + Escape close. Registered once per open. |
| 35 | + useEffect(() => { |
| 36 | + const onDown = (e: MouseEvent) => { |
| 37 | + if (rootRef.current && !rootRef.current.contains(e.target as Node)) onClose(); |
| 38 | + }; |
| 39 | + const onKey = (e: KeyboardEvent) => { |
| 40 | + if (e.key === "Escape") onClose(); |
| 41 | + }; |
| 42 | + window.addEventListener("mousedown", onDown); |
| 43 | + window.addEventListener("keydown", onKey); |
| 44 | + return () => { |
| 45 | + window.removeEventListener("mousedown", onDown); |
| 46 | + window.removeEventListener("keydown", onKey); |
| 47 | + }; |
| 48 | + }, [onClose]); |
| 49 | + |
| 50 | + // Clamp to viewport after first layout — reading offsetWidth/Height is |
| 51 | + // only reliable once React has committed the DOM. |
| 52 | + useLayoutEffect(() => { |
| 53 | + const el = rootRef.current; |
| 54 | + if (!el) return; |
| 55 | + const margin = 8; |
| 56 | + const w = el.offsetWidth; |
| 57 | + const h = el.offsetHeight; |
| 58 | + let left = anchor.x; |
| 59 | + let top = anchor.y; |
| 60 | + if (left + w > window.innerWidth - margin) left = Math.max(margin, window.innerWidth - w - margin); |
| 61 | + if (top + h > window.innerHeight - margin) top = Math.max(margin, window.innerHeight - h - margin); |
| 62 | + setPos({ left, top }); |
| 63 | + }, [anchor.x, anchor.y]); |
| 64 | + |
| 65 | + async function toggle(emp: EmployeeData) { |
| 66 | + if (!emp.user_id) return; |
| 67 | + if (busy) return; |
| 68 | + const wasAssigned = assigned.has(emp.user_id); |
| 69 | + // Optimistic toggle so the checkbox flips immediately. |
| 70 | + setAssigned((prev) => { |
| 71 | + const next = new Set(prev); |
| 72 | + if (wasAssigned) next.delete(emp.user_id!); |
| 73 | + else next.add(emp.user_id!); |
| 74 | + return next; |
| 75 | + }); |
| 76 | + setBusy(emp.user_id); |
| 77 | + try { |
| 78 | + if (wasAssigned) { |
| 79 | + await callMethod("frappe.desk.form.assign_to.remove", { |
| 80 | + doctype: "Task", |
| 81 | + name: taskName, |
| 82 | + assign_to: emp.user_id, |
| 83 | + }); |
| 84 | + } else { |
| 85 | + await callMethod("frappe.desk.form.assign_to.add", { |
| 86 | + assign_to: JSON.stringify([emp.user_id]), |
| 87 | + doctype: "Task", |
| 88 | + name: taskName, |
| 89 | + description: taskSubject, |
| 90 | + }); |
| 91 | + } |
| 92 | + await reload(); |
| 93 | + } catch (err) { |
| 94 | + // Roll back the optimistic flip on failure. |
| 95 | + setAssigned((prev) => { |
| 96 | + const next = new Set(prev); |
| 97 | + if (wasAssigned) next.add(emp.user_id!); |
| 98 | + else next.delete(emp.user_id!); |
| 99 | + return next; |
| 100 | + }); |
| 101 | + console.error("[kg-planning] assign toggle failed:", err); |
| 102 | + alert(err instanceof Error ? err.message : String(err)); |
| 103 | + } finally { |
| 104 | + setBusy(null); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return ( |
| 109 | + <div |
| 110 | + ref={rootRef} |
| 111 | + style={{ position: "fixed", left: pos.left, top: pos.top, width: 240, maxHeight: 380, zIndex: 1000 }} |
| 112 | + className="bg-white border border-slate-200 rounded-md shadow-lg flex flex-col overflow-hidden" |
| 113 | + > |
| 114 | + <div className="px-3 py-2 bg-slate-50 border-b border-slate-200 text-xs font-semibold text-slate-700 flex items-center justify-between"> |
| 115 | + <span className="truncate" title={taskSubject}>Toewijzen aan</span> |
| 116 | + <button onClick={onClose} className="text-slate-400 hover:text-slate-700 cursor-pointer" aria-label="Close">×</button> |
| 117 | + </div> |
| 118 | + <ul className="flex-1 overflow-y-auto py-1"> |
| 119 | + {employees.map((e) => ( |
| 120 | + <AssignRow |
| 121 | + key={e.name} |
| 122 | + emp={e} |
| 123 | + checked={!!e.user_id && assigned.has(e.user_id)} |
| 124 | + disabled={!e.user_id || busy === e.user_id} |
| 125 | + onToggle={() => toggle(e)} |
| 126 | + /> |
| 127 | + ))} |
| 128 | + </ul> |
| 129 | + </div> |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +function AssignRow({ emp, checked, disabled, onToggle }: { |
| 134 | + emp: EmployeeData; |
| 135 | + checked: boolean; |
| 136 | + disabled: boolean; |
| 137 | + onToggle: () => void; |
| 138 | +}) { |
| 139 | + const [avatar, setAvatar] = useState<string | null>(null); |
| 140 | + useEffect(() => { |
| 141 | + let cancelled = false; |
| 142 | + fetchPrivateFileUrl(emp.image).then((url) => { |
| 143 | + if (!cancelled) setAvatar(url); |
| 144 | + }); |
| 145 | + return () => { cancelled = true; }; |
| 146 | + }, [emp.image]); |
| 147 | + |
| 148 | + return ( |
| 149 | + <li> |
| 150 | + <button |
| 151 | + onClick={onToggle} |
| 152 | + disabled={disabled} |
| 153 | + className={`w-full flex items-center gap-2 px-3 py-1.5 text-xs text-left ${ |
| 154 | + disabled ? "opacity-50 cursor-not-allowed" : "hover:bg-slate-50 cursor-pointer" |
| 155 | + }`} |
| 156 | + title={emp.user_id ?? "No linked user"} |
| 157 | + > |
| 158 | + <input type="checkbox" checked={checked} readOnly className="cursor-pointer" /> |
| 159 | + {avatar ? ( |
| 160 | + <img src={avatar} alt="" className="w-6 h-6 rounded-full object-cover border border-slate-200" /> |
| 161 | + ) : ( |
| 162 | + <div className="w-6 h-6 rounded-full bg-slate-200 flex items-center justify-center text-[10px] text-slate-500"> |
| 163 | + {initials(emp.employee_name)} |
| 164 | + </div> |
| 165 | + )} |
| 166 | + <span className="flex-1 truncate">{emp.employee_name}</span> |
| 167 | + </button> |
| 168 | + </li> |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +function initials(name: string): string { |
| 173 | + const parts = name.trim().split(/\s+/).filter(Boolean); |
| 174 | + if (parts.length === 0) return "?"; |
| 175 | + if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase(); |
| 176 | + return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase(); |
| 177 | +} |
0 commit comments