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

ReactQueryDevtoolsPanel.tsx

91 lines2.4 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
39​
40export function ReactQueryDevtoolsPanel(
41 props: DevtoolsPanelOptions,
42): React.ReactElement | null {
43 const queryClient = useQueryClient(props.client)
44 const ref = React.useRef<HTMLDivElement>(null)
45 const { errorTypes, styleNonce, shadowDOMTarget } = props
46 const [devtools] = React.useState(
47 new TanstackQueryDevtoolsPanel({
48 client: queryClient,
49 queryFlavor: 'React Query',
50 version: '5',
51 onlineManager,
52 buttonPosition: 'bottom-left',
53 position: 'bottom',
54 initialIsOpen: true,
55 errorTypes,
56 styleNonce,
57 shadowDOMTarget,
58 onClose: props.onClose,
59 }),
60 )
61​
62 React.useEffect(() => {
63 devtools.setClient(queryClient)
64 }, [queryClient, devtools])
65​
66 React.useEffect(() => {
67 devtools.setOnClose(props.onClose ?? (() => {}))
68 }, [props.onClose, devtools])
69​
70 React.useEffect(() => {
71 devtools.setErrorTypes(errorTypes || [])
72 }, [errorTypes, devtools])
73​
74 React.useEffect(() => {
75 if (ref.current) {
76 devtools.mount(ref.current)
77 }
78​
79 return () => {
80 devtools.unmount()
81 }
82 }, [devtools])
83​
84 return (
85 <div
86 style={{ height: '500px', ...props.style }}
87 className="tsqd-parent-container"
88 ref={ref}
89 ></div>
90 )