refactor(web): extract allocation drag finalize helpers

This commit is contained in:
2026-04-01 09:57:29 +02:00
parent 54c6cf2e2d
commit 6dac993521
3 changed files with 237 additions and 23 deletions
+10 -23
View File
@@ -7,11 +7,16 @@ import { pixelsToDays, computeDragDates } from "~/components/timeline/dragMath.j
import {
captureLivePreviewTargets,
clearLivePreview,
datesMatch,
preserveLivePreview,
scheduleLivePreview,
type LivePreviewSession,
} from "./timelineLivePreview.js";
import {
buildAllocationMovedSnapshot,
hasAllocationDateChange,
requiresAllocationFragmentExtraction,
shouldTreatAllocationDragAsClick,
} from "./timelineAllocationFinalize.js";
import {
createMultiSelectState,
finalizeMultiSelectDraft,
@@ -729,23 +734,14 @@ export function useTimelineDrag({
updateAllocationDragPosition(ev.clientX);
const alloc = allocDragRef.current;
if (!alloc.isActive) return;
const pointerDelta = Math.abs(alloc.pointerDeltaX);
const hasDateChange =
Boolean(alloc.originalStartDate && alloc.currentStartDate && alloc.originalEndDate && alloc.currentEndDate) &&
(
alloc.originalStartDate!.getTime() !== alloc.currentStartDate!.getTime() ||
alloc.originalEndDate!.getTime() !== alloc.currentEndDate!.getTime()
);
const hasDateChange = hasAllocationDateChange(alloc);
if (hasDateChange) {
preserveLivePreview(allocPreviewRef.current);
}
clearLivePreview(allocPreviewRef.current);
allocPreviewRef.current = null;
const shouldTreatAsClick =
alloc.mode === "move" &&
alloc.daysDelta === 0 &&
pointerDelta <= DRAG_CLICK_THRESHOLD_PX;
const shouldTreatAsClick = shouldTreatAllocationDragAsClick(alloc, DRAG_CLICK_THRESHOLD_PX);
if (shouldTreatAsClick && alloc.allocationId) {
// No movement → treat as click
@@ -767,18 +763,9 @@ export function useTimelineDrag({
const currentStartDate = alloc.currentStartDate;
const currentEndDate = alloc.currentEndDate;
const baseMutationAllocationId = alloc.mutationAllocationId ?? activeAllocationId;
const requiresExtraction =
alloc.scope === "segment" &&
(!datesMatch(alloc.originalStartDate, alloc.allocationStartDate) ||
!datesMatch(alloc.originalEndDate, alloc.allocationEndDate));
const requiresExtraction = requiresAllocationFragmentExtraction(alloc);
pendingSnapshotRef.current = {
allocationId: activeAllocationId,
mutationAllocationId: baseMutationAllocationId,
projectName: alloc.projectName ?? "",
before: { startDate: alloc.originalStartDate!, endDate: alloc.originalEndDate! },
after: { startDate: currentStartDate, endDate: currentEndDate },
};
pendingSnapshotRef.current = buildAllocationMovedSnapshot(alloc);
pendingOptimisticAllocationIdRef.current = activeAllocationId;
setOptimisticAllocations((prev) => {
const next = new Map(prev);