Skip to content

Commit e8ac487

Browse files
committed
refactor: use exact authorId when searching for operations
1 parent 4365640 commit e8ac487

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

src/apis/operation-set.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useAtomValue } from 'jotai'
21
import { noop } from 'lodash-es'
32
import {
43
CopilotSetPageRes,
@@ -9,7 +8,6 @@ import {
98
import useSWR from 'swr'
109
import useSWRInfinite from 'swr/infinite'
1110

12-
import { authAtom } from 'store/auth'
1311
import { OperationSetApi } from 'utils/maa-copilot-client'
1412
import { useSWRRefresh } from 'utils/swr'
1513

@@ -19,20 +17,18 @@ export type OrderBy = 'views' | 'hot' | 'id'
1917

2018
export interface UseOperationSetsParams {
2119
keyword?: string
22-
byMyself?: boolean
20+
creatorId?: string
2321

2422
disabled?: boolean
2523
suspense?: boolean
2624
}
2725

2826
export function useOperationSets({
2927
keyword,
30-
byMyself,
28+
creatorId,
3129
disabled,
3230
suspense,
3331
}: UseOperationSetsParams) {
34-
const { userId } = useAtomValue(authAtom)
35-
3632
const {
3733
data: pages,
3834
error,
@@ -53,14 +49,13 @@ export function useOperationSets({
5349
limit: 50,
5450
page: pageIndex + 1,
5551
keyword,
56-
creatorId: byMyself ? userId : undefined,
52+
creatorId,
5753
} satisfies CopilotSetQuery,
58-
byMyself,
5954
]
6055
},
61-
async ([, req, byMyself]) => {
56+
async ([, req]) => {
6257
const res = await new OperationSetApi({
63-
sendToken: byMyself ? 'always' : 'optional', // 如果有 token 会用来获取自己的作业集
58+
sendToken: 'optional', // 如果有 token 即可获取到私有的作业集
6459
requireData: true,
6560
}).querySets({ copilotSetQuery: req })
6661
return res.data

src/apis/operation.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface UseOperationsParams {
1919
levelKeyword?: string
2020
operator?: string
2121
operationIds?: number[]
22-
byMyself?: boolean
22+
uploaderId?: string
2323

2424
disabled?: boolean
2525
suspense?: boolean
@@ -34,7 +34,7 @@ export function useOperations({
3434
levelKeyword,
3535
operator,
3636
operationIds,
37-
byMyself,
37+
uploaderId,
3838
disabled,
3939
suspense,
4040
revalidateFirstPage,
@@ -84,7 +84,7 @@ export function useOperations({
8484
orderBy,
8585
desc: descending,
8686
copilotIds: operationIds,
87-
uploaderId: byMyself ? 'me' : undefined,
87+
uploaderId,
8888
} satisfies QueriesCopilotRequest,
8989
]
9090
},
@@ -96,8 +96,7 @@ export function useOperations({
9696
}
9797

9898
const res = await new OperationApi({
99-
sendToken:
100-
'uploaderId' in req && req.uploaderId === 'me' ? 'always' : 'never',
99+
sendToken: 'optional',
101100
requireData: true,
102101
}).queriesCopilot(req)
103102

src/components/Operations.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export const Operations: ComponentType = withSuspensable(() => {
6565
className=""
6666
icon="user"
6767
title="只显示我发布的作品"
68-
active={queryParams.byMyself}
68+
active={!!queryParams.uploaderId}
6969
onClick={() => {
7070
setQueryParams((old) => ({
7171
...old,
72-
byMyself: !old.byMyself,
72+
uploaderId: old.uploaderId ? undefined : authState.userId,
7373
}))
7474
}}
7575
>
@@ -209,7 +209,12 @@ export const Operations: ComponentType = withSuspensable(() => {
209209
revalidateFirstPage={queryParams.orderBy !== 'hot'}
210210
/>
211211
)}
212-
{listMode === 'operationSet' && <OperationSetList {...queryParams} />}
212+
{listMode === 'operationSet' && (
213+
<OperationSetList
214+
{...queryParams}
215+
creatorId={queryParams.uploaderId}
216+
/>
217+
)}
213218
</div>
214219
</>
215220
)

src/components/operation-set/AddToOperationSet.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import {
1414
useOperationSets,
1515
} from 'apis/operation-set'
1616
import clsx from 'clsx'
17+
import { useAtomValue } from 'jotai'
1718
import { useState } from 'react'
1819

1920
import { AppToaster } from 'components/Toaster'
2021
import { OperationSetEditorDialog } from 'components/operation-set/OperationSetEditor'
2122
import { formatError } from 'utils/error'
2223
import { useNetworkState } from 'utils/useNetworkState'
2324

25+
import { authAtom } from '../../store/auth'
26+
2427
interface AddToOperationSetButtonProps extends ButtonProps {
2528
operationId: number
2629
}
@@ -60,14 +63,17 @@ export function AddToOperationSet({
6063
operationId,
6164
onSuccess,
6265
}: AddToOperationSetProps) {
66+
const auth = useAtomValue(authAtom)
67+
6368
const {
6469
operationSets,
6570
isReachingEnd,
6671
isValidating,
6772
setSize,
6873
error: listError,
6974
} = useOperationSets({
70-
byMyself: true,
75+
disabled: !auth.userId,
76+
creatorId: auth.userId,
7177
})
7278

7379
const {
@@ -82,7 +88,8 @@ export function AddToOperationSet({
8288
{} as Record<number, boolean>,
8389
)
8490

85-
const error = submitError || listError
91+
const error =
92+
submitError || listError || (!auth.userId ? '未登录' : undefined)
8693

8794
const operationSetList = onlyShowAdded
8895
? operationSets?.filter(

0 commit comments

Comments
 (0)