Open History to see the last commit
1import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
2import { onlineManager, useQueryClient } from '@tanstack/solid-query'
3import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
4import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/solid-query'
6import type { JSX } from 'solid-js'
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 * @deprecated The devtools no longer add a `<style>` element, so they need no
22 * nonce. This option does nothing.
23 */
24 styleNonce?: string
25 /**
26 * The shadow root to add the devtools styles to. By default, the styles are
27 * added to the shadow root the devtools render in, or to the document when
28 * they do not render in a shadow root.
29 */
30 shadowDOMTarget?: ShadowRoot
31 /**
32 * The Trusted Types policy the devtools use to create their HTML templates.
33 * Pass one when your page enforces `require-trusted-types-for 'script'`.
34 * The devtools only pass it their own markup.
35 */
36 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
37β
38 /**
39 * Custom styles for the devtools panel
40 * @default { height: '100%', display: 'flex', 'flex-direction': 'column' }
41 * @example { height: '100%' }
42 * @example { height: '100%', width: '100%' }
43 */
44 style?: JSX.CSSProperties
45β
46 /**
47 * Callback function that is called when the devtools panel is closed
48 */
49 onClose?: () => void
50 /**
51 * Set this to true to hide disabled queries from the devtools panel.
52 */
53 hideDisabledQueries?: boolean
54 /**
55 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
56 * This overrides the theme selected in the devtools Settings menu. When omitted, the
57 * selected theme is used, or `'system'` if none is selected.
58 */
59 theme?: Theme
60}
61β
62/**
63 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
64 * @param props - The devtools panel options.
65 * @returns The element the devtools panel is mounted in.
66 */
67export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
68 const queryClient = useQueryClient(props.client)
69 const client = createMemo(() => queryClient)
70 let ref!: HTMLDivElement
71 const {
72 errorTypes,
73 shadowDOMTarget,
74 trustedTypesPolicy,
75 hideDisabledQueries,
76 } = props
77 const devtools = new TanstackQueryDevtoolsPanel({
78 client: client(),
79 queryFlavor: 'Solid Query',
80 version: '5',
81 onlineManager,
82 buttonPosition: 'bottom-left',
83 position: 'bottom',
84 initialIsOpen: true,
85 errorTypes,
86 shadowDOMTarget,
87 trustedTypesPolicy,
88 onClose: props.onClose,
89 hideDisabledQueries,
90 theme: props.theme,
91 })
92 createEffect(() => {
93 devtools.setClient(client())
94 })
95 createEffect(() => {
96 devtools.setOnClose(props.onClose ?? (() => {}))
97 })
98β
99 createEffect(() => {
100 devtools.setErrorTypes(props.errorTypes || [])
101 })
102β
103 createEffect(() => {
104 devtools.setTheme(props.theme)
105 })
106β
107 onMount(() => {
108 devtools.mount(ref)
109 onCleanup(() => devtools.unmount())
110 })
111β
112 return (
113 <div
114 style={{
115 height: '100%',
116 display: 'flex',
117 'flex-direction': 'column',
118 ...props.style,
119 }}
120 class="tsqd-parent-container"
121 ref={ref}
122 />
123 )
124}