File size: 17,756 Bytes
612f57c
c5eff7f
612f57c
20df2a6
 
 
612f57c
 
 
 
 
 
 
 
 
 
 
a141ee7
 
 
 
 
 
20df2a6
 
 
 
 
 
 
 
612f57c
 
20df2a6
 
 
 
 
 
 
 
 
 
a141ee7
20df2a6
 
a141ee7
 
eed056f
 
 
 
 
 
20df2a6
 
 
612f57c
c5eff7f
 
 
612f57c
 
 
20df2a6
 
 
 
 
 
 
 
a141ee7
20df2a6
 
a141ee7
 
eed056f
 
 
 
 
 
20df2a6
 
 
c5eff7f
a141ee7
612f57c
 
20df2a6
612f57c
 
 
 
c62e893
612f57c
 
 
 
 
 
 
 
c5eff7f
eed056f
612f57c
 
 
 
20df2a6
 
 
 
 
c5eff7f
 
 
 
 
 
20df2a6
c5eff7f
 
 
 
 
20df2a6
 
 
 
a141ee7
c5eff7f
eed056f
a141ee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
612f57c
 
 
 
 
 
 
 
 
c5eff7f
a141ee7
 
 
 
 
 
 
 
 
 
 
612f57c
 
 
 
 
 
 
 
 
 
 
20df2a6
612f57c
 
20df2a6
 
612f57c
c5eff7f
612f57c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c62e893
 
 
 
 
c5eff7f
c62e893
 
c5eff7f
 
 
 
 
 
 
 
 
 
612f57c
 
 
 
c5eff7f
 
a141ee7
 
 
 
 
 
 
 
 
 
 
20df2a6
c5eff7f
 
20df2a6
 
 
c5eff7f
612f57c
c5eff7f
 
 
 
 
 
 
 
 
 
 
612f57c
 
c5eff7f
 
 
 
612f57c
c5eff7f
612f57c
c5eff7f
612f57c
 
 
 
c62e893
 
 
 
 
612f57c
 
 
20df2a6
 
 
612f57c
 
 
c5eff7f
 
 
 
 
 
 
 
 
 
20df2a6
612f57c
c5eff7f
 
 
c62e893
 
 
 
 
 
 
c5eff7f
c62e893
 
 
612f57c
 
 
 
20df2a6
 
612f57c
a141ee7
c5eff7f
 
 
612f57c
 
 
c5eff7f
 
612f57c
 
 
 
c5eff7f
612f57c
 
c5eff7f
612f57c
 
 
 
 
c5eff7f
612f57c
 
 
 
 
 
 
 
 
 
 
c5eff7f
 
 
 
 
 
 
 
 
a141ee7
 
 
c5eff7f
a141ee7
 
 
c5eff7f
 
612f57c
c5eff7f
 
 
 
 
eed056f
c5eff7f
612f57c
 
 
 
c5eff7f
 
 
 
 
 
 
 
 
 
 
 
eed056f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5eff7f
 
 
eed056f
 
 
c5eff7f
eed056f
c5eff7f
eed056f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5eff7f
eed056f
c5eff7f
 
 
 
612f57c
c5eff7f
612f57c
 
 
 
 
 
 
20df2a6
 
 
 
 
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { addWeeks, format, parseISO } from "date-fns";
import { Calendar, ChevronRight, HelpCircle, MoreHorizontal } from "lucide-react";
import { useEffect, useMemo, useRef, useState } from "react";
import { createPortal } from "react-dom";

import {
  allocationPctFromEffort,
  effortValueFromPct,
  endDateFromWeekdayCount,
  formatHoursClock,
  parseHoursClock,
  totalEffortDays,
  totalEffortHours,
  type AllocationRepeatConfig,
  type EffortUnit,
  type TotalEffortUnit,
} from "../../planner/allocationRepeat";
import {
  hoursPerDayFromAllocation,
  roundToHalfHour,
  visibleWeekdayCount,
} from "../../planner/allocationTimeline";
import type { Allocation, Person } from "../../types";

export interface AllocationDragDraft {
  person_id: number;
  start_date: string;
  end_date: string;
  allocation_pct: number;
  hours_per_day: number;
  work_days: number;
  note?: string;
  repeat?: AllocationRepeatConfig;
}

interface AllocationCreatePopoverProps {
  anchor: { top: number; left: number };
  dayDates: Date[];
  startDate: string;
  endDate: string;
  people: Person[];
  fixedPerson?: Person;
  defaultHoursPerDay?: number;
  allocation?: Allocation;
  onCancel: () => void;
  onSave: (draft: AllocationDragDraft) => void;
  onUpdate?: (draft: AllocationDragDraft) => void;
  onDelete?: () => void;
  transferPeople?: Person[];
  onTransfer?: (personId: number) => void;
  onClone?: () => void;
  onSelectAllToRight?: () => void;
  onToggleMultiSelect?: () => void;
  multiSelectActive?: boolean;
  isSaving?: boolean;
}

const EFFORT_UNITS: Array<{ value: EffortUnit; label: string }> = [
  { value: "hours_per_day", label: "h/d" },
  { value: "hours_per_week", label: "h/wk" },
  { value: "capacity_pct", label: "%" },
  { value: "fte", label: "FTE" },
];

export function AllocationCreatePopover({
  anchor,
  dayDates,
  startDate,
  endDate,
  people,
  fixedPerson,
  defaultHoursPerDay,
  allocation,
  onCancel,
  onSave,
  onUpdate,
  onDelete,
  transferPeople = [],
  onTransfer,
  onClone,
  onSelectAllToRight,
  onToggleMultiSelect,
  multiSelectActive = false,
  isSaving = false,
}: AllocationCreatePopoverProps) {
  const popoverRef = useRef<HTMLDivElement>(null);
  const menuRef = useRef<HTMLDivElement>(null);
  const isEdit = Boolean(allocation);
  const dragWorkDays = visibleWeekdayCount(dayDates, startDate, endDate);

  const [personId, setPersonId] = useState<number | "">(fixedPerson?.id ?? people[0]?.id ?? "");
  const [rangeStart, setRangeStart] = useState(startDate);
  const [rangeEnd, setRangeEnd] = useState(endDate);
  const [workDays, setWorkDays] = useState(Math.max(1, dragWorkDays));
  const [effortUnit, setEffortUnit] = useState<EffortUnit>("hours_per_day");
  const [effortInput, setEffortInput] = useState("8:00");
  const [totalUnit, setTotalUnit] = useState<TotalEffortUnit>("hours");
  const [repeatEnabled, setRepeatEnabled] = useState(false);
  const [repeatEndMode, setRepeatEndMode] = useState<"on" | "after">("on");
  const [repeatEndOn, setRepeatEndOn] = useState(format(addWeeks(parseISO(startDate), 4), "yyyy-MM-dd"));
  const [repeatOccurrences, setRepeatOccurrences] = useState(4);
  const [noteOpen, setNoteOpen] = useState(false);
  const [note, setNote] = useState("");
  const [datesOpen, setDatesOpen] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);
  const [transferOpen, setTransferOpen] = useState(false);

  const selectedPerson =
    fixedPerson ?? people.find((person) => person.id === Number(personId));
  const capacityDaily = selectedPerson ? roundToHalfHour(selectedPerson.weekly_capacity_hrs / 5) : 8;

  useEffect(() => {
    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape") onCancel();
    };
    const onDown = (event: MouseEvent) => {
      const target = event.target as Node;
      if (popoverRef.current?.contains(target)) return;
      if (menuRef.current?.contains(target)) return;
      onCancel();
    };
    window.addEventListener("keydown", onKey);
    document.addEventListener("mousedown", onDown);
    return () => {
      window.removeEventListener("keydown", onKey);
      document.removeEventListener("mousedown", onDown);
    };
  }, [onCancel]);

  useEffect(() => {
    setPersonId(fixedPerson?.id ?? people[0]?.id ?? "");
    setDatesOpen(false);
    setMenuOpen(false);
    setTransferOpen(false);

    const editPerson = allocation
      ? (fixedPerson ?? people.find((person) => person.id === allocation.person_id))
      : undefined;

    if (allocation && editPerson) {
      const savedWorkDays = visibleWeekdayCount(
        dayDates,
        allocation.start_date,
        allocation.end_date,
      );
      const savedHoursPerDay = hoursPerDayFromAllocation(
        allocation.allocation_pct,
        editPerson.weekly_capacity_hrs,
      );
      setRangeStart(allocation.start_date);
      setRangeEnd(allocation.end_date);
      setWorkDays(Math.max(1, savedWorkDays));
      setRepeatEnabled(false);
      setNote(allocation.note ?? "");
      setNoteOpen(Boolean(allocation.note));
      setEffortUnit("hours_per_day");
      setEffortInput(formatHoursClock(savedHoursPerDay));
      return;
    }

    setRangeStart(startDate);
    setRangeEnd(endDate);
    setWorkDays(Math.max(1, dragWorkDays));
    setRepeatEndOn(format(addWeeks(parseISO(startDate), 4), "yyyy-MM-dd"));
    setRepeatOccurrences(4);
    setRepeatEnabled(false);
    setNote("");
    setNoteOpen(false);
    setEffortUnit("hours_per_day");
    setEffortInput(formatHoursClock(defaultHoursPerDay ?? capacityDaily));
  }, [
    allocation,
    fixedPerson,
    people,
    defaultHoursPerDay,
    capacityDaily,
    startDate,
    endDate,
    dragWorkDays,
    dayDates,
  ]);

  useEffect(() => {
    setRangeEnd(endDateFromWeekdayCount(rangeStart, workDays));
  }, [rangeStart, workDays]);

  const effortValue = useMemo(() => {
    if (effortUnit === "capacity_pct" || effortUnit === "fte") {
      return Number(effortInput) || 0;
    }
    return parseHoursClock(effortInput);
  }, [effortInput, effortUnit]);

  const allocationPct = selectedPerson
    ? allocationPctFromEffort(effortUnit, effortValue, selectedPerson.weekly_capacity_hrs)
    : 50;

  const hoursPerDay = selectedPerson
    ? roundToHalfHour((allocationPct / 100) * (selectedPerson.weekly_capacity_hrs / 5))
    : roundToHalfHour(effortValue);

  const totalHours = totalEffortHours(hoursPerDay, workDays);
  const totalDays = selectedPerson
    ? totalEffortDays(hoursPerDay, workDays, selectedPerson.weekly_capacity_hrs)
    : workDays;

  const handleEffortUnitChange = (unit: EffortUnit) => {
    if (!selectedPerson) {
      setEffortUnit(unit);
      return;
    }
    const nextValue = effortValueFromPct(unit, allocationPct, selectedPerson.weekly_capacity_hrs);
    setEffortUnit(unit);
    if (unit === "capacity_pct") setEffortInput(String(Math.round(nextValue)));
    else if (unit === "fte") setEffortInput(String(roundToHalfHour(nextValue * 10) / 10));
    else setEffortInput(formatHoursClock(nextValue));
  };

  const handlePersonChange = (nextPersonId: number) => {
    setPersonId(nextPersonId);
    const person = people.find((item) => item.id === nextPersonId);
    if (!person) return;
    setEffortUnit("hours_per_day");
    setEffortInput(formatHoursClock(roundToHalfHour(person.weekly_capacity_hrs / 5)));
  };

  const buildDraft = (): AllocationDragDraft => ({
    person_id: selectedPerson!.id,
    start_date: rangeStart,
    end_date: rangeEnd,
    allocation_pct: allocationPct,
    hours_per_day: hoursPerDay,
    work_days: workDays,
    note: note.trim() || undefined,
    repeat: {
      enabled: !isEdit && repeatEnabled,
      interval: "week",
      endMode: repeatEndMode,
      endOn: repeatEndOn,
      occurrences: Math.max(1, repeatOccurrences),
    },
  });

  const handleSave = () => {
    if (!selectedPerson || personId === "") return;
    const draft = buildDraft();
    if (isEdit && onUpdate) {
      onUpdate(draft);
      return;
    }
    onSave(draft);
  };

  const style = {
    top: Math.min(anchor.top, window.innerHeight - 320),
    left: Math.min(Math.max(8, anchor.left - 200), window.innerWidth - 580),
  };

  return createPortal(
    <div className="runn-allocation-popover" ref={popoverRef} style={style}>
      {!fixedPerson ? (
        <select
          className="runn-allocation-person"
          onChange={(event) => handlePersonChange(Number(event.target.value))}
          value={personId}
        >
          {people.map((person) => (
            <option key={person.id} value={person.id}>
              {person.name}
            </option>
          ))}
        </select>
      ) : null}

      <div className="runn-effort-row">
        <label className="runn-field">
          <span className="runn-field-label">
            Effort <HelpCircle aria-hidden size={13} />
          </span>
          <div className="runn-split">
            <input
              aria-label="Effort"
              onChange={(event) => setEffortInput(event.target.value)}
              type="text"
              value={effortInput}
            />
            <select
              aria-label="Effort unit"
              onChange={(event) => handleEffortUnitChange(event.target.value as EffortUnit)}
              value={effortUnit}
            >
              {EFFORT_UNITS.map((option) => (
                <option key={option.value} value={option.value}>
                  {option.label}
                </option>
              ))}
            </select>
          </div>
        </label>

        <label className="runn-field">
          <span className="runn-field-label">Work Days</span>
          <input
            aria-label="Work days"
            className="runn-workdays"
            min={1}
            onChange={(event) => setWorkDays(Math.max(1, Number(event.target.value) || 1))}
            type="number"
            value={workDays}
          />
        </label>

        <label className="runn-field">
          <span className="runn-field-label">Total Effort</span>
          <div className="runn-split">
            <input
              aria-label="Total effort"
              readOnly
              type="text"
              value={totalUnit === "hours" ? formatHoursClock(totalHours) : `${totalDays}`}
            />
            <select
              aria-label="Total unit"
              onChange={(event) => setTotalUnit(event.target.value as TotalEffortUnit)}
              value={totalUnit}
            >
              <option value="hours">Hours</option>
              <option value="days">Days</option>
            </select>
          </div>
        </label>
      </div>

      {!isEdit && repeatEnabled ? (
        <div className="runn-repeat-hint muted-text">
          Repeats weekly until{" "}
          {repeatEndMode === "on" ? format(new Date(repeatEndOn), "d MMM yyyy") : `${repeatOccurrences} times`}
        </div>
      ) : null}

      <button className="runn-note-toggle" onClick={() => setNoteOpen((open) => !open)} type="button">
        <ChevronRight className={noteOpen ? "is-open" : ""} size={14} />
        Note
      </button>
      {noteOpen ? (
        <textarea
          className="runn-note-input"
          onChange={(event) => setNote(event.target.value)}
          placeholder="Add a note…"
          rows={2}
          value={note}
        />
      ) : null}

      {datesOpen ? (
        <div className="runn-date-row">
          <label>
            Start
            <input onChange={(event) => setRangeStart(event.target.value)} type="date" value={rangeStart} />
          </label>
          <label>
            End
            <input onChange={(event) => setRangeEnd(event.target.value)} type="date" value={rangeEnd} />
          </label>
        </div>
      ) : null}

      <div className="runn-popover-footer">
        {isEdit && onDelete ? (
          <button
            className="runn-delete"
            onClick={() => {
              if (window.confirm("Delete this assignment?")) onDelete();
            }}
            type="button"
          >
            Delete
          </button>
        ) : (
          <button className="runn-delete" onClick={onCancel} type="button">
            Cancel
          </button>
        )}

        <div className="runn-footer-actions">
          <button
            aria-label="Dates"
            className={`runn-icon-pill${datesOpen ? " is-active" : ""}`}
            onClick={() => {
              setDatesOpen((open) => !open);
              setMenuOpen(false);
              setTransferOpen(false);
            }}
            type="button"
          >
            <Calendar size={18} />
          </button>

          <div className="runn-menu-wrap" ref={menuRef}>
            <button
              aria-expanded={menuOpen}
              aria-label="More options"
              className={`runn-icon-pill${menuOpen ? " is-active" : ""}`}
              onClick={() => setMenuOpen((open) => !open)}
              type="button"
            >
              <MoreHorizontal size={18} />
            </button>
            {menuOpen ? (
              <div className="runn-menu runn-menu-actions">
                {isEdit ? (
                  <>
                    <button
                      className="runn-menu-action"
                      onClick={() => setTransferOpen((open) => !open)}
                      type="button"
                    >
                      Transfer
                    </button>
                    {transferOpen && transferPeople.length > 0 ? (
                      <div className="runn-menu-sub">
                        <select
                          onChange={(event) => {
                            const id = Number(event.target.value);
                            if (id) onTransfer?.(id);
                            setMenuOpen(false);
                            setTransferOpen(false);
                          }}
                          value=""
                        >
                          <option value="">Choose person…</option>
                          {transferPeople.map((person) => (
                            <option key={person.id} value={person.id}>
                              {person.name}
                            </option>
                          ))}
                        </select>
                      </div>
                    ) : null}
                    <button
                      className="runn-menu-action"
                      onClick={() => {
                        onClone?.();
                        setMenuOpen(false);
                      }}
                      type="button"
                    >
                      Clone
                    </button>
                    <button
                      className="runn-menu-action"
                      onClick={() => {
                        const last = dayDates.length
                          ? format(dayDates[dayDates.length - 1], "yyyy-MM-dd")
                          : rangeEnd;
                        setRangeEnd(last);
                        onSelectAllToRight?.();
                        setMenuOpen(false);
                      }}
                      type="button"
                    >
                      Select All to Right
                    </button>
                    <button
                      className="runn-menu-action"
                      onClick={() => {
                        onToggleMultiSelect?.();
                        setMenuOpen(false);
                      }}
                      type="button"
                    >
                      {multiSelectActive ? "Disable Multi-Select Mode" : "Enable Multi-Select Mode"}
                    </button>
                  </>
                ) : (
                  <>
                    <label className="runn-menu-item">
                      <input
                        checked={repeatEnabled}
                        onChange={(event) => setRepeatEnabled(event.target.checked)}
                        type="checkbox"
                      />
                      Repeat weekly
                    </label>
                    {repeatEnabled ? (
                      <>
                        <label className="runn-menu-item">
                          End on
                          <input
                            onChange={(event) => {
                              setRepeatEndMode("on");
                              setRepeatEndOn(event.target.value);
                            }}
                            type="date"
                            value={repeatEndOn}
                          />
                        </label>
                        <label className="runn-menu-item">
                          After
                          <input
                            min={1}
                            onChange={(event) => {
                              setRepeatEndMode("after");
                              setRepeatOccurrences(Math.max(1, Number(event.target.value) || 1));
                            }}
                            type="number"
                            value={repeatOccurrences}
                          />
                          times
                        </label>
                      </>
                    ) : null}
                  </>
                )}
              </div>
            ) : null}
          </div>

          <button
            className="runn-save"
            disabled={isSaving || !selectedPerson || personId === ""}
            onClick={handleSave}
            type="button"
          >
            Save
          </button>
        </div>
      </div>
    </div>,
    document.body,
  );
}