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

devtools.tsx

150 lines4.1 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 * @defaultValue bottom-right
21 */
22 buttonPosition?: DevtoolsButtonPosition
23 /**
24 * The position of the Solid Query devtools panel.
25 * 'top' | 'bottom' | 'left' | 'right'
26 * @defaultValue 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 to pass a shadow DOM target to the devtools so that the styles will be applied
43 * within the shadow DOM instead of within the head tag in the light DOM.
44 */
45 shadowDOMTarget?: ShadowRoot
46 /**
47 * Set this to true to hide disabled queries from the devtools panel.
48 */
49 hideDisabledQueries?: boolean
50 /**
51 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
52 * This overrides the theme selected in the devtools Settings menu. When omitted, the
53 * selected theme is used, or `'system'` if none is selected.
54 */
55 theme?: Theme
57​
58/**
59 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
60 * nearest `QueryClient`.
61 * @param props - The devtools options.
62 * @returns The element the devtools are mounted in.
63 */
64export default function SolidQueryDevtools(props: DevtoolsOptions) {
65 const queryClient = useQueryClient(props.client)
66 const client = createMemo(() => queryClient)
67 let ref!: HTMLDivElement
68 const devtools = new TanstackQueryDevtools({
69 client: client(),
70 queryFlavor: 'Solid Query',
71 version: '5',
72 onlineManager,
73 buttonPosition: props.buttonPosition,
74 position: props.position,
75 initialIsOpen: props.initialIsOpen,
76 errorTypes: props.errorTypes,
77 styleNonce: props.styleNonce,
78 shadowDOMTarget: props.shadowDOMTarget,
79 hideDisabledQueries: props.hideDisabledQueries,
80 theme: props.theme,
81 })
82​
83 createEffect(
84 () => client(),
85 (c) => {
86 devtools.setClient(c)
87 },
88 )
89​
90 createEffect(
91 () => props.buttonPosition,
92 (buttonPos) => {
93 if (buttonPos) {
94 devtools.setButtonPosition(buttonPos)
95 }
96 },
97 )
98​
99 createEffect(
100 () => props.position,
101 (pos) => {
102 if (pos) {
103 devtools.setPosition(pos)
104 }
105 },
106 )
107​
108 createEffect(
109 () => props.initialIsOpen,
110 (initialIsOpen) => {
111 devtools.setInitialIsOpen(initialIsOpen || false)
112 },
113 )
114​
115 createEffect(
116 () => props.errorTypes,
117 (errorTypes) => {
118 devtools.setErrorTypes(errorTypes || [])
119 },
120 )
121​
122 createEffect(
123 () => props.theme,
124 (theme) => {
125 devtools.setTheme(theme)
126 },
127 )
128​
129 // devtools.mount() calls render() from solid-js/web, which calls flush().
130 // flush() is not reentrant inside onSettled, so defer the mount to a
131 // microtask. runWithOwner(null) escapes the owned scope because
132 // devtools.mount renders a nested Solid app (creating reactive primitives).
133 let mounted = false
134 let disposed = false
135 onSettled(() => {
136 queueMicrotask(() => {
137 if (disposed) return
138 runWithOwner(null, () => devtools.mount(ref))
139 mounted = true
140 })
141 return () => {
142 disposed = true
143 if (mounted) {
144 devtools.unmount()
145 }
146 }
147 })
148​
149 return <div class="tsqd-parent-container" ref={ref} />