Open History to see the last commit
1import { createEffect, createMemo, onSettled, runWithOwner } from 'solid-js'
2import { onlineManager, useQueryClient } from '@tanstack/solid-query'
3import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
4import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/solid-query'
6import type { JSX } from '@solidjs/web'
7β
8export interface DevtoolsPanelOptions {
9 /**
10 * Custom instance of QueryClient
11 */
12 client?: QueryClient
13 /**
14 * Use this so you can define custom errors that can be shown in the devtools.
15 */
16 errorTypes?: Array<DevtoolsErrorType>
17 /**
18 * 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.
19 */
20 styleNonce?: string
21 /**
22 * Use this so you can attach the devtool's styles to specific element in the DOM.
23 */
24 shadowDOMTarget?: ShadowRoot
25β
26 /**
27 * Custom styles for the devtools panel
28 * @default { height: '500px' }
29 * @example { height: '100%' }
30 * @example { height: '100%', width: '100%' }
31 */
32 style?: JSX.CSSProperties
33β
34 /**
35 * Callback function that is called when the devtools panel is closed
36 */
37 onClose?: () => void
38 /**
39 * Set this to true to hide disabled queries from the devtools panel.
40 */
41 hideDisabledQueries?: boolean
42 /**
43 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
44 * Defaults to 'system'.
45 */
46 theme?: Theme
47}
48β
49export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
50 const queryClient = useQueryClient(props.client)
51 const client = createMemo(() => queryClient)
52 let ref!: HTMLDivElement
53 const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props
54 const devtools = new TanstackQueryDevtoolsPanel({
55 client: client(),
56 queryFlavor: 'Solid Query',
57 version: '5',
58 onlineManager,
59 buttonPosition: 'bottom-left',
60 position: 'bottom',
61 initialIsOpen: true,
62 errorTypes,
63 styleNonce,
64 shadowDOMTarget,
65 onClose: props.onClose,
66 hideDisabledQueries,
67 theme: props.theme,
68 })
69 createEffect(
70 () => client(),
71 (c) => {
72 devtools.setClient(c)
73 },
74 )
75 createEffect(
76 () => props.onClose,
77 (onClose) => {
78 devtools.setOnClose(onClose ?? (() => {}))
79 },
80 )
81β
82 createEffect(
83 () => props.errorTypes,
84 (errorTypes2) => {
85 devtools.setErrorTypes(errorTypes2 || [])
86 },
87 )
88β
89 createEffect(
90 () => props.theme,
91 (theme) => {
92 devtools.setTheme(theme || 'system')
93 },
94 )
95β
96 // devtools.mount() calls render() from solid-js/web, which calls flush().
97 // flush() is not reentrant inside onSettled, so defer the mount to a
98 // microtask. runWithOwner(null) escapes the owned scope because
99 // devtools.mount renders a nested Solid app (creating reactive primitives).
100 let mounted = false
101 let disposed = false
102 onSettled(() => {
103 queueMicrotask(() => {
104 if (disposed) return
105 runWithOwner(null, () => devtools.mount(ref))
106 mounted = true
107 })
108 return () => {
109 disposed = true
110 if (mounted) {
111 devtools.unmount()
112 }
113 }
114 })
115β
116 return (
117 <div
118 style={{ height: '500px', ...props.style }}
119 class="tsqd-parent-container"
120 ref={ref}
121 />
122 )
123}