refactor(web): extract allocation release classification
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveAllocationRelease } from "./timelineAllocationRelease.js";
|
||||
|
||||
const BASE_ALLOC = {
|
||||
isActive: true,
|
||||
mode: "move" as const,
|
||||
pointerDeltaX: 0,
|
||||
daysDelta: 0,
|
||||
allocationId: "alloc-1",
|
||||
mutationAllocationId: null,
|
||||
projectId: "project-1",
|
||||
projectName: "Project One",
|
||||
scope: "allocation" as const,
|
||||
allocationStartDate: new Date("2025-01-01"),
|
||||
allocationEndDate: new Date("2025-01-03"),
|
||||
originalStartDate: new Date("2025-01-01"),
|
||||
originalEndDate: new Date("2025-01-03"),
|
||||
currentStartDate: new Date("2025-01-01"),
|
||||
currentEndDate: new Date("2025-01-03"),
|
||||
};
|
||||
|
||||
describe("timelineAllocationRelease", () => {
|
||||
it("ignores inactive drags", () => {
|
||||
expect(resolveAllocationRelease({ ...BASE_ALLOC, isActive: false }, { clickThresholdPx: 5, wasShift: false })).toEqual({
|
||||
kind: "ignore",
|
||||
preservePreview: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("routes shift-click releases to multi-select toggling", () => {
|
||||
expect(resolveAllocationRelease(BASE_ALLOC, { clickThresholdPx: 5, wasShift: true })).toEqual({
|
||||
kind: "shift-click",
|
||||
allocationId: "alloc-1",
|
||||
preservePreview: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to reset when click info cannot be built", () => {
|
||||
expect(
|
||||
resolveAllocationRelease(
|
||||
{
|
||||
...BASE_ALLOC,
|
||||
originalStartDate: null,
|
||||
originalEndDate: null,
|
||||
},
|
||||
{ clickThresholdPx: 5, wasShift: false },
|
||||
),
|
||||
).toEqual({
|
||||
kind: "reset",
|
||||
preservePreview: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("builds a mutation outcome when the allocation dates changed", () => {
|
||||
const currentStartDate = new Date("2025-01-02");
|
||||
const currentEndDate = new Date("2025-01-04");
|
||||
|
||||
expect(
|
||||
resolveAllocationRelease(
|
||||
{
|
||||
...BASE_ALLOC,
|
||||
daysDelta: 1,
|
||||
pointerDeltaX: 20,
|
||||
currentStartDate,
|
||||
currentEndDate,
|
||||
},
|
||||
{ clickThresholdPx: 5, wasShift: false },
|
||||
),
|
||||
).toEqual({
|
||||
kind: "mutation",
|
||||
preservePreview: true,
|
||||
mutationPlan: {
|
||||
activeAllocationId: "alloc-1",
|
||||
currentStartDate,
|
||||
currentEndDate,
|
||||
baseMutationAllocationId: "alloc-1",
|
||||
requiresExtraction: false,
|
||||
pendingSnapshot: {
|
||||
allocationId: "alloc-1",
|
||||
mutationAllocationId: "alloc-1",
|
||||
projectName: "Project One",
|
||||
before: {
|
||||
startDate: new Date("2025-01-01"),
|
||||
endDate: new Date("2025-01-03"),
|
||||
},
|
||||
after: {
|
||||
startDate: currentStartDate,
|
||||
endDate: currentEndDate,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("resets changed segment drags when no allocation id remains available", () => {
|
||||
expect(
|
||||
resolveAllocationRelease(
|
||||
{
|
||||
...BASE_ALLOC,
|
||||
allocationId: null,
|
||||
scope: "segment",
|
||||
daysDelta: 1,
|
||||
pointerDeltaX: 20,
|
||||
currentStartDate: new Date("2025-01-02"),
|
||||
currentEndDate: new Date("2025-01-04"),
|
||||
},
|
||||
{ clickThresholdPx: 5, wasShift: false },
|
||||
),
|
||||
).toEqual({
|
||||
kind: "reset",
|
||||
preservePreview: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { buildAllocationBlockClickInfo, buildAllocationMutationPlan } from "./timelineAllocationActions.js";
|
||||
import { hasAllocationDateChange, shouldTreatAllocationDragAsClick } from "./timelineAllocationFinalize.js";
|
||||
|
||||
type AllocationReleaseLike = {
|
||||
isActive: boolean;
|
||||
mode: "move" | "resize-start" | "resize-end";
|
||||
pointerDeltaX: number;
|
||||
daysDelta: number;
|
||||
allocationId: string | null;
|
||||
mutationAllocationId: string | null;
|
||||
projectId: string | null;
|
||||
projectName: string | null;
|
||||
scope: "allocation" | "segment";
|
||||
allocationStartDate: Date | null;
|
||||
allocationEndDate: Date | null;
|
||||
originalStartDate: Date | null;
|
||||
originalEndDate: Date | null;
|
||||
currentStartDate: Date | null;
|
||||
currentEndDate: Date | null;
|
||||
};
|
||||
|
||||
type AllocationBlockClickInfo = ReturnType<typeof buildAllocationBlockClickInfo>;
|
||||
type AllocationMutationPlan = NonNullable<ReturnType<typeof buildAllocationMutationPlan>>;
|
||||
|
||||
export type AllocationReleaseOutcome =
|
||||
| { kind: "ignore"; preservePreview: false }
|
||||
| { kind: "reset"; preservePreview: boolean }
|
||||
| { kind: "shift-click"; allocationId: string; preservePreview: boolean }
|
||||
| { kind: "click"; clickInfo: NonNullable<AllocationBlockClickInfo>; preservePreview: boolean }
|
||||
| { kind: "mutation"; mutationPlan: AllocationMutationPlan; preservePreview: true };
|
||||
|
||||
export function resolveAllocationRelease(
|
||||
alloc: AllocationReleaseLike,
|
||||
{ clickThresholdPx, wasShift }: { clickThresholdPx: number; wasShift: boolean },
|
||||
): AllocationReleaseOutcome {
|
||||
if (!alloc.isActive) {
|
||||
return { kind: "ignore", preservePreview: false };
|
||||
}
|
||||
|
||||
const preservePreview = hasAllocationDateChange(alloc);
|
||||
const shouldTreatAsClick = shouldTreatAllocationDragAsClick(alloc, clickThresholdPx);
|
||||
|
||||
if (shouldTreatAsClick && alloc.allocationId) {
|
||||
if (wasShift) {
|
||||
return { kind: "shift-click", allocationId: alloc.allocationId, preservePreview };
|
||||
}
|
||||
|
||||
const clickInfo = buildAllocationBlockClickInfo(alloc);
|
||||
if (clickInfo) {
|
||||
return { kind: "click", clickInfo, preservePreview };
|
||||
}
|
||||
}
|
||||
|
||||
if (preservePreview && alloc.allocationId && alloc.currentStartDate && alloc.currentEndDate) {
|
||||
const mutationPlan = buildAllocationMutationPlan(alloc);
|
||||
if (mutationPlan) {
|
||||
return { kind: "mutation", mutationPlan, preservePreview: true };
|
||||
}
|
||||
}
|
||||
|
||||
return { kind: "reset", preservePreview };
|
||||
}
|
||||
@@ -11,18 +11,14 @@ import {
|
||||
scheduleLivePreview,
|
||||
type LivePreviewSession,
|
||||
} from "./timelineLivePreview.js";
|
||||
import {
|
||||
buildAllocationMovedSnapshot,
|
||||
hasAllocationDateChange,
|
||||
shouldTreatAllocationDragAsClick,
|
||||
} from "./timelineAllocationFinalize.js";
|
||||
import { buildAllocationBlockClickInfo, buildAllocationMutationPlan } from "./timelineAllocationActions.js";
|
||||
import { buildAllocationMovedSnapshot } from "./timelineAllocationFinalize.js";
|
||||
import {
|
||||
finalizeAllocationMultiDrag,
|
||||
isAllocationMultiSelected,
|
||||
startAllocationMultiDrag,
|
||||
updateAllocationMultiDrag,
|
||||
} from "./timelineAllocationMultiDrag.js";
|
||||
import { resolveAllocationRelease } from "./timelineAllocationRelease.js";
|
||||
import { createAllocationDragState } from "./timelineAllocationDragState.js";
|
||||
import { attachDocumentMouseDrag } from "./timelineDocumentDrag.js";
|
||||
import { buildProjectShiftMutationInput, createProjectDragState } from "./timelineProjectDrag.js";
|
||||
@@ -703,36 +699,24 @@ export function useTimelineDrag({
|
||||
allocDragCleanupRef.current = null;
|
||||
updateAllocationDragPosition(ev.clientX);
|
||||
const alloc = allocDragRef.current;
|
||||
if (!alloc.isActive) return;
|
||||
const hasDateChange = hasAllocationDateChange(alloc);
|
||||
const release = resolveAllocationRelease(alloc, {
|
||||
clickThresholdPx: DRAG_CLICK_THRESHOLD_PX,
|
||||
wasShift,
|
||||
});
|
||||
if (release.kind === "ignore") return;
|
||||
|
||||
if (hasDateChange) {
|
||||
if (release.preservePreview) {
|
||||
preserveLivePreview(allocPreviewRef.current);
|
||||
}
|
||||
clearLivePreview(allocPreviewRef.current);
|
||||
allocPreviewRef.current = null;
|
||||
const shouldTreatAsClick = shouldTreatAllocationDragAsClick(alloc, DRAG_CLICK_THRESHOLD_PX);
|
||||
|
||||
if (shouldTreatAsClick && alloc.allocationId) {
|
||||
// No movement → treat as click
|
||||
if (wasShift) {
|
||||
// Shift+Click → toggle multi-selection for this allocation
|
||||
onShiftClickAllocRef.current?.(alloc.allocationId);
|
||||
} else {
|
||||
// Normal click → open alloc popover
|
||||
const clickInfo = buildAllocationBlockClickInfo(alloc);
|
||||
if (clickInfo) {
|
||||
onBlockClickRef.current?.(clickInfo);
|
||||
}
|
||||
}
|
||||
} else if (hasDateChange && alloc.allocationId && alloc.currentStartDate && alloc.currentEndDate) {
|
||||
const mutationPlan = buildAllocationMutationPlan(alloc);
|
||||
if (!mutationPlan) {
|
||||
allocDragRef.current = INITIAL_ALLOC_DRAG;
|
||||
setAllocDragState(INITIAL_ALLOC_DRAG);
|
||||
return;
|
||||
}
|
||||
|
||||
if (release.kind === "shift-click") {
|
||||
onShiftClickAllocRef.current?.(release.allocationId);
|
||||
} else if (release.kind === "click") {
|
||||
onBlockClickRef.current?.(release.clickInfo);
|
||||
} else if (release.kind === "mutation") {
|
||||
const { mutationPlan } = release;
|
||||
const {
|
||||
activeAllocationId,
|
||||
currentStartDate,
|
||||
|
||||
Reference in New Issue
Block a user