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

TanstackQueryDevtools.tsx

180 lines5.1 KB

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 `TanstackQueryDevtools`, which mounts the floating devtools.
19 */
20export interface TanstackQueryDevtoolsConfig 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 }
38​
39class TanstackQueryDevtools {
40 #client: Signal<QueryClient>
41 #onlineManager: typeof TOnlineManager
42 #queryFlavor: string
43 #version: string
44 #isMounted = false
45 #shadowDOMTarget?: ShadowRoot
46 #trustedTypesPolicy?: { createHTML: (html: string) => unknown }
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 #Component: DevtoolsComponentType | undefined
53 #theme: Signal<Theme | undefined>
54 #dispose?: () => void
55​
56 constructor(config: TanstackQueryDevtoolsConfig) {
57 const {
58 client,
59 queryFlavor,
60 version,
61 onlineManager,
62 buttonPosition,
63 position,
64 initialIsOpen,
65 errorTypes,
66 shadowDOMTarget,
67 trustedTypesPolicy,
68 hideDisabledQueries,
69 theme,
70 } = config
71 this.#client = createSignal(client)
72 this.#queryFlavor = queryFlavor
73 this.#version = version
74 this.#onlineManager = onlineManager
75 this.#shadowDOMTarget = shadowDOMTarget
76 this.#trustedTypesPolicy = trustedTypesPolicy
77 this.#buttonPosition = createSignal(buttonPosition)
78 this.#position = createSignal(position)
79 this.#initialIsOpen = createSignal(initialIsOpen)
80 this.#errorTypes = createSignal(errorTypes)
81 this.#hideDisabledQueries = createSignal(hideDisabledQueries)
82 this.#theme = createSignal(theme)
83 }
84​
85 setButtonPosition(position: DevtoolsButtonPosition) {
86 this.#buttonPosition[1](position)
87 }
88​
89 setPosition(position: DevtoolsPosition) {
90 this.#position[1](position)
91 }
92​
93 setInitialIsOpen(isOpen: boolean) {
94 this.#initialIsOpen[1](isOpen)
95 }
96​
97 setErrorTypes(errorTypes: Array<DevtoolsErrorType>) {
98 this.#errorTypes[1](errorTypes)
99 }
100​
101 setClient(client: QueryClient) {
102 this.#client[1](client)
103 }
104​
105 setTheme(theme?: Theme) {
106 this.#theme[1](theme)
107 }
108​
109 mount<T extends HTMLElement>(el: T) {
110 if (this.#isMounted) {
111 throw new Error('Devtools is already mounted')
112 }
113 // Solid creates each template once, so set the policy before rendering.
114 // It is shared by every devtools instance, so never reset it.
115 if (this.#trustedTypesPolicy) {
116 setTrustedTypesPolicy(this.#trustedTypesPolicy)
117 }
118 const dispose = render(() => {
119 const [btnPosition] = this.#buttonPosition
120 const [pos] = this.#position
121 const [isOpen] = this.#initialIsOpen
122 const [errors] = this.#errorTypes
123 const [hideDisabledQueries] = this.#hideDisabledQueries
124 const [queryClient] = this.#client
125 const [theme] = this.#theme
126 let Devtools: DevtoolsComponentType
127​
128 if (this.#Component) {
129 Devtools = this.#Component
130 } else {
131 Devtools = lazy(() => import('./DevtoolsComponent'))
132 this.#Component = Devtools
133 }
134​
135 return (
136 <Devtools
137 queryFlavor={this.#queryFlavor}
138 version={this.#version}
139 onlineManager={this.#onlineManager}
140 shadowDOMTarget={this.#shadowDOMTarget}
141 {...{
142 get client() {
143 return queryClient()
144 },
145 get buttonPosition() {
146 return btnPosition()
147 },
148 get position() {
149 return pos()
150 },
151 get initialIsOpen() {
152 return isOpen()
153 },
154 get errorTypes() {
155 return errors()
156 },
157 get hideDisabledQueries() {
158 return hideDisabledQueries()
159 },
160 get theme() {
161 return theme()
162 },
163 }}
164 />
165 )
166 }, el)
167 this.#isMounted = true
168 this.#dispose = dispose
169 }
170​
171 unmount() {
172 if (!this.#isMounted) {
173 throw new Error('Devtools is not mounted')
174 }
175 this.#dispose?.()
176 this.#isMounted = false
177 }
179​
180export { TanstackQueryDevtools }