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

devtools.tsx

142 lines3.7 KB

1import { createEffect, createMemo, onSettled, runWithOwner } from 'solid-js'
2import { onlineManager, useQueryClient } from '@tanstack/solid-query'
3import { TanstackQueryDevtools } from '@tanstack/query-devtools'
4import type {
5 DevtoolsButtonPosition,
6 DevtoolsErrorType,
7 DevtoolsPosition,
8 Theme,
9} from '@tanstack/query-devtools'
10import type { QueryClient } from '@tanstack/solid-query'
11​
12interface DevtoolsOptions {
13 /**
14 * Set this true if you want the dev tools to default to being open
15 */
16 initialIsOpen?: boolean
17 /**
18 * The position of the TanStack logo to open and close the devtools panel.
19 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
20 * Defaults to 'bottom-right'.
21 */
22 buttonPosition?: DevtoolsButtonPosition
23 /**
24 * The position of the Solid Query devtools panel.
25 * 'top' | 'bottom' | 'left' | 'right'
26 * Defaults to 'bottom'.
27 */
28 position?: DevtoolsPosition
29 /**
30 * Custom instance of QueryClient
31 */
32 client?: QueryClient
33 /**
34 * Use this so you can define custom errors that can be shown in the devtools.
35 */
36 errorTypes?: Array<DevtoolsErrorType>
37 /**
38 * Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
39 */
40 styleNonce?: string
41 /**
42 * Use this so you can attach the devtool's styles to specific element in the DOM.
43 */
44 shadowDOMTarget?: ShadowRoot
45 /**
46 * Set this to true to hide disabled queries from the devtools panel.
47 */
48 hideDisabledQueries?: boolean
49 /**
50 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
51 * Defaults to 'system'.
52 */
53 theme?: Theme
55​
56export default function SolidQueryDevtools(props: DevtoolsOptions) {
57 const queryClient = useQueryClient(props.client)
58 const client = createMemo(() => queryClient)
59 let ref!: HTMLDivElement
60 const devtools = new TanstackQueryDevtools({
61 client: client(),
62 queryFlavor: 'Solid Query',
63 version: '5',
64 onlineManager,
65 buttonPosition: props.buttonPosition,
66 position: props.position,
67 initialIsOpen: props.initialIsOpen,
68 errorTypes: props.errorTypes,
69 styleNonce: props.styleNonce,
70 shadowDOMTarget: props.shadowDOMTarget,
71 hideDisabledQueries: props.hideDisabledQueries,
72 theme: props.theme,
73 })
74​
75 createEffect(
76 () => client(),
77 (c) => {
78 devtools.setClient(c)
79 },
80 )
81​
82 createEffect(
83 () => props.buttonPosition,
84 (buttonPos) => {
85 if (buttonPos) {
86 devtools.setButtonPosition(buttonPos)
87 }
88 },
89 )
90​
91 createEffect(
92 () => props.position,
93 (pos) => {
94 if (pos) {
95 devtools.setPosition(pos)
96 }
97 },
98 )
99​
100 createEffect(
101 () => props.initialIsOpen,
102 (initialIsOpen) => {
103 devtools.setInitialIsOpen(initialIsOpen || false)
104 },
105 )
106​
107 createEffect(
108 () => props.errorTypes,
109 (errorTypes) => {
110 devtools.setErrorTypes(errorTypes || [])
111 },
112 )
113​
114 createEffect(
115 () => props.theme,
116 (theme) => {
117 devtools.setTheme(theme || 'system')
118 },
119 )
120​
121 // devtools.mount() calls render() from solid-js/web, which calls flush().
122 // flush() is not reentrant inside onSettled, so defer the mount to a
123 // microtask. runWithOwner(null) escapes the owned scope because
124 // devtools.mount renders a nested Solid app (creating reactive primitives).
125 let mounted = false
126 let disposed = false
127 onSettled(() => {
128 queueMicrotask(() => {
129 if (disposed) return
130 runWithOwner(null, () => devtools.mount(ref))
131 mounted = true
132 })
133 return () => {
134 disposed = true
135 if (mounted) {
136 devtools.unmount()
137 }
138 }
139 })
140​
141 return <div class="tsqd-parent-container" ref={ref}></div>