Skip to content

Commit 251d400

Browse files
authored
perf: improve search target speed by pagination strategy (#1205)
1 parent 6bb6988 commit 251d400

File tree

1 file changed

+46
-75
lines changed

1 file changed

+46
-75
lines changed

apps/nextjs-app/src/features/app/blocks/view/search/SearchCountPagination.tsx

Lines changed: 46 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum PageDirection {
2323
type ISearchMap = Record<number, NonNullable<ISearchIndexVo>[number]>;
2424

2525
const PaginationBuffer = 300;
26-
const PaginationGap = 100;
26+
const PaginationGap = 200;
2727

2828
type ISearchCountPaginationProps = Pick<ISearchButtonProps, 'shareView'>;
2929

@@ -41,7 +41,7 @@ export const SearchCountPagination = forwardRef<
4141
const tableId = useTableId();
4242
const view = useView() as GridView;
4343
const fields = useFields();
44-
const [currentPage, setCurrentPage] = useState(0);
44+
const [currentIndex, setCurrentIndex] = useState(0);
4545
const { gridRef, setSearchCursor } = useGridSearchStore();
4646

4747
useImperativeHandle(ref, () => ({
@@ -108,31 +108,33 @@ export const SearchCountPagination = forwardRef<
108108
queryFn: async () => {
109109
setSearchCursor(null);
110110
const nextMap = await getNextIndex();
111-
if (totalCount <= PaginationBuffer) {
111+
if (totalCount <= PaginationBuffer || currentIndex === 1) {
112112
return nextMap;
113113
}
114+
114115
const preMap = await getPreviousIndex();
116+
115117
return {
116118
...preMap,
117119
...nextMap,
118120
};
119121
},
120-
enabled: Boolean(tableId && value && currentPage !== 0),
122+
enabled: Boolean(tableId && value && currentIndex !== 0),
121123
refetchOnWindowFocus: false,
122124
});
123125

124126
const getPreviousIndex = async () => {
125127
const finalResult: ISearchMap = {};
126128
let skip = 0;
127-
const previousCursor = currentPage - PaginationBuffer - 1;
129+
const previousCursor = currentIndex - PaginationBuffer - 1;
128130
if (previousCursor === 0) {
129-
skip = totalCount - PaginationBuffer;
131+
skip = 0;
130132
}
131133
if (previousCursor > 0) {
132-
skip = currentPage - PaginationBuffer;
134+
skip = currentIndex - PaginationBuffer;
133135
}
134136
const baseQueryRo = {
135-
skip: skip,
137+
skip: skip || undefined,
136138
take: PaginationBuffer,
137139
viewId: view?.id,
138140
orderBy: viewOrderBy,
@@ -145,33 +147,13 @@ export const SearchCountPagination = forwardRef<
145147
? (baseQueryRo: ISearchIndexByQueryRo) => getShareViewSearchIndex(view.shareId!, baseQueryRo)
146148
: (baseQueryRo: ISearchIndexByQueryRo) => getSearchIndex(tableId!, baseQueryRo);
147149

148-
if (previousCursor < 0) {
149-
const preSkip = previousCursor + totalCount;
150-
const [preResult, nextResult] = await Promise.all([
151-
previousFn({ ...baseQueryRo, skip: preSkip }),
152-
previousFn({ ...baseQueryRo }),
153-
]);
154-
preResult?.data &&
155-
preResult?.data?.forEach((result, index) => {
156-
const indexNumber = preSkip + index + 1;
157-
finalResult[indexNumber] = result;
158-
});
159-
160-
nextResult?.data &&
161-
nextResult.data?.forEach((result, index) => {
162-
const indexNumber = index + 1;
163-
finalResult[indexNumber] = result;
164-
});
165-
return finalResult;
166-
} else {
167-
const result = await previousFn(baseQueryRo);
168-
result?.data &&
169-
result.data?.forEach((result, index) => {
170-
const indexNumber = skip + index + 1;
171-
finalResult[indexNumber] = result;
172-
});
173-
return finalResult;
174-
}
150+
const result = await previousFn(baseQueryRo);
151+
result?.data &&
152+
result.data?.forEach((result, index) => {
153+
const indexNumber = skip + index + 1;
154+
finalResult[indexNumber] = result;
155+
});
156+
return finalResult;
175157
};
176158

177159
const getNextIndex = async () => {
@@ -184,69 +166,58 @@ export const SearchCountPagination = forwardRef<
184166
groupBy: view.group,
185167
filter: view.filter,
186168
};
187-
const nextCursor = currentPage + PaginationBuffer - 1;
188169
const nextFn = shareView
189170
? (baseQueryRo: ISearchIndexByQueryRo) => getShareViewSearchIndex(view.shareId!, baseQueryRo)
190171
: (baseQueryRo: ISearchIndexByQueryRo) => getSearchIndex(tableId!, baseQueryRo);
191-
if (nextCursor <= totalCount) {
192-
const skip = currentPage - 1;
193-
const result = await nextFn({ ...baseQueryRo, skip });
194-
result?.data &&
195-
result.data?.forEach((result, index) => {
196-
const indexNumber = skip + index + 1;
197-
finalResult[indexNumber] = result;
198-
});
199-
return finalResult;
200-
} else {
201-
const preSkip = currentPage - 1;
202-
const [preResult, nextResult] = await Promise.all([
203-
nextFn({ ...baseQueryRo, skip: preSkip }),
204-
nextFn({ ...baseQueryRo }),
205-
]);
206172

207-
preResult?.data &&
208-
preResult?.data?.forEach((result, index) => {
209-
const indexNumber = preSkip + index + 1;
210-
finalResult[indexNumber] = result;
211-
});
212-
nextResult?.data &&
213-
nextResult.data?.forEach((result, index) => {
214-
const indexNumber = index + 1;
215-
finalResult[indexNumber] = result;
216-
});
217-
return finalResult;
218-
}
173+
const skip = currentIndex - 1 < 0 ? 0 : currentIndex - 1;
174+
const result = await nextFn({ ...baseQueryRo, skip });
175+
176+
result?.data &&
177+
result.data?.forEach((result, index) => {
178+
const indexNumber = skip + index + 1;
179+
finalResult[indexNumber] = result;
180+
});
181+
return finalResult;
219182
};
220183

221184
useEffect(() => {
222-
if (currentPage && indexData?.[currentPage]) {
223-
const index = indexData?.[currentPage];
185+
if (currentIndex && indexData?.[currentIndex]) {
186+
const index = indexData?.[currentIndex];
224187
index && setIndexSelection(index.index, index.fieldId);
225188
}
226-
}, [currentPage, indexData, setIndexSelection]);
189+
}, [currentIndex, indexData, setIndexSelection]);
227190

228191
useEffect(() => {
229192
if (totalCount) {
230-
setCurrentPage(1);
193+
setCurrentIndex(1);
231194
return;
232195
}
233196

234-
setCurrentPage(0);
197+
setCurrentIndex(0);
235198
}, [totalCount]);
236199

237200
// refetch the index window
238201
useEffect(() => {
239202
if (!indexData || totalCount <= PaginationBuffer || indexFetching) return;
240-
const nextAnchor = ((currentPage + totalCount + PaginationGap - 1) % totalCount) + 1;
241-
const prevAnchor = ((currentPage + totalCount - PaginationGap - 1) % totalCount) + 1;
203+
204+
const nextAnchor =
205+
currentIndex + PaginationBuffer - PaginationGap >= totalCount
206+
? totalCount
207+
: currentIndex + PaginationBuffer - PaginationGap;
208+
209+
const prevAnchor =
210+
currentIndex - PaginationBuffer + PaginationGap <= 0
211+
? 1
212+
: currentIndex - PaginationBuffer + PaginationGap;
242213

243214
if (!indexData[nextAnchor] || !indexData[prevAnchor]) {
244215
refetch();
245216
}
246-
}, [currentPage, indexData, indexFetching, refetch, totalCount]);
217+
}, [currentIndex, indexData, indexFetching, refetch, totalCount]);
247218

248219
const switchIndex = (direction: PageDirection) => {
249-
const newIndex = ((currentPage - 1 + direction + totalCount) % totalCount) + 1;
220+
const newIndex = ((currentIndex - 1 + direction + totalCount) % totalCount) + 1;
250221
if (
251222
!totalCount ||
252223
totalCount === 1 ||
@@ -255,12 +226,12 @@ export const SearchCountPagination = forwardRef<
255226
) {
256227
return;
257228
}
258-
setCurrentPage(newIndex);
229+
setCurrentIndex(newIndex);
259230
};
260231

261232
return (
262233
value &&
263-
(countLoading || (currentPage !== 0 && indexLoading) ? (
234+
(indexFetching || countLoading || (currentIndex !== 0 && indexLoading) ? (
264235
<Spin className="size-3 shrink-0" />
265236
) : (
266237
<div className="flex flex-1 shrink-0 items-center gap-0.5 p-0">
@@ -276,7 +247,7 @@ export const SearchCountPagination = forwardRef<
276247
<ChevronLeft />
277248
</Button>
278249
<span className="pointer-events-none whitespace-nowrap">
279-
{currentPage} / {totalCount}
250+
{currentIndex} / {totalCount}
280251
</span>
281252
<Button
282253
size={'xs'}

0 commit comments

Comments
 (0)