mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
refactor(planner): the day plan's drag state lives in a hook of its own; the holiday provider cases get a suite of their own
DayPlanSidebar grew past its size baseline with the Firefox drag fix. Its drag state (what is dragged, the drop line, the day under the drag, whether a drag happened) and the effect that ends a lost drag move into useDayPlanDragState, typed, which also lowers the file's strict-mode count; size and strict baselines are lowered with it. vacay.service.test.ts went past the 2000-line limit once the Nager v4 cases were back in it. They move into vacay.holiday-provider.test.ts, which takes VacayService from a Nest testing module over a snapshot db, so neither the db mock nor a hand-built service is needed.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { useEffect, useRef, useState, type MutableRefObject } from 'react';
|
||||
|
||||
/** A stop's or a day's id, as the day plan's drag handlers pass them around. */
|
||||
type DragKey = string | number | null;
|
||||
|
||||
/**
|
||||
* The drag state of the day plan (DayPlanSidebar): what is being dragged, which
|
||||
* row the drop line stands on, which day the drag is over, and whether a drag
|
||||
* happened yet.
|
||||
*
|
||||
* The day list fades in once, on mount. Switching its stagger back on when a
|
||||
* drag ended played that entrance again on every drop, so the first drag turns
|
||||
* it off for good (#3041).
|
||||
*
|
||||
* A drag whose source row re-rendered away never sends its dragend to the
|
||||
* document, and the drop line then stayed on a row until the next reload. The
|
||||
* browser sends no mousemove during a drag, so the first plain mouse move or
|
||||
* press after one means it is over. Plain means no button held: Firefox fires
|
||||
* dragstart while it handles the move that starts the drag and hands that move
|
||||
* to the page afterwards, button still down. Read as the end, it threw the drag
|
||||
* away at pick-up and left only the arrows to reorder a day (#3041).
|
||||
*/
|
||||
export function useDayPlanDragState(dragDataRef: MutableRefObject<unknown>) {
|
||||
const [draggingId, setDraggingIdState] = useState<DragKey>(null);
|
||||
const [hasDragged, setHasDragged] = useState(false);
|
||||
const [dropTargetKey, setDropTargetKeyState] = useState<string | null>(null);
|
||||
const dropTargetRef = useRef<string | null>(null);
|
||||
const [dragOverDayId, setDragOverDayId] = useState<DragKey>(null);
|
||||
|
||||
const setDraggingId = (id: DragKey) => {
|
||||
if (id != null) setHasDragged(true);
|
||||
setDraggingIdState(id);
|
||||
};
|
||||
const setDropTargetKey = (key: string | null) => {
|
||||
dropTargetRef.current = key;
|
||||
setDropTargetKeyState(key);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!draggingId && !dropTargetKey && !dragOverDayId) return;
|
||||
const settle = (e: MouseEvent | PointerEvent) => {
|
||||
if (e.type === 'mousemove' && e.buttons !== 0) return;
|
||||
setDraggingId(null);
|
||||
setDropTargetKey(null);
|
||||
setDragOverDayId(null);
|
||||
dragDataRef.current = null;
|
||||
window.__dragData = null;
|
||||
};
|
||||
document.addEventListener('mousemove', settle);
|
||||
document.addEventListener('pointerdown', settle, true);
|
||||
return () => {
|
||||
document.removeEventListener('mousemove', settle);
|
||||
document.removeEventListener('pointerdown', settle, true);
|
||||
};
|
||||
// The setters close over nothing that changes; the effect follows the drag state only.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [draggingId, dropTargetKey, dragOverDayId]);
|
||||
|
||||
return {
|
||||
draggingId,
|
||||
setDraggingId,
|
||||
hasDragged,
|
||||
dropTargetKey,
|
||||
dropTargetRef,
|
||||
setDropTargetKey,
|
||||
dragOverDayId,
|
||||
setDragOverDayId,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user