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