@@ -12,39 +12,41 @@ import {
12
12
} from 'react-hook-form' ;
13
13
import { useTranslation } from 'react-i18next' ;
14
14
import { useEffect , useMemo } from 'react' ;
15
- import { useQuery , useSuspenseQuery } from '@tanstack/react-query' ;
16
- import {
17
- getPluginSchemaQueryOptions ,
18
- getPluginsListQueryOptions ,
19
- } from '@/apis/plugins' ;
20
15
import { PluginCardList , PluginCardListSearch } from './PluginCardList' ;
21
16
import { SelectPluginsDrawer } from './SelectPluginsDrawer' ;
22
17
import { difference } from 'rambdax' ;
23
- import {
24
- PluginEditorDrawer ,
25
- type PluginConfig ,
26
- type PluginEditorDrawerProps ,
27
- } from './PluginEditorDrawer' ;
18
+ import { PluginEditorDrawer , type PluginConfig } from './PluginEditorDrawer' ;
28
19
import { observer , useLocalObservable } from 'mobx-react-lite' ;
29
20
import type { PluginCardProps } from './PluginCard' ;
21
+ import {
22
+ getPluginsListWithSchemaQueryOptions ,
23
+ type NeedPluginSchema ,
24
+ } from '@/apis/plugins' ;
25
+ import { useSuspenseQuery } from '@tanstack/react-query' ;
26
+ import { useDeepCompareEffect } from 'react-use' ;
27
+ import type { APISIXType } from '@/types/schema/apisix' ;
28
+ import { toJS } from 'mobx' ;
30
29
31
30
export type FormItemPluginsProps < T extends FieldValues > = InputWrapperProps &
32
31
UseControllerProps < T > & {
33
32
onChange ?: ( value : Record < string , unknown > ) => void ;
34
- } & Pick < PluginEditorDrawerProps , 'schema' > ;
33
+ } & Partial < NeedPluginSchema > ;
35
34
36
35
const FormItemPluginsCore = < T extends FieldValues > (
37
36
props : FormItemPluginsProps < T >
38
37
) => {
39
- const { controllerProps, restProps } = genControllerProps ( props , { } ) ;
38
+ const {
39
+ controllerProps,
40
+ restProps : { schema = 'schema' , ...restProps } ,
41
+ } = genControllerProps ( props , { } ) ;
40
42
const { t } = useTranslation ( ) ;
41
43
42
44
const {
43
45
field : { value : rawObject , onChange : fOnChange , name : fName , ...restField } ,
44
46
fieldState,
45
47
} = useController < T > ( controllerProps ) ;
46
48
const isView = useMemo ( ( ) => restField . disabled , [ restField . disabled ] ) ;
47
- const pluginsListReq = useSuspenseQuery ( getPluginsListQueryOptions ( ) ) ;
49
+
48
50
const pluginsOb = useLocalObservable ( ( ) => ( {
49
51
__map : new Map < string , object > ( ) ,
50
52
init ( obj : Record < string , object > ) {
@@ -54,11 +56,21 @@ const FormItemPluginsCore = <T extends FieldValues>(
54
56
this . __map . delete ( name ) ;
55
57
this . save ( ) ;
56
58
} ,
59
+ allPluginNames : [ ] as string [ ] ,
60
+ pluginSchemaObj : new Map < string , APISIXType [ 'PluginSchema' ] > ( ) ,
61
+ initPlugins ( props : {
62
+ names : string [ ] ;
63
+ originObj : Record < string , Record < string , unknown > > ;
64
+ } ) {
65
+ const { names, originObj } = props ;
66
+ this . allPluginNames = names ;
67
+ this . pluginSchemaObj = new Map ( Object . entries ( originObj ) ) ;
68
+ } ,
57
69
get selected ( ) {
58
70
return Array . from ( this . __map . keys ( ) ) ;
59
71
} ,
60
72
get unSelected ( ) {
61
- return difference ( pluginsListReq . data , this . selected ) ;
73
+ return difference ( this . allPluginNames , this . selected ) ;
62
74
} ,
63
75
save ( ) {
64
76
const obj = Object . fromEntries ( this . __map ) ;
@@ -77,6 +89,11 @@ const FormItemPluginsCore = <T extends FieldValues>(
77
89
} as PluginConfig ;
78
90
this . setEditorOpened ( true ) ;
79
91
} ,
92
+ get curPluginSchema ( ) {
93
+ const d = this . pluginSchemaObj . get ( this . curPlugin . name ) ;
94
+ if ( ! d ) return { } ;
95
+ return d [ schema ] ;
96
+ } ,
80
97
editorOpened : false ,
81
98
setEditorOpened ( val : boolean ) {
82
99
this . editorOpened = val ;
@@ -101,14 +118,17 @@ const FormItemPluginsCore = <T extends FieldValues>(
101
118
} ,
102
119
} ) ) ;
103
120
104
- const getSchemaReq = useQuery (
105
- getPluginSchemaQueryOptions ( pluginsOb . curPlugin . name )
121
+ const pluginsListReq = useSuspenseQuery (
122
+ getPluginsListWithSchemaQueryOptions ( { schema } )
106
123
) ;
107
124
108
125
// init the selected plugins
109
126
useEffect ( ( ) => {
110
127
pluginsOb . init ( rawObject ) ;
111
128
} , [ pluginsOb , rawObject ] ) ;
129
+ useDeepCompareEffect ( ( ) => {
130
+ pluginsOb . initPlugins ( pluginsListReq . data ) ;
131
+ } , [ pluginsOb , pluginsListReq . data ] ) ;
112
132
113
133
return (
114
134
< InputWrapper error = { fieldState . error ?. message } { ...restProps } >
@@ -122,8 +142,8 @@ const FormItemPluginsCore = <T extends FieldValues>(
122
142
< SelectPluginsDrawer
123
143
plugins = { pluginsOb . unSelected }
124
144
opened = { pluginsOb . selectPluginsOpened }
125
- onAdd = { ( name ) => pluginsOb . on ( 'add' , name ) }
126
145
setOpened = { pluginsOb . setSelectPluginsOpened }
146
+ onAdd = { ( name ) => pluginsOb . on ( 'add' , name ) }
127
147
disabled = { restField . disabled }
128
148
/>
129
149
</ Group >
@@ -139,7 +159,7 @@ const FormItemPluginsCore = <T extends FieldValues>(
139
159
/>
140
160
< PluginEditorDrawer
141
161
mode = { isView ? 'view' : 'edit' }
142
- schema = { getSchemaReq . data }
162
+ schema = { toJS ( pluginsOb . curPluginSchema ) }
143
163
opened = { pluginsOb . editorOpened }
144
164
onClose = { pluginsOb . closeEditor }
145
165
plugin = { pluginsOb . curPlugin }
@@ -151,3 +171,4 @@ const FormItemPluginsCore = <T extends FieldValues>(
151
171
} ;
152
172
153
173
export const FormItemPlugins = observer ( FormItemPluginsCore ) ;
174
+
0 commit comments