mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +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:
@@ -10,6 +10,7 @@ import { safeHttpUrl } from '../../utils/safeUrl'
|
||||
import { ChevronDown, ChevronRight, ChevronUp, Compass, RotateCcw, ExternalLink, Pencil, GripVertical, Ticket, Plus, FileText, Trash2, Car, Lock, Hotel, Eraser, Route as RouteIcon, RouteOff, Bookmark, StickyNote, TramFront, Zap, MapPin, Globe } from 'lucide-react'
|
||||
import type { PlannedTransitLeg } from './transitLeg'
|
||||
import { useTransitLegPlanner } from './useTransitLegPlanner'
|
||||
import { useDayPlanDragState } from './useDayPlanDragState'
|
||||
import { WalkLateLine } from '../Tours/walk/WalkTransitNote'
|
||||
import { TourOverrunLine } from '../Tours/TourDayTimeParts'
|
||||
import { dayItemOverrun } from '../Tours/tourDayTime'
|
||||
@@ -246,17 +247,7 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
// distances refresh instead of showing stale cached text (#1300).
|
||||
const distanceUnit = useSettingsStore(s => s.settings.distance_unit)
|
||||
const legsAbortRef = useRef<AbortController | null>(null)
|
||||
const [draggingId, _setDraggingId] = useState(null)
|
||||
// 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).
|
||||
const [hasDragged, setHasDragged] = useState(false)
|
||||
const setDraggingId = (id) => { if (id != null) setHasDragged(true); _setDraggingId(id) }
|
||||
const [lockedIds, setLockedIds] = useState(new Set())
|
||||
const [dropTargetKey, _setDropTargetKey] = useState(null)
|
||||
const dropTargetRef = useRef(null)
|
||||
const setDropTargetKey = (key) => { dropTargetRef.current = key; _setDropTargetKey(key) }
|
||||
const [dragOverDayId, setDragOverDayId] = useState(null)
|
||||
const [transportDetail, setTransportDetail] = useState(null)
|
||||
const [transportPosVersion, setTransportPosVersion] = useState(0)
|
||||
|
||||
@@ -273,6 +264,9 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
fromType?: string; toType?: string; toId?: number; insertAfter?: boolean; toLegIndex?: number | null;
|
||||
} | null>(null)
|
||||
const dragDataRef = useRef(null)
|
||||
const {
|
||||
draggingId, setDraggingId, hasDragged, dropTargetKey, dropTargetRef, setDropTargetKey, dragOverDayId, setDragOverDayId,
|
||||
} = useDayPlanDragState(dragDataRef)
|
||||
const scrollContainerRef = useRef<HTMLDivElement | null>(null)
|
||||
const dayRefs = useRef<Map<number, HTMLElement>>(new Map())
|
||||
|
||||
@@ -381,31 +375,6 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
return () => document.removeEventListener('dragend', cleanup)
|
||||
}, [])
|
||||
|
||||
// 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).
|
||||
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)
|
||||
}
|
||||
}, [draggingId, dropTargetKey, dragOverDayId])
|
||||
|
||||
// Initialize missing transport positions outside of render to avoid setState-during-render
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
useEffect(() => { days.forEach(day => initTransportPositions(day.id)) }, [days, reservations])
|
||||
|
||||
@@ -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