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

ReactQueryDevtoolsPanel.tsx

96 lines2.5 KB

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