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'
@@ -103,47 +104,59 @@ export const TanStackDevtools = ({
103
104
eventBusConfig,
104
105
} : TanStackDevtoolsReactInit ) : ReactElement | null => {
105
106
const devToolRef = useRef < HTMLDivElement > ( null )
106
- const [ pluginContainer , setPluginContainer ] = useState < HTMLElement | null > (
107
- null ,
107
+ const [ pluginContainers , setPluginContainers ] = useState < Array < HTMLElement > > (
108
+ [ ] ,
108
109
)
109
- const [ titleContainer , setTitleContainer ] = useState < HTMLElement | null > ( null )
110
- const [ PluginComponent , setPluginComponent ] = useState < JSX . Element | null > (
111
- null ,
110
+ const [ titleContainers , setTitleContainers ] = useState < Array < HTMLElement > > ( [ ] )
111
+ const [ PluginComponents , setPluginComponents ] = useState < Array < JSX . Element > > (
112
+ [ ] ,
112
113
)
113
- const [ TitleComponent , setTitleComponent ] = useState < JSX . Element | null > ( null )
114
+ const [ TitleComponents , setTitleComponents ] = useState < Array < JSX . Element > > ( [ ] )
115
+
114
116
const [ devtools ] = useState (
115
117
( ) =>
116
118
new TanStackDevtoolsCore ( {
117
119
config,
118
120
eventBusConfig,
119
- plugins : plugins ?. map ( ( plugin ) => {
121
+ plugins : plugins ?. map ( ( plugin , index ) => {
120
122
return {
121
123
...plugin ,
122
124
name :
123
125
typeof plugin . name === 'string'
124
126
? plugin . name
125
127
: // The check above confirms that `plugin.name` is of Render type
126
128
( e ) => {
127
- setTitleContainer (
128
- e . ownerDocument . getElementById (
129
- PLUGIN_TITLE_CONTAINER_ID ,
130
- ) || null ,
129
+ const target = e . ownerDocument . getElementById (
130
+ // @ts -ignore just testing
131
+ `${ PLUGIN_TITLE_CONTAINER_ID } -${ generatePluginId ( plugin , index ) } ` ,
131
132
)
132
- convertRender (
133
- plugin . name as PluginRender ,
134
- setTitleComponent ,
133
+ if ( target ) {
134
+ setTitleContainers ( ( prev ) => [ ...prev , target ] )
135
+ }
136
+ convertRender ( plugin . name as PluginRender , ( newVal ) =>
137
+ // @ts -ignore just testing
138
+ setTitleComponents ( ( prev ) => [ ...prev , newVal ] ) ,
135
139
)
136
140
} ,
137
141
render : ( e ) => {
138
- setPluginContainer (
139
- e . ownerDocument . getElementById ( PLUGIN_CONTAINER_ID ) || null ,
142
+ const target = e . ownerDocument . getElementById (
143
+ // @ts -ignore just testing
144
+ `${ PLUGIN_CONTAINER_ID } -${ generatePluginId ( plugin , index ) } ` ,
145
+ )
146
+ if ( target ) {
147
+ setPluginContainers ( ( prev ) => [ ...prev , target ] )
148
+ }
149
+
150
+ convertRender ( plugin . render , ( newVal ) =>
151
+ // @ts -ignore just testing
152
+ setPluginComponents ( ( prev ) => [ ...prev , newVal ] ) ,
140
153
)
141
- convertRender ( plugin . render , setPluginComponent )
142
154
} ,
143
155
}
144
156
} ) ,
145
157
} ) ,
146
158
)
159
+
147
160
useEffect ( ( ) => {
148
161
if ( devToolRef . current ) {
149
162
devtools . mount ( devToolRef . current )
@@ -155,11 +168,17 @@ export const TanStackDevtools = ({
155
168
return (
156
169
< >
157
170
< div style = { { position : 'absolute' } } ref = { devToolRef } />
158
- { pluginContainer && PluginComponent
159
- ? createPortal ( < > { PluginComponent } </ > , pluginContainer )
171
+
172
+ { pluginContainers . length > 0 && PluginComponents . length > 0
173
+ ? pluginContainers . map ( ( pluginContainer , index ) =>
174
+ createPortal ( < > { PluginComponents [ index ] } </ > , pluginContainer ) ,
175
+ )
160
176
: null }
161
- { titleContainer && TitleComponent
162
- ? createPortal ( < > { TitleComponent } </ > , titleContainer )
177
+
178
+ { titleContainers . length > 0 && TitleComponents . length > 0
179
+ ? titleContainers . map ( ( titleContainer , index ) =>
180
+ createPortal ( < > { TitleComponents [ index ] } </ > , titleContainer ) ,
181
+ )
163
182
: null }
164
183
</ >
165
184
)
0 commit comments