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

PreactQueryDevtoolsPanel.tsx

111 lines2.7 KB

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 { CSSProperties, 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?: 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 *
43 * @defaultValue system
44 */
45 theme?: Theme
47​
48export function PreactQueryDevtoolsPanel(
49 props: DevtoolsPanelOptions,
50): VNode | null {
51 const queryClient = useQueryClient(props.client)
52 const ref = useRef<HTMLDivElement>(null)
53 const {
54 errorTypes,
55 styleNonce,
56 shadowDOMTarget,
57 hideDisabledQueries,
58 theme,
59 } = props
60 const [devtools] = useState(
61 () =>
62 new TanstackQueryDevtoolsPanel({
63 client: queryClient,
64 queryFlavor: 'Preact Query',
65 version: '5',
66 onlineManager,
67 buttonPosition: 'bottom-left',
68 position: 'bottom',
69 initialIsOpen: true,
70 errorTypes,
71 styleNonce,
72 shadowDOMTarget,
73 onClose: props.onClose,
74 hideDisabledQueries,
75 theme,
76 }),
77 )
78​
79 useEffect(() => {
80 devtools.setClient(queryClient)
81 }, [queryClient, devtools])
82​
83 useEffect(() => {
84 devtools.setOnClose(props.onClose ?? (() => {}))
85 }, [props.onClose, devtools])
86​
87 useEffect(() => {
88 devtools.setErrorTypes(errorTypes || [])
89 }, [errorTypes, devtools])
90​
91 useEffect(() => {
92 devtools.setTheme(theme)
93 }, [theme, devtools])
94​
95 useEffect(() => {
96 if (ref.current) {
97 devtools.mount(ref.current)
98 }
99 return () => {
100 devtools.unmount()
101 }
102 }, [devtools])
103​
104 return (
105 <div
106 style={{ height: '500px', ...props.style }}
107 className="tsqd-parent-container"
108 ref={ref}
109 />
110 )