Skip to content

Commit 3a48c61

Browse files
committed
perf: do not revalidate first page when ordering operations by hot (#368)
1 parent 3fee7b1 commit 3a48c61

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/apis/operation.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { uniqBy } from 'lodash-es'
12
import { QueriesCopilotRequest } from 'maa-copilot-client'
23
import useSWR from 'swr'
34
import useSWRInfinite from 'swr/infinite'
@@ -22,6 +23,7 @@ export interface UseOperationsParams {
2223

2324
disabled?: boolean
2425
suspense?: boolean
26+
revalidateFirstPage?: boolean
2527
}
2628

2729
export function useOperations({
@@ -35,6 +37,7 @@ export function useOperations({
3537
byMyself,
3638
disabled,
3739
suspense,
40+
revalidateFirstPage,
3841
}: UseOperationsParams) {
3942
const {
4043
error,
@@ -98,11 +101,14 @@ export function useOperations({
98101
requireData: true,
99102
}).queriesCopilot(req)
100103

101-
const parsedOperations: Operation[] = res.data.data.map((operation) => ({
104+
let parsedOperations: Operation[] = res.data.data.map((operation) => ({
102105
...operation,
103106
parsedContent: toCopilotOperation(operation),
104107
}))
105108

109+
// 如果 revalidateFirstPage=false,从第二页开始可能会有重复数据,需要去重
110+
parsedOperations = uniqBy(parsedOperations, (o) => o.id)
111+
106112
return {
107113
...res.data,
108114
data: parsedOperations,
@@ -111,6 +117,7 @@ export function useOperations({
111117
{
112118
suspense,
113119
focusThrottleInterval: 1000 * 60 * 30,
120+
revalidateFirstPage,
114121
},
115122
)
116123

src/components/Operations.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ export const Operations: ComponentType = withSuspensable(() => {
219219

220220
<div className="tabular-nums">
221221
{listMode === 'operation' && (
222-
<OperationList {...queryParams} operator={selectedOperatorQuery} />
222+
<OperationList
223+
{...queryParams}
224+
operator={selectedOperatorQuery}
225+
// 按热度排序时列表前几页的变化不会太频繁,可以不刷新第一页,节省点流量
226+
revalidateFirstPage={queryParams.orderBy !== 'hot'}
227+
/>
223228
)}
224229
{listMode === 'operationSet' && <OperationSetList {...queryParams} />}
225230
</div>

0 commit comments

Comments
 (0)