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