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

TanstackQueryDevtools.tsx

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