Open History to see the last commit
1import { useRef, useState, useEffect } from 'preact/hooks'
2import { onlineManager, useQueryClient } from '@tanstack/preact-query'
3import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
4import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/preact-query'
6import type { JSX, VNode } from 'preact'
7β
8export interface DevtoolsPanelOptions {
9 /**
10 * Use this to provide a custom QueryClient. Otherwise, the one from the
11 * nearest QueryClientProvider will be used.
12 */
13 client?: QueryClient
14 /**
15 * Custom error types to be shown in the devtools.
16 */
17 errorTypes?: Array<DevtoolsErrorType>
18 /**
19 * Use this to pass a nonce to the style tag that is added to the document
20 * head. This is useful if you are using a Content Security Policy (CSP)
21 * nonce to allow inline styles.
22 */
23 styleNonce?: string
24 /**
25 * Use this to render the devtools inside a Shadow DOM.
26 */
27 shadowDOMTarget?: ShadowRoot
28 /**
29 * Custom styles for the devtools panel container.
30 */
31 style?: JSX.CSSProperties
32 /**
33 * Callback function when the devtools panel is closed.
34 */
35 onClose?: () => unknown
36 /**
37 * Use this to hide disabled queries from the devtools panel.
38 */
39 hideDisabledQueries?: boolean
40 /**
41 * Use this to set the theme of the devtools panel.
42 * Defaults to 'system'.
43 */
44 theme?: Theme
45}
46β
47export function PreactQueryDevtoolsPanel(
48 props: DevtoolsPanelOptions,
49): VNode | null {
50 const queryClient = useQueryClient(props.client)
51 const ref = useRef<HTMLDivElement>(null)
52 const {
53 errorTypes,
54 styleNonce,
55 shadowDOMTarget,
56 hideDisabledQueries,
57 theme,
58 } = props
59 const [devtools] = useState(
60 () =>
61 new TanstackQueryDevtoolsPanel({
62 client: queryClient,
63 queryFlavor: 'Preact Query',
64 version: '5',
65 onlineManager,
66 buttonPosition: 'bottom-left',
67 position: 'bottom',
68 initialIsOpen: true,
69 errorTypes,
70 styleNonce,
71 shadowDOMTarget,
72 onClose: props.onClose,
73 hideDisabledQueries,
74 theme,
75 }),
76 )
77β
78 useEffect(() => {
79 devtools.setClient(queryClient)
80 }, [queryClient, devtools])
81β
82 useEffect(() => {
83 devtools.setOnClose(props.onClose ?? (() => {}))
84 }, [props.onClose, devtools])
85β
86 useEffect(() => {
87 devtools.setErrorTypes(errorTypes || [])
88 }, [errorTypes, devtools])
89β
90 useEffect(() => {
91 devtools.setTheme(theme)
92 }, [theme, devtools])
93β
94 useEffect(() => {
95 if (ref.current) {
96 devtools.mount(ref.current)
97 }
98 return () => {
99 devtools.unmount()
100 }
101 }, [devtools])
102β
103 return (
104 <div
105 style={{ height: '500px', ...props.style }}
106 className="tsqd-parent-container"
107 ref={ref}
108 />
109 )
110}