|
| 1 | +import { useAtomValue } from 'jotai' |
| 2 | +import { |
| 3 | + CopilotSetPageRes, |
| 4 | + CopilotSetQuery, |
| 5 | + CopilotSetStatus, |
| 6 | +} from 'maa-copilot-client' |
| 7 | +import useSWR from 'swr' |
| 8 | +import useSWRInfinite from 'swr/infinite' |
| 9 | + |
| 10 | +import { authAtom } from 'store/auth' |
| 11 | +import { OperationSetApi } from 'utils/maa-copilot-client' |
| 12 | +import { useSWRRefresh } from 'utils/swr' |
| 13 | + |
| 14 | +export type OrderBy = 'views' | 'hot' | 'id' |
| 15 | + |
| 16 | +export interface UseOperationSetsParams { |
| 17 | + keyword?: string |
| 18 | + byMyself?: boolean |
| 19 | + |
| 20 | + disabled?: boolean |
| 21 | + suspense?: boolean |
| 22 | +} |
| 23 | + |
| 24 | +export function useOperationSets({ |
| 25 | + keyword, |
| 26 | + byMyself, |
| 27 | + disabled, |
| 28 | + suspense, |
| 29 | +}: UseOperationSetsParams) { |
| 30 | + const { userId } = useAtomValue(authAtom) |
| 31 | + |
| 32 | + const { |
| 33 | + data: pages, |
| 34 | + error, |
| 35 | + setSize, |
| 36 | + isValidating, |
| 37 | + } = useSWRInfinite( |
| 38 | + (pageIndex, previousPage: CopilotSetPageRes) => { |
| 39 | + if (disabled) { |
| 40 | + return null |
| 41 | + } |
| 42 | + if (previousPage && !previousPage.hasNext) { |
| 43 | + return null // reached the end |
| 44 | + } |
| 45 | + |
| 46 | + return [ |
| 47 | + 'operationSets', |
| 48 | + { |
| 49 | + limit: 50, |
| 50 | + page: pageIndex + 1, |
| 51 | + keyword, |
| 52 | + creatorId: byMyself ? userId : undefined, |
| 53 | + } satisfies CopilotSetQuery, |
| 54 | + byMyself, |
| 55 | + ] |
| 56 | + }, |
| 57 | + async ([, req, byMyself]) => { |
| 58 | + const res = await new OperationSetApi({ |
| 59 | + sendToken: byMyself ? 'always' : 'optional', // 如果有 token 会用来获取自己的作业集 |
| 60 | + requireData: true, |
| 61 | + }).querySets({ copilotSetQuery: req }) |
| 62 | + return res.data |
| 63 | + }, |
| 64 | + { |
| 65 | + suspense, |
| 66 | + focusThrottleInterval: 1000 * 60 * 30, |
| 67 | + }, |
| 68 | + ) |
| 69 | + |
| 70 | + const isReachingEnd = !!pages?.some((page) => !page.hasNext) |
| 71 | + const operationSets = pages?.map((page) => page.data).flat() |
| 72 | + |
| 73 | + return { |
| 74 | + operationSets, |
| 75 | + error, |
| 76 | + setSize, |
| 77 | + isValidating, |
| 78 | + isReachingEnd, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export function useRefreshOperationSets() { |
| 83 | + const refresh = useSWRRefresh() |
| 84 | + return () => refresh((key) => key.includes('operationSets')) |
| 85 | +} |
| 86 | + |
| 87 | +interface UseOperationSetParams { |
| 88 | + id?: number |
| 89 | + suspense?: boolean |
| 90 | +} |
| 91 | + |
| 92 | +export function useOperationSet({ id, suspense }: UseOperationSetParams) { |
| 93 | + return useSWR( |
| 94 | + id ? ['operationSet', id] : null, |
| 95 | + () => getOperationSet({ id: id! }), |
| 96 | + { suspense }, |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +export function useRefreshOperationSet() { |
| 101 | + const refresh = useSWRRefresh() |
| 102 | + return (id: number) => |
| 103 | + refresh((key) => key.includes('operationSet') && key.includes(String(id))) |
| 104 | +} |
| 105 | + |
| 106 | +export async function getOperationSet(req: { id: number }) { |
| 107 | + const res = await new OperationSetApi({ |
| 108 | + sendToken: 'optional', // 如果有 token 会用来获取用户是否点赞 |
| 109 | + requireData: true, |
| 110 | + }).getSet(req) |
| 111 | + return res.data |
| 112 | +} |
| 113 | + |
| 114 | +export async function createOperationSet(req: { |
| 115 | + name: string |
| 116 | + description: string |
| 117 | + operationIds: number[] |
| 118 | + status: CopilotSetStatus |
| 119 | +}) { |
| 120 | + await new OperationSetApi().createSet({ |
| 121 | + copilotSetCreateReq: { |
| 122 | + name: req.name, |
| 123 | + description: req.description, |
| 124 | + copilotIds: req.operationIds, |
| 125 | + status: req.status, |
| 126 | + }, |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +export async function updateOperationSet(req: { |
| 131 | + id: number |
| 132 | + name: string |
| 133 | + description: string |
| 134 | + status: CopilotSetStatus |
| 135 | +}) { |
| 136 | + await new OperationSetApi().updateCopilotSet({ copilotSetUpdateReq: req }) |
| 137 | +} |
| 138 | + |
| 139 | +export async function deleteOperationSet(req: { id: number }) { |
| 140 | + await new OperationSetApi().deleteCopilotSet({ commonIdReqLong: req }) |
| 141 | +} |
| 142 | + |
| 143 | +export async function addToOperationSet(req: { |
| 144 | + operationSetId: number |
| 145 | + operationIds: number[] |
| 146 | +}) { |
| 147 | + await new OperationSetApi().addCopilotIds({ |
| 148 | + copilotSetModCopilotsReq: { |
| 149 | + id: req.operationSetId, |
| 150 | + copilotIds: req.operationIds, |
| 151 | + }, |
| 152 | + }) |
| 153 | +} |
| 154 | + |
| 155 | +export async function removeFromOperationSet(req: { |
| 156 | + operationSetId: number |
| 157 | + operationIds: number[] |
| 158 | +}) { |
| 159 | + await new OperationSetApi().removeCopilotIds({ |
| 160 | + copilotSetModCopilotsReq: { |
| 161 | + id: req.operationSetId, |
| 162 | + copilotIds: req.operationIds, |
| 163 | + }, |
| 164 | + }) |
| 165 | +} |
0 commit comments