-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathTanstackQueryDevtoolsPanel.tsx
More file actions
172 lines (157 loc) · 4.56 KB
/
TanstackQueryDevtoolsPanel.tsx
File metadata and controls
172 lines (157 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { render } from 'solid-js/web'
import { createSignal, lazy } from 'solid-js'
import { setupStyleSheet } from './utils'
import type {
QueryClient,
onlineManager as TOnlineManager,
} from '@tanstack/query-core'
import type { DevtoolsComponentType } from './Devtools'
import type {
DevtoolsButtonPosition,
DevtoolsErrorType,
DevtoolsPosition,
QueryDevtoolsProps,
Theme,
} from './contexts'
import type { Signal } from 'solid-js'
export interface TanstackQueryDevtoolsPanelConfig extends QueryDevtoolsProps {
styleNonce?: string
shadowDOMTarget?: ShadowRoot
onClose?: () => void
}
class TanstackQueryDevtoolsPanel {
#client: Signal<QueryClient>
#onlineManager: typeof TOnlineManager
#queryFlavor: string
#version: string
#isMounted = false
#styleNonce?: string
#shadowDOMTarget?: ShadowRoot
#buttonPosition: Signal<DevtoolsButtonPosition | undefined>
#position: Signal<DevtoolsPosition | undefined>
#initialIsOpen: Signal<boolean | undefined>
#errorTypes: Signal<Array<DevtoolsErrorType> | undefined>
#hideDisabledQueries: Signal<boolean | undefined>
#onClose: Signal<(() => unknown) | undefined>
#Component: DevtoolsComponentType | undefined
#theme: Signal<Theme | undefined>
#dispose?: () => void
constructor(config: TanstackQueryDevtoolsPanelConfig) {
const {
client,
queryFlavor,
version,
onlineManager,
buttonPosition,
position,
initialIsOpen,
errorTypes,
styleNonce,
shadowDOMTarget,
onClose,
hideDisabledQueries,
theme,
} = config
this.#client = createSignal(client)
this.#queryFlavor = queryFlavor
this.#version = version
this.#onlineManager = onlineManager
this.#styleNonce = styleNonce
this.#shadowDOMTarget = shadowDOMTarget
this.#buttonPosition = createSignal(buttonPosition)
this.#position = createSignal(position)
this.#initialIsOpen = createSignal(initialIsOpen)
this.#errorTypes = createSignal(errorTypes)
this.#hideDisabledQueries = createSignal(hideDisabledQueries)
this.#onClose = createSignal(onClose)
this.#theme = createSignal(theme)
}
setButtonPosition(position: DevtoolsButtonPosition) {
this.#buttonPosition[1](position)
}
setPosition(position: DevtoolsPosition) {
this.#position[1](position)
}
setInitialIsOpen(isOpen: boolean) {
this.#initialIsOpen[1](isOpen)
}
setErrorTypes(errorTypes: Array<DevtoolsErrorType>) {
this.#errorTypes[1](errorTypes)
}
setClient(client: QueryClient) {
this.#client[1](client)
}
setOnClose(onClose: () => unknown) {
this.#onClose[1](() => onClose)
}
setTheme(theme?: Theme) {
this.#theme[1](theme)
}
mount<T extends HTMLElement>(el: T) {
if (this.#isMounted) {
throw new Error('Devtools is already mounted')
}
const dispose = render(() => {
const [btnPosition] = this.#buttonPosition
const [pos] = this.#position
const [isOpen] = this.#initialIsOpen
const [errors] = this.#errorTypes
const [hideDisabledQueries] = this.#hideDisabledQueries
const [queryClient] = this.#client
const [onClose] = this.#onClose
const [theme] = this.#theme
let Devtools: DevtoolsComponentType
if (this.#Component) {
Devtools = this.#Component
} else {
Devtools = lazy(() => import('./DevtoolsPanelComponent'))
this.#Component = Devtools
}
setupStyleSheet(this.#styleNonce, this.#shadowDOMTarget)
return (
<Devtools
queryFlavor={this.#queryFlavor}
version={this.#version}
onlineManager={this.#onlineManager}
shadowDOMTarget={this.#shadowDOMTarget}
{...{
get client() {
return queryClient()
},
get buttonPosition() {
return btnPosition()
},
get position() {
return pos()
},
get initialIsOpen() {
return isOpen()
},
get errorTypes() {
return errors()
},
get hideDisabledQueries() {
return hideDisabledQueries()
},
get onClose() {
return onClose()
},
get theme() {
return theme()
},
}}
/>
)
}, el)
this.#isMounted = true
this.#dispose = dispose
}
unmount() {
if (!this.#isMounted) {
throw new Error('Devtools is not mounted')
}
this.#dispose?.()
this.#isMounted = false
}
}
export { TanstackQueryDevtoolsPanel }