3
3
PLUGIN_CONTAINER_ID ,
4
4
PLUGIN_TITLE_CONTAINER_ID ,
5
5
TanStackDevtoolsCore ,
6
+ generatePluginId ,
6
7
} from '@tanstack/devtools'
7
8
import { createPortal } from 'react-dom'
8
9
import type { JSX , ReactElement } from 'react'
@@ -109,49 +110,67 @@ export const TanStackDevtools = ({
109
110
eventBusConfig,
110
111
} : TanStackDevtoolsReactInit ) : ReactElement | null => {
111
112
const devToolRef = useRef < HTMLDivElement > ( null )
112
- const [ pluginContainer , setPluginContainer ] = useState < HTMLElement | null > (
113
- null ,
113
+ const [ pluginContainers , setPluginContainers ] = useState < Array < HTMLElement > > (
114
+ [ ] ,
114
115
)
115
- const [ titleContainer , setTitleContainer ] = useState < HTMLElement | null > ( null )
116
- const [ PluginComponent , setPluginComponent ] = useState < JSX . Element | null > (
117
- null ,
116
+ const [ titleContainers , setTitleContainers ] = useState < Array < HTMLElement > > ( [ ] )
117
+ const [ PluginComponents , setPluginComponents ] = useState < Array < JSX . Element > > (
118
+ [ ] ,
118
119
)
119
- const [ TitleComponent , setTitleComponent ] = useState < JSX . Element | null > ( null )
120
+ const [ TitleComponents , setTitleComponents ] = useState < Array < JSX . Element > > ( [ ] )
121
+
120
122
const [ devtools ] = useState (
121
123
( ) =>
122
124
new TanStackDevtoolsCore ( {
123
125
config,
124
126
eventBusConfig,
125
- plugins : plugins ?. map ( ( plugin ) => {
127
+ plugins : plugins ?. map ( ( plugin , index ) => {
126
128
return {
127
129
...plugin ,
128
130
name :
129
131
typeof plugin . name === 'string'
130
132
? plugin . name
131
133
: // The check above confirms that `plugin.name` is of Render type
132
134
( e , theme ) => {
133
- setTitleContainer (
134
- e . ownerDocument . getElementById (
135
- PLUGIN_TITLE_CONTAINER_ID ,
136
- ) || null ,
135
+ const target = e . ownerDocument . getElementById (
136
+ // @ts -ignore just testing
137
+ `${ PLUGIN_TITLE_CONTAINER_ID } -${ generatePluginId ( plugin , index ) } ` ,
137
138
)
139
+ if ( target ) {
140
+ setTitleContainers ( ( prev ) => [ ...prev , target ] )
141
+ }
138
142
convertRender (
139
143
plugin . name as PluginRender ,
140
- setTitleComponent ,
144
+ ( newVal ) =>
145
+ // @ts -ignore just testing
146
+ setTitleComponents ( ( prev ) => [ ...prev , newVal ] ) ,
141
147
e ,
142
148
theme ,
143
149
)
144
150
} ,
145
151
render : ( e , theme ) => {
146
- setPluginContainer (
147
- e . ownerDocument . getElementById ( PLUGIN_CONTAINER_ID ) || null ,
152
+ const target = e . ownerDocument . getElementById (
153
+ // @ts -ignore just testing
154
+ `${ PLUGIN_CONTAINER_ID } -${ generatePluginId ( plugin , index ) } ` ,
155
+ )
156
+ if ( target ) {
157
+ setPluginContainers ( ( prev ) => [ ...prev , target ] )
158
+ }
159
+
160
+ convertRender (
161
+ plugin . render ,
162
+ ( newVal ) =>
163
+ // @ts -ignore just testing
164
+ setPluginComponents ( ( prev ) => [ ...prev , newVal ] ) ,
165
+ e ,
166
+ theme ,
148
167
)
149
- convertRender ( plugin . render , setPluginComponent , e , theme )
150
168
} ,
151
169
}
152
170
} ) ,
153
171
} ) ,
154
172
)
173
+
155
174
useEffect ( ( ) => {
156
175
if ( devToolRef . current ) {
157
176
devtools . mount ( devToolRef . current )
@@ -163,11 +182,17 @@ export const TanStackDevtools = ({
163
182
return (
164
183
< >
165
184
< div style = { { position : 'absolute' } } ref = { devToolRef } />
166
- { pluginContainer && PluginComponent
167
- ? createPortal ( < > { PluginComponent } </ > , pluginContainer )
185
+
186
+ { pluginContainers . length > 0 && PluginComponents . length > 0
187
+ ? pluginContainers . map ( ( pluginContainer , index ) =>
188
+ createPortal ( < > { PluginComponents [ index ] } </ > , pluginContainer ) ,
189
+ )
168
190
: null }
169
- { titleContainer && TitleComponent
170
- ? createPortal ( < > { TitleComponent } </ > , titleContainer )
191
+
192
+ { titleContainers . length > 0 && TitleComponents . length > 0
193
+ ? titleContainers . map ( ( titleContainer , index ) =>
194
+ createPortal ( < > { TitleComponents [ index ] } </ > , titleContainer ) ,
195
+ )
171
196
: null }
172
197
</ >
173
198
)
0 commit comments