Open History to see the last commit
1import { createSignal, lazy } from 'solid-js'
2import { render, setTrustedTypesPolicy } from 'solid-js/web'
3import type {
4 QueryClient,
5 onlineManager as TOnlineManager,
6} from '@tanstack/query-core'
7import type { Signal } from 'solid-js'
8import type { DevtoolsComponentType } from './Devtools'
9import type {
10 DevtoolsButtonPosition,
11 DevtoolsErrorType,
12 DevtoolsPosition,
13 QueryDevtoolsProps,
14 Theme,
15} from './contexts/types'
16β
17/**
18 * The options for `TanstackQueryDevtoolsPanel`, which mounts the devtools as an embedded panel.
19 */
20export interface TanstackQueryDevtoolsPanelConfig extends QueryDevtoolsProps {
21 /**
22 * @deprecated The devtools no longer add a `<style>` element, so they need no nonce. This option
23 * does nothing.
24 */
25 styleNonce?: string
26 /**
27 * The shadow root to add the devtools styles to. By default, the styles are added to the shadow
28 * root the devtools render in, or to the document when they do not render in a shadow root.
29 */
30 shadowDOMTarget?: ShadowRoot
31 /**
32 * The Trusted Types policy the devtools use to create their HTML templates.
33 * Pass one when your page enforces `require-trusted-types-for 'script'`.
34 * The devtools only pass it their own markup.
35 */
36 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
37 /**
38 * Callback function that is called when the devtools panel is closed.
39 */
40 onClose?: () => void
41}
42β
43class TanstackQueryDevtoolsPanel {
44 #client: Signal<QueryClient>
45 #onlineManager: typeof TOnlineManager
46 #queryFlavor: string
47 #version: string
48 #isMounted = false
49 #shadowDOMTarget?: ShadowRoot
50 #trustedTypesPolicy?: { createHTML: (html: string) => unknown }
51 #buttonPosition: Signal<DevtoolsButtonPosition | undefined>
52 #position: Signal<DevtoolsPosition | undefined>
53 #initialIsOpen: Signal<boolean | undefined>
54 #errorTypes: Signal<Array<DevtoolsErrorType> | undefined>
55 #hideDisabledQueries: Signal<boolean | undefined>
56 #onClose: Signal<(() => void) | undefined>
57 #Component: DevtoolsComponentType | undefined
58 #theme: Signal<Theme | undefined>
59 #dispose?: () => void
60β
61 constructor(config: TanstackQueryDevtoolsPanelConfig) {
62 const {
63 client,
64 queryFlavor,
65 version,
66 onlineManager,
67 buttonPosition,
68 position,
69 initialIsOpen,
70 errorTypes,
71 shadowDOMTarget,
72 trustedTypesPolicy,
73 onClose,
74 hideDisabledQueries,
75 theme,
76 } = config
77 this.#client = createSignal(client)
78 this.#queryFlavor = queryFlavor
79 this.#version = version
80 this.#onlineManager = onlineManager
81 this.#shadowDOMTarget = shadowDOMTarget
82 this.#trustedTypesPolicy = trustedTypesPolicy
83 this.#buttonPosition = createSignal(buttonPosition)
84 this.#position = createSignal(position)
85 this.#initialIsOpen = createSignal(initialIsOpen)
86 this.#errorTypes = createSignal(errorTypes)
87 this.#hideDisabledQueries = createSignal(hideDisabledQueries)
88 this.#onClose = createSignal(onClose)
89 this.#theme = createSignal(theme)
90 }
91β
92 setButtonPosition(position: DevtoolsButtonPosition) {
93 this.#buttonPosition[1](position)
94 }
95β
96 setPosition(position: DevtoolsPosition) {
97 this.#position[1](position)
98 }
99β
100 setInitialIsOpen(isOpen: boolean) {
101 this.#initialIsOpen[1](isOpen)
102 }
103β
104 setErrorTypes(errorTypes: Array<DevtoolsErrorType>) {
105 this.#errorTypes[1](errorTypes)
106 }
107β
108 setClient(client: QueryClient) {
109 this.#client[1](client)
110 }
111β
112 setOnClose(onClose: () => void) {
113 this.#onClose[1](() => onClose)
114 }
115β
116 setTheme(theme?: Theme) {
117 this.#theme[1](theme)
118 }
119β
120 mount<T extends HTMLElement>(el: T) {
121 if (this.#isMounted) {
122 throw new Error('Devtools is already mounted')
123 }
124 // Solid creates each template once, so set the policy before rendering.
125 // It is shared by every devtools instance, so never reset it.
126 if (this.#trustedTypesPolicy) {
127 setTrustedTypesPolicy(this.#trustedTypesPolicy)
128 }
129 const dispose = render(() => {
130 const [btnPosition] = this.#buttonPosition
131 const [pos] = this.#position
132 const [isOpen] = this.#initialIsOpen
133 const [errors] = this.#errorTypes
134 const [hideDisabledQueries] = this.#hideDisabledQueries
135 const [queryClient] = this.#client
136 const [onClose] = this.#onClose
137 const [theme] = this.#theme
138 let Devtools: DevtoolsComponentType
139β
140 if (this.#Component) {
141 Devtools = this.#Component
142 } else {
143 Devtools = lazy(() => import('./DevtoolsPanelComponent'))
144 this.#Component = Devtools
145 }
146β
147 return (
148 <Devtools
149 queryFlavor={this.#queryFlavor}
150 version={this.#version}
151 onlineManager={this.#onlineManager}
152 shadowDOMTarget={this.#shadowDOMTarget}
153 {...{
154 get client() {
155 return queryClient()
156 },
157 get buttonPosition() {
158 return btnPosition()
159 },
160 get position() {
161 return pos()
162 },
163 get initialIsOpen() {
164 return isOpen()
165 },
166 get errorTypes() {
167 return errors()
168 },
169 get hideDisabledQueries() {
170 return hideDisabledQueries()
171 },
172 get onClose() {
173 return onClose()
174 },
175 get theme() {
176 return theme()
177 },
178 }}
179 />
180 )
181 }, el)
182 this.#isMounted = true
183 this.#dispose = dispose
184 }
185β
186 unmount() {
187 if (!this.#isMounted) {
188 throw new Error('Devtools is not mounted')
189 }
190 this.#dispose?.()
191 this.#isMounted = false
192 }
193}
194β
195export { TanstackQueryDevtoolsPanel }