Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

TanstackQueryDevtoolsPanel.tsx

187 lines5.1 KB

1import { render } from 'solid-js/web'
2import { createSignal, lazy } from 'solid-js'
3import { setupStyleSheet } from './utils'
4import type {
5 QueryClient,
6 onlineManager as TOnlineManager,
7} from '@tanstack/query-core'
8import type { DevtoolsComponentType } from './Devtools'
9import type {
10 DevtoolsButtonPosition,
11 DevtoolsErrorType,
12 DevtoolsPosition,
13 QueryDevtoolsProps,
14 Theme,
15} from './contexts/types'
16import type { Signal } from 'solid-js'
17​
18/**
19 * The options for `TanstackQueryDevtoolsPanel`, which mounts the devtools as an
20 * embedded panel.
21 */
22export interface TanstackQueryDevtoolsPanelConfig extends QueryDevtoolsProps {
23 /**
24 * Use this to pass a nonce to the style tag that is added to the document
25 * head. This is useful if you are using a Content Security Policy (CSP) nonce
26 * to allow inline styles.
27 */
28 styleNonce?: string
29 /**
30 * Use this to pass a shadow DOM target to the devtools so that the styles
31 * will be applied within the shadow DOM instead of within the head tag in the
32 * light DOM.
33 */
34 shadowDOMTarget?: ShadowRoot
35 /** Callback function that is called when the devtools panel is closed. */
36 onClose?: () => void
38​
39class TanstackQueryDevtoolsPanel {
40 #client: Signal<QueryClient>
41 #onlineManager: typeof TOnlineManager
42 #queryFlavor: string
43 #version: string
44 #isMounted = false
45 #styleNonce?: string
46 #shadowDOMTarget?: ShadowRoot
47 #buttonPosition: Signal<DevtoolsButtonPosition | undefined>
48 #position: Signal<DevtoolsPosition | undefined>
49 #initialIsOpen: Signal<boolean | undefined>
50 #errorTypes: Signal<Array<DevtoolsErrorType> | undefined>
51 #hideDisabledQueries: Signal<boolean | undefined>
52 #onClose: Signal<(() => void) | undefined>
53 #Component: DevtoolsComponentType | undefined
54 #theme: Signal<Theme | undefined>
55 #dispose?: () => void
56​
57 constructor(config: TanstackQueryDevtoolsPanelConfig) {
58 const {
59 client,
60 queryFlavor,
61 version,
62 onlineManager,
63 buttonPosition,
64 position,
65 initialIsOpen,
66 errorTypes,
67 styleNonce,
68 shadowDOMTarget,
69 onClose,
70 hideDisabledQueries,
71 theme,
72 } = config
73 this.#client = createSignal(client)
74 this.#queryFlavor = queryFlavor
75 this.#version = version
76 this.#onlineManager = onlineManager
77 this.#styleNonce = styleNonce
78 this.#shadowDOMTarget = shadowDOMTarget
79 this.#buttonPosition = createSignal(buttonPosition)
80 this.#position = createSignal(position)
81 this.#initialIsOpen = createSignal(initialIsOpen)
82 this.#errorTypes = createSignal(errorTypes)
83 this.#hideDisabledQueries = createSignal(hideDisabledQueries)
84 this.#onClose = createSignal(onClose)
85 this.#theme = createSignal(theme)
86 }
87​
88 setButtonPosition(position: DevtoolsButtonPosition) {
89 this.#buttonPosition[1](position)
90 }
91​
92 setPosition(position: DevtoolsPosition) {
93 this.#position[1](position)
94 }
95​
96 setInitialIsOpen(isOpen: boolean) {
97 this.#initialIsOpen[1](isOpen)
98 }
99​
100 setErrorTypes(errorTypes: Array<DevtoolsErrorType>) {
101 this.#errorTypes[1](errorTypes)
102 }
103​
104 setClient(client: QueryClient) {
105 this.#client[1](client)
106 }
107​
108 setOnClose(onClose: () => void) {
109 this.#onClose[1](() => onClose)
110 }
111​
112 setTheme(theme?: Theme) {
113 this.#theme[1](theme)
114 }
115​
116 mount<T extends HTMLElement>(el: T) {
117 if (this.#isMounted) {
118 throw new Error('Devtools is already mounted')
119 }
120 const dispose = render(() => {
121 const [btnPosition] = this.#buttonPosition
122 const [pos] = this.#position
123 const [isOpen] = this.#initialIsOpen
124 const [errors] = this.#errorTypes
125 const [hideDisabledQueries] = this.#hideDisabledQueries
126 const [queryClient] = this.#client
127 const [onClose] = this.#onClose
128 const [theme] = this.#theme
129 let Devtools: DevtoolsComponentType
130​
131 if (this.#Component) {
132 Devtools = this.#Component
133 } else {
134 Devtools = lazy(() => import('./DevtoolsPanelComponent'))
135 this.#Component = Devtools
136 }
137​
138 setupStyleSheet(this.#styleNonce, this.#shadowDOMTarget)
139 return (
140 <Devtools
141 queryFlavor={this.#queryFlavor}
142 version={this.#version}
143 onlineManager={this.#onlineManager}
144 shadowDOMTarget={this.#shadowDOMTarget}
145 {...{
146 get client() {
147 return queryClient()
148 },
149 get buttonPosition() {
150 return btnPosition()
151 },
152 get position() {
153 return pos()
154 },
155 get initialIsOpen() {
156 return isOpen()
157 },
158 get errorTypes() {
159 return errors()
160 },
161 get hideDisabledQueries() {
162 return hideDisabledQueries()
163 },
164 get onClose() {
165 return onClose()
166 },
167 get theme() {
168 return theme()
169 },
170 }}
171 />
172 )
173 }, el)
174 this.#isMounted = true
175 this.#dispose = dispose
176 }
177​
178 unmount() {
179 if (!this.#isMounted) {
180 throw new Error('Devtools is not mounted')
181 }
182 this.#dispose?.()
183 this.#isMounted = false
184 }
186​
187export { TanstackQueryDevtoolsPanel }