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