Skip to content

Commit 5b2a8b7

Browse files
committed
feat: searching operation set by short code
1 parent 01be696 commit 5b2a8b7

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/apis/operation-set.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useAtomValue } from 'jotai'
2+
import { noop } from 'lodash-es'
23
import {
34
CopilotSetPageRes,
45
CopilotSetQuery,
@@ -11,6 +12,8 @@ import { authAtom } from 'store/auth'
1112
import { OperationSetApi } from 'utils/maa-copilot-client'
1213
import { useSWRRefresh } from 'utils/swr'
1314

15+
import { parseShortCode } from '../models/shortCode'
16+
1417
export type OrderBy = 'views' | 'hot' | 'id'
1518

1619
export interface UseOperationSetsParams {
@@ -81,7 +84,65 @@ export function useOperationSets({
8184

8285
export function useRefreshOperationSets() {
8386
const refresh = useSWRRefresh()
84-
return () => refresh((key) => key.includes('operationSets'))
87+
return () =>
88+
refresh(
89+
(key) =>
90+
key.includes('operationSets') ||
91+
(key.includes('operationSet') && key.includes('fromList')),
92+
)
93+
}
94+
95+
export function useOperationSetSearch({
96+
keyword,
97+
suspense,
98+
disabled,
99+
...params
100+
}: UseOperationSetsParams) {
101+
if (!suspense) {
102+
throw new Error('useOperationSetSearch must be used with suspense')
103+
}
104+
if (disabled) {
105+
throw new Error('useOperationSetSearch cannot be disabled')
106+
}
107+
108+
let id: number | undefined
109+
110+
if (keyword) {
111+
const shortCodeContent = parseShortCode(keyword)
112+
113+
if (shortCodeContent) {
114+
if (shortCodeContent.type === 'operation') {
115+
throw new Error('该神秘代码属于作业,无法在此使用⊙﹏⊙∥')
116+
}
117+
118+
id = shortCodeContent.id
119+
}
120+
}
121+
122+
const { data: operationSet } = useOperationSet({ id, suspense })
123+
124+
const listResponse = useOperationSets({
125+
keyword,
126+
suspense,
127+
...params,
128+
129+
// disable the list query if we are fetching a single operation set
130+
disabled: !!id,
131+
})
132+
133+
if (id) {
134+
return {
135+
operationSets: [operationSet],
136+
isReachingEnd: true,
137+
setSize: noop,
138+
139+
// these are fixed values in suspense mode
140+
error: undefined,
141+
isValidating: false,
142+
}
143+
}
144+
145+
return listResponse
85146
}
86147

87148
interface UseOperationSetParams {

src/components/OperationSetList.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Button, NonIdealState } from '@blueprintjs/core'
22

3-
import { UseOperationSetsParams, useOperationSets } from 'apis/operation-set'
3+
import {
4+
UseOperationSetsParams,
5+
useOperationSetSearch,
6+
} from 'apis/operation-set'
47
import { useAtomValue } from 'jotai'
58
import { ComponentType, ReactNode } from 'react'
69

@@ -15,7 +18,7 @@ export const OperationSetList: ComponentType<UseOperationSetsParams> =
1518
const neoLayout = useAtomValue(neoLayoutAtom)
1619

1720
const { operationSets, setSize, isValidating, isReachingEnd } =
18-
useOperationSets({ ...props, suspense: true })
21+
useOperationSetSearch({ ...props, suspense: true })
1922

2023
// make TS happy: we got Suspense out there
2124
if (!operationSets) throw new Error('unreachable')

0 commit comments

Comments
 (0)