1
1
import { useAtomValue } from 'jotai'
2
+ import { noop } from 'lodash-es'
2
3
import {
3
4
CopilotSetPageRes ,
4
5
CopilotSetQuery ,
@@ -11,6 +12,8 @@ import { authAtom } from 'store/auth'
11
12
import { OperationSetApi } from 'utils/maa-copilot-client'
12
13
import { useSWRRefresh } from 'utils/swr'
13
14
15
+ import { parseShortCode } from '../models/shortCode'
16
+
14
17
export type OrderBy = 'views' | 'hot' | 'id'
15
18
16
19
export interface UseOperationSetsParams {
@@ -81,7 +84,65 @@ export function useOperationSets({
81
84
82
85
export function useRefreshOperationSets ( ) {
83
86
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
85
146
}
86
147
87
148
interface UseOperationSetParams {
0 commit comments