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β
8/**
9 * The props of `ReactQueryDevtoolsPanel`, which renders the devtools panel inline.
10 */
11export interface DevtoolsPanelOptions {
12 /**
13 * Custom instance of QueryClient
14 */
15 client?: QueryClient
16 /**
17 * Use this so you can define custom errors that can be shown in the devtools.
18 */
19 errorTypes?: Array<DevtoolsErrorType>
20 /**
21 * 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.
22 */
23 styleNonce?: string
24 /**
25 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
26 * within the shadow DOM instead of within the head tag in the light DOM.
27 */
28 shadowDOMTarget?: ShadowRoot
29β
30 /**
31 * Custom styles for the devtools panel
32 * @default { height: '500px' }
33 * @example { height: '100%' }
34 * @example { height: '100%', width: '100%' }
35 */
36 style?: React.CSSProperties
37β
38 /**
39 * Callback function that is called when the devtools panel is closed
40 */
41 onClose?: () => void
42 /**
43 * Set this to true to hide disabled queries from the devtools panel.
44 */
45 hideDisabledQueries?: boolean
46 /**
47 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
48 * @defaultValue system
49 */
50 theme?: Theme
51}
52β
53/**
54 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
55 * @param props - The devtools panel options.
56 * @returns The element the devtools panel is mounted in.
57 */
58export function ReactQueryDevtoolsPanel(
59 props: DevtoolsPanelOptions,
60): React.ReactElement | null {
61 const queryClient = useQueryClient(props.client)
62 const ref = React.useRef<HTMLDivElement>(null)
63 const {
64 errorTypes,
65 styleNonce,
66 shadowDOMTarget,
67 hideDisabledQueries,
68 theme,
69 } = props
70 const [devtools] = React.useState(
71 new TanstackQueryDevtoolsPanel({
72 client: queryClient,
73 queryFlavor: 'React Query',
74 version: '5',
75 onlineManager,
76 buttonPosition: 'bottom-left',
77 position: 'bottom',
78 initialIsOpen: true,
79 errorTypes,
80 styleNonce,
81 shadowDOMTarget,
82 onClose: props.onClose,
83 hideDisabledQueries,
84 theme,
85 }),
86 )
87β
88 React.useEffect(() => {
89 devtools.setClient(queryClient)
90 }, [queryClient, devtools])
91β
92 React.useEffect(() => {
93 devtools.setOnClose(props.onClose ?? (() => {}))
94 }, [props.onClose, devtools])
95β
96 React.useEffect(() => {
97 devtools.setErrorTypes(errorTypes || [])
98 }, [errorTypes, devtools])
99β
100 React.useEffect(() => {
101 devtools.setTheme(theme)
102 }, [theme, devtools])
103β
104 React.useEffect(() => {
105 if (ref.current) {
106 devtools.mount(ref.current)
107 }
108β
109 return () => {
110 devtools.unmount()
111 }
112 }, [devtools])
113β
114 return (
115 <div
116 style={{ height: '500px', ...props.style }}
117 className="tsqd-parent-container"
118 ref={ref}
119 ></div>
120 )
121}