-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathCardList.tsx
More file actions
144 lines (129 loc) · 4.19 KB
/
CardList.tsx
File metadata and controls
144 lines (129 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { useVirtualizer } from '@tanstack/react-virtual';
import { memo, useCallback, useLayoutEffect, useMemo, useRef } from 'react';
import { PADDING_END } from '@/application/database-yjs';
import { useBoardActions, useBoardSelection } from '@/components/database/board/BoardProvider';
import { Card } from '@/components/database/components/board/card';
import { cn } from '@/lib/utils';
export enum CardType {
CARD = 'card',
NEW_CARD = 'new_card',
}
export interface RenderCard {
type: CardType;
id: string;
}
const CARD_LIST_MAX_HEIGHT = 2000;
const CARD_LIST_STYLE = {
maxHeight: CARD_LIST_MAX_HEIGHT,
overflowY: 'auto',
} as const;
function CardList({
data,
fieldId,
columnId,
setScrollElement: _setScrollElement,
}: {
columnId: string;
data: RenderCard[];
fieldId: string;
setScrollElement?: (element: HTMLDivElement | null) => void;
}) {
const parentRef = useRef<HTMLDivElement>(null);
const parentOffsetRef = useRef(0);
const { creatingColumnId } = useBoardSelection();
const { setCreatingColumnId } = useBoardActions();
const isCreating = useMemo(() => {
return creatingColumnId === columnId;
}, [creatingColumnId, columnId]);
const setIsCreating = useCallback(
(isCreating: boolean) => {
if (isCreating) {
setCreatingColumnId(columnId);
} else {
setCreatingColumnId(null);
}
},
[columnId, setCreatingColumnId]
);
const getScrollElement = useCallback(() => {
if (!parentRef.current) return null;
// Board cards scroll within their local column container, not the document
// Return the parent div itself as the scroll container
return parentRef.current;
}, []);
// Board columns have local scroll, so scrollMargin should always be 0
// No need for RAF measurement like Grid - layout is stable within the column
useLayoutEffect(() => {
parentOffsetRef.current = 0;
}, []);
const virtualizer = useVirtualizer({
count: data.length,
scrollMargin: 0, // Always 0 for Board - items are positioned relative to column top
overscan: 5,
getScrollElement,
estimateSize: () => 36,
paddingStart: 0,
paddingEnd: PADDING_END,
getItemKey: (index) => data[index].id || String(index),
});
const virtualItems = virtualizer.getVirtualItems();
const viewportHeight = Math.min(
parentRef.current?.clientHeight || CARD_LIST_MAX_HEIGHT,
CARD_LIST_MAX_HEIGHT
);
const scrollTop = parentRef.current?.scrollTop ?? 0;
const renderStart = Math.max(0, scrollTop - 5 * 36);
const renderEnd = scrollTop + viewportHeight + 5 * 36;
const maxRenderedItems = Math.ceil(viewportHeight / 36) + 12;
const items = virtualItems.length > maxRenderedItems
? virtualItems
.filter((item) => item.end >= renderStart && item.start <= renderEnd)
.slice(0, maxRenderedItems)
: virtualItems;
return (
<div
ref={parentRef}
className='appflowy-custom-scroller w-full min-h-0 flex-1'
style={CARD_LIST_STYLE}
>
<div
style={{
height: virtualizer.getTotalSize(),
width: '100%',
position: 'relative',
}}
>
{items.map((virtualRow) => {
const row = data[virtualRow.index];
const { id, type } = row;
return (
<div
key={virtualRow.key}
data-index={virtualRow.index}
ref={virtualizer.measureElement}
className={cn('w-full px-2 py-[3px]', isCreating && 'transform transition-all duration-150 ease-in-out')}
style={{
position: 'absolute',
top: 0,
left: 0,
transform: `translateY(${virtualRow.start - virtualizer.options.scrollMargin}px)`,
paddingTop: virtualRow.index === 0 ? 10 : undefined,
}}
>
<Card
type={type}
rowId={id}
groupFieldId={fieldId}
setIsCreating={setIsCreating}
isCreating={isCreating}
columnId={columnId}
beforeId={data[virtualRow.index - 1]?.id}
/>
</div>
);
})}
</div>
</div>
);
}
export default memo(CardList);