import { router } from '@inertiajs/react'; import { useCallback, useEffect, useRef, useState } from 'react'; import { toast } from 'sonner'; import { addRangeOptimistic, toggleDayOptimistic } from '@/lib/periods'; import { destroy, store, toggle, update } from '@/routes/periods'; import type { Marker, VacationPeriod } from '@/types/planner'; export type Selection = { start: string; end: string }; type Options = { markers: Marker[]; periods: VacationPeriod[]; }; export function usePlanner({ markers: serverMarkers, periods: serverPeriods, }: Options) { const [markers, setMarkers] = useState(serverMarkers); const [periods, setPeriods] = useState(serverPeriods); const [activeMarkerId, setActiveMarkerId] = useState(null); const [selection, setSelectionState] = useState(null); const selectionRef = useRef(null); const [touchAnchor, setTouchAnchor] = useState(null); const draggingRef = useRef(false); const setSelection = useCallback((s: Selection | null) => { selectionRef.current = s; setSelectionState(s); }, []); useEffect(() => setMarkers(serverMarkers), [serverMarkers]); useEffect(() => setPeriods(serverPeriods), [serverPeriods]); // Drop the brush if its marker was deleted. useEffect(() => { if ( activeMarkerId !== null && !markers.some((m) => m.id === activeMarkerId) ) { setActiveMarkerId(null); } }, [markers, activeMarkerId]); const activeMarker = markers.find((m) => m.id === activeMarkerId) ?? null; const rollback = (previous: VacationPeriod[]) => { setPeriods(previous); toast.error('Не удалось сохранить изменения. Попробуйте ещё раз.'); }; const commitRange = useCallback( (a: string, b: string) => { if (activeMarkerId === null) { return; } const start = a <= b ? a : b; const end = a <= b ? b : a; const previous = periods; if (start === end) { setPeriods(toggleDayOptimistic(periods, activeMarkerId, start)); router.post( toggle().url, { marker_id: activeMarkerId, date: start }, { preserveScroll: true, only: ['periods'], onError: () => rollback(previous), }, ); } else { setPeriods( addRangeOptimistic(periods, activeMarkerId, start, end), ); router.post( store().url, { marker_id: activeMarkerId, start_date: start, end_date: end, }, { preserveScroll: true, only: ['periods'], onError: () => rollback(previous), }, ); } }, [activeMarkerId, periods], ); /** * Pointer down on a day cell while a brush is active. * Mouse/pen: start drag. Touch: two-tap range selection. * Returns true when the event was handled as marking. */ const dayPointerDown = useCallback( (date: string, pointerType: string): boolean => { if (activeMarkerId === null) { return false; } if (pointerType === 'touch') { if (touchAnchor === null) { setTouchAnchor(date); setSelection({ start: date, end: date }); } else { commitRange(touchAnchor, date); setTouchAnchor(null); setSelection(null); } return true; } draggingRef.current = true; setSelection({ start: date, end: date }); return true; }, [activeMarkerId, touchAnchor, commitRange], ); const dayPointerEnter = useCallback( (date: string) => { if (draggingRef.current) { const current = selectionRef.current; if (current) { setSelection({ ...current, end: date }); } } }, [setSelection], ); const finishDrag = useCallback(() => { if (!draggingRef.current) { return; } draggingRef.current = false; const current = selectionRef.current; setSelection(null); if (current) { commitRange(current.start, current.end); } }, [commitRange, setSelection]); useEffect(() => { window.addEventListener('pointerup', finishDrag); window.addEventListener('pointercancel', finishDrag); return () => { window.removeEventListener('pointerup', finishDrag); window.removeEventListener('pointercancel', finishDrag); }; }, [finishDrag]); const cancelTouchAnchor = useCallback(() => { setTouchAnchor(null); setSelection(null); }, []); const deletePeriod = useCallback( (period: VacationPeriod) => { const previous = periods; setPeriods(periods.filter((p) => p.id !== period.id)); router.delete(destroy.url(period.id), { preserveScroll: true, only: ['periods'], onError: () => rollback(previous), }); }, [periods], ); const reschedulePeriod = useCallback( ( period: VacationPeriod, data: { start_date: string; end_date: string; marker_id: number }, ) => { const previous = periods; const without = periods.filter((p) => p.id !== period.id); setPeriods( addRangeOptimistic( without, data.marker_id, data.start_date, data.end_date, ), ); router.patch(update.url(period.id), data, { preserveScroll: true, only: ['periods'], onError: () => rollback(previous), }); }, [periods], ); return { markers, periods, activeMarker, activeMarkerId, setActiveMarkerId, selection, touchAnchor, cancelTouchAnchor, dayPointerDown, dayPointerEnter, deletePeriod, reschedulePeriod, }; } export type Planner = ReturnType;