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

devtoolsPanel.tsx

105 lines3.0 KB

1import { createEffect, createMemo, onCleanup, onMount } 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 '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 * 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: '500px' }
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 * @defaultValue system
49 */
50 theme?: Theme
52​
53/**
54 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
55 * @param props - The devtools panel options.
56 * @returns The element the devtools panel is mounted in.
57 */
58export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
59 const queryClient = useQueryClient(props.client)
60 const client = createMemo(() => queryClient)
61 let ref!: HTMLDivElement
62 const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props
63 const devtools = new TanstackQueryDevtoolsPanel({
64 client: client(),
65 queryFlavor: 'Solid Query',
66 version: '5',
67 onlineManager,
68 buttonPosition: 'bottom-left',
69 position: 'bottom',
70 initialIsOpen: true,
71 errorTypes,
72 styleNonce,
73 shadowDOMTarget,
74 onClose: props.onClose,
75 hideDisabledQueries,
76 theme: props.theme,
77 })
78 createEffect(() => {
79 devtools.setClient(client())
80 })
81 createEffect(() => {
82 devtools.setOnClose(props.onClose ?? (() => {}))
83 })
84​
85 createEffect(() => {
86 devtools.setErrorTypes(props.errorTypes || [])
87 })
88​
89 createEffect(() => {
90 devtools.setTheme(props.theme || 'system')
91 })
92​
93 onMount(() => {
94 devtools.mount(ref)
95 onCleanup(() => devtools.unmount())
96 })
97​
98 return (
99 <div
100 style={{ height: '500px', ...props.style }}
101 class="tsqd-parent-container"
102 ref={ref}
103 />
104 )