|
| 1 | +import { closestCenter, type CollisionDetection } from "@dnd-kit/core"; |
| 2 | +import { verticalListSortingStrategy, type SortingStrategy } from "@dnd-kit/sortable"; |
| 3 | +import { |
| 4 | + resolveSidebarDropTarget, |
| 5 | + sidebarListItemId, |
| 6 | + sidebarMarkerId, |
| 7 | + type SidebarListItem, |
| 8 | + type SidebarListMarker, |
| 9 | + type SidebarSection, |
| 10 | +} from "./Sidebar.logic"; |
| 11 | + |
| 12 | +const stationary = { x: 0, y: 0, scaleX: 1, scaleY: 1 }; |
| 13 | +const hidden = { ...stationary, scaleY: 0 }; |
| 14 | +type ThreadItem = Extract<SidebarListItem, { kind: "thread" }>; |
| 15 | +type Layout = Parameters<SortingStrategy>[0]; |
| 16 | + |
| 17 | +/** Reject the nearest unsupported target without selecting another section. |
| 18 | + * Recreate this detector when drop eligibility changes. */ |
| 19 | +export function createSidebarCollisionDetection( |
| 20 | + isValidTarget: (id: string) => boolean, |
| 21 | + options: { emptyPins?: boolean; activationY?: number | null } = {}, |
| 22 | +): CollisionDetection { |
| 23 | + const validity = new Map<string, boolean>(); |
| 24 | + const pinnedHeaderId = sidebarMarkerId("pinned-header"); |
| 25 | + return (args) => { |
| 26 | + let collisions = closestCenter(args); |
| 27 | + const pinnedRect = options.emptyPins ? args.droppableRects.get(pinnedHeaderId) : undefined; |
| 28 | + const pointer = args.pointerCoordinates; |
| 29 | + // The card itself is clamped by the scroll container. An upward pointer |
| 30 | + // gesture can still reach the empty pinned boundary without reserving a row. |
| 31 | + if ( |
| 32 | + pinnedRect && |
| 33 | + pointer && |
| 34 | + options.activationY != null && |
| 35 | + pointer.y <= options.activationY - 6 && |
| 36 | + pointer.y <= pinnedRect.top + 8 && |
| 37 | + pointer.x >= pinnedRect.left && |
| 38 | + pointer.x <= pinnedRect.right |
| 39 | + ) { |
| 40 | + const pinned = collisions.find((collision) => collision.id === pinnedHeaderId); |
| 41 | + if (pinned) { |
| 42 | + collisions = [pinned, ...collisions.filter((collision) => collision !== pinned)]; |
| 43 | + } |
| 44 | + } |
| 45 | + const nearest = collisions[0]; |
| 46 | + if (!nearest || nearest.id === args.active.id) return collisions; |
| 47 | + const id = String(nearest.id); |
| 48 | + const valid = validity.get(id) ?? isValidTarget(id); |
| 49 | + validity.set(id, valid); |
| 50 | + return valid ? collisions : collisions.filter((collision) => collision.id === args.active.id); |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +/** Preview the committed section layout without moving or mounting DOM nodes. |
| 55 | + * A zero scaleY marks rows/markers to hide while retaining their measured nodes. */ |
| 56 | +export function createSidebarSortingStrategy(input: { |
| 57 | + items: readonly SidebarListItem[]; |
| 58 | + settledOrder: readonly string[]; |
| 59 | + settledExpanded: boolean; |
| 60 | + settledVisibleCount?: number; |
| 61 | + routeThreadKey?: string | null; |
| 62 | + snoozedThreadCount?: number; |
| 63 | + cardHeight?: number; |
| 64 | + slimHeight?: number; |
| 65 | +}): SortingStrategy { |
| 66 | + const { items } = input; |
| 67 | + const indices = new Map(items.map((item, index) => [sidebarListItemId(item), index])); |
| 68 | + let previous: Pick<Layout, "rects" | "activeIndex" | "overIndex"> | undefined; |
| 69 | + let transforms: ReturnType<SortingStrategy>[] | null = []; |
| 70 | + |
| 71 | + function project({ rects, activeIndex, overIndex }: Layout) { |
| 72 | + const active = items[activeIndex]; |
| 73 | + const over = items[overIndex]; |
| 74 | + if (active?.kind !== "thread" || !over || !rects[0]) return []; |
| 75 | + const target = resolveSidebarDropTarget(items, active.key, sidebarListItemId(over)); |
| 76 | + if (!target) return []; |
| 77 | + if (target.section === active.section && over.kind === "thread") |
| 78 | + return target.section === "settled" ? [] : null; |
| 79 | + const groups: Record<SidebarSection, ThreadItem[]> = { |
| 80 | + pinned: [], |
| 81 | + active: [], |
| 82 | + snoozed: [], |
| 83 | + settled: [], |
| 84 | + }; |
| 85 | + let cardHeight = input.cardHeight; |
| 86 | + let slimHeight = input.slimHeight; |
| 87 | + for (const [index, item] of items.entries()) { |
| 88 | + if (item.kind === "marker") { |
| 89 | + if (item.marker.endsWith("placeholder")) slimHeight ??= rects[index]?.height; |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (item.section === "pinned" || item.section === "active") |
| 93 | + cardHeight ??= rects[index]?.height; |
| 94 | + else slimHeight ??= rects[index]?.height; |
| 95 | + if (item.key !== active.key) groups[item.section].push(item); |
| 96 | + } |
| 97 | + // Cards are 4.875rem + 0.25rem padding; slim rows/placeholders are h-9. |
| 98 | + const scale = slimHeight !== undefined ? slimHeight / 36 : (cardHeight ?? 82) / 82; |
| 99 | + cardHeight ??= 82 * scale; |
| 100 | + slimHeight ??= 36 * scale; |
| 101 | + const group = groups[target.section]; |
| 102 | + const order = |
| 103 | + target.section === "pinned" |
| 104 | + ? target.pinnedOrder |
| 105 | + : target.section === "settled" |
| 106 | + ? input.settledOrder |
| 107 | + : target.activeOrder; |
| 108 | + const ranks = new Map(order.map((key, index) => [key, index])); |
| 109 | + const rank = ranks.get(active.key) ?? Number.POSITIVE_INFINITY; |
| 110 | + const index = group.findIndex( |
| 111 | + (item) => (ranks.get(item.key) ?? Number.POSITIVE_INFINITY) > rank, |
| 112 | + ); |
| 113 | + group.splice(index < 0 ? group.length : index, 0, { ...active, section: target.section }); |
| 114 | + const settledOrder = ( |
| 115 | + input.settledOrder.length > 0 ? input.settledOrder : groups.settled.map((item) => item.key) |
| 116 | + ).filter((key) => key !== active.key || target.section === "settled"); |
| 117 | + const visible = input.settledExpanded |
| 118 | + ? settledOrder.slice(0, input.settledVisibleCount ?? settledOrder.length) |
| 119 | + : []; |
| 120 | + const routeKey = input.routeThreadKey; |
| 121 | + if (routeKey && settledOrder.includes(routeKey) && !visible.includes(routeKey)) { |
| 122 | + visible.push(routeKey); |
| 123 | + } |
| 124 | + groups.settled = visible.map((key) => ({ kind: "thread", key, section: "settled" })); |
| 125 | + const projected: SidebarListItem[] = []; |
| 126 | + const marker = (name: SidebarListMarker) => projected.push({ kind: "marker", marker: name }); |
| 127 | + const section = (name: "active" | "settled") => { |
| 128 | + if (groups[name].length > 0) projected.push(...groups[name]); |
| 129 | + else marker(`${name}-placeholder`); |
| 130 | + }; |
| 131 | + marker("pinned-header"); |
| 132 | + projected.push(...groups.pinned); |
| 133 | + marker("pinned-divider"); |
| 134 | + section("active"); |
| 135 | + if ( |
| 136 | + groups.snoozed.length > 0 || |
| 137 | + ((active.section !== "snoozed" || (input.snoozedThreadCount ?? 0) > 1) && |
| 138 | + items.some((item) => item.kind === "marker" && item.marker === "snoozed-header")) |
| 139 | + ) { |
| 140 | + marker("snoozed-header"); |
| 141 | + projected.push(...groups.snoozed); |
| 142 | + } |
| 143 | + marker("settled-header"); |
| 144 | + section("settled"); |
| 145 | + const result = items.map(() => hidden); |
| 146 | + let top = rects[0].top; |
| 147 | + for (const item of projected) { |
| 148 | + const index = indices.get(sidebarListItemId(item)); |
| 149 | + const rect = index === undefined ? undefined : rects[index]; |
| 150 | + if (index !== undefined && rect) result[index] = { ...stationary, y: top - rect.top }; |
| 151 | + const fallback = |
| 152 | + item.kind === "thread" && (item.section === "pinned" || item.section === "active") |
| 153 | + ? cardHeight |
| 154 | + : slimHeight; |
| 155 | + const moved = item.kind === "thread" && item.key === active.key; |
| 156 | + top += (moved ? fallback : (rect?.height ?? fallback)) + 1; |
| 157 | + } |
| 158 | + result[activeIndex] = stationary; |
| 159 | + return result; |
| 160 | + } |
| 161 | + |
| 162 | + return (args) => { |
| 163 | + if ( |
| 164 | + previous?.rects !== args.rects || |
| 165 | + previous.activeIndex !== args.activeIndex || |
| 166 | + previous.overIndex !== args.overIndex |
| 167 | + ) { |
| 168 | + previous = args; |
| 169 | + transforms = project(args); |
| 170 | + } |
| 171 | + return transforms === null |
| 172 | + ? verticalListSortingStrategy(args) |
| 173 | + : (transforms[args.index] ?? stationary); |
| 174 | + }; |
| 175 | +} |
0 commit comments