Open History to see the last commit
1'use client'
2import * as React from 'react'
3import { onlineManager, useQueryClient } from '@tanstack/react-query'
4import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
5import type { DevtoolsErrorType, Theme } 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
42 /**
43 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
44 * Defaults to 'system'.
45 */
46 theme?: Theme
47}
48β
49export function ReactQueryDevtoolsPanel(
50 props: DevtoolsPanelOptions,
51): React.ReactElement | null {
52 const queryClient = useQueryClient(props.client)
53 const ref = React.useRef<HTMLDivElement>(null)
54 const {
55 errorTypes,
56 styleNonce,
57 shadowDOMTarget,
58 hideDisabledQueries,
59 theme,
60 } = props
61 const [devtools] = React.useState(
62 new TanstackQueryDevtoolsPanel({
63 client: queryClient,
64 queryFlavor: 'React 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 React.useEffect(() => {
80 devtools.setClient(queryClient)
81 }, [queryClient, devtools])
82β
83 React.useEffect(() => {
84 devtools.setOnClose(props.onClose ?? (() => {}))
85 }, [props.onClose, devtools])
86β
87 React.useEffect(() => {
88 devtools.setErrorTypes(errorTypes || [])
89 }, [errorTypes, devtools])
90β
91 React.useEffect(() => {
92 devtools.setTheme(theme)
93 }, [theme, devtools])
94β
95 React.useEffect(() => {
96 if (ref.current) {
97 devtools.mount(ref.current)
98 }
99β
100 return () => {
101 devtools.unmount()
102 }
103 }, [devtools])
104β
105 return (
106 <div
107 style={{ height: '500px', ...props.style }}
108 className="tsqd-parent-container"
109 ref={ref}
110 ></div>
111 )
112}