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

devtoolsPanel.tsx

85 lines2.4 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 } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/solid-query'
6​
7export interface DevtoolsPanelOptions {
8 /**
9 * Custom instance of QueryClient
10 */
11 client?: QueryClient
12 /**
13 * Use this so you can define custom errors that can be shown in the devtools.
14 */
15 errorTypes?: Array<DevtoolsErrorType>
16 /**
17 * 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.
18 */
19 styleNonce?: string
20 /**
21 * Use this so you can attach the devtool's styles to specific element in the DOM.
22 */
23 shadowDOMTarget?: ShadowRoot
24​
25 /**
26 * Custom styles for the devtools panel
27 * @default { height: '500px' }
28 * @example { height: '100%' }
29 * @example { height: '100%', width: '100%' }
30 */
31 style?: CSSStyleValue
32​
33 /**
34 * Callback function that is called when the devtools panel is closed
35 */
36 onClose?: () => unknown
37 /**
38 * Set this to true to hide disabled queries from the devtools panel.
39 */
40 hideDisabledQueries?: boolean
42​
43export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) {
44 const queryClient = useQueryClient()
45 const client = createMemo(() => props.client || queryClient)
46 let ref!: HTMLDivElement
47 const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props
48 const devtools = new TanstackQueryDevtoolsPanel({
49 client: client(),
50 queryFlavor: 'Solid Query',
51 version: '5',
52 onlineManager,
53 buttonPosition: 'bottom-left',
54 position: 'bottom',
55 initialIsOpen: true,
56 errorTypes,
57 styleNonce,
58 shadowDOMTarget,
59 onClose: props.onClose,
60 hideDisabledQueries,
61 })
62 createEffect(() => {
63 devtools.setClient(client())
64 })
65 createEffect(() => {
66 devtools.setOnClose(props.onClose ?? (() => {}))
67 })
68​
69 createEffect(() => {
70 devtools.setErrorTypes(props.errorTypes || [])
71 })
72​
73 onMount(() => {
74 devtools.mount(ref)
75 onCleanup(() => devtools.unmount())
76 })
77​
78 return (
79 <div
80 style={{ height: '500px', ...props.style }}
81 class="tsqd-parent-container"
82 ref={ref}
83 />
84 )