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