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

ReactQueryDevtoolsPanel.tsx

122 lines3.2 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, 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
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 () =>
72 new TanstackQueryDevtoolsPanel({
73 client: queryClient,
74 queryFlavor: 'React Query',
75 version: '5',
76 onlineManager,
77 buttonPosition: 'bottom-left',
78 position: 'bottom',
79 initialIsOpen: true,
80 errorTypes,
81 styleNonce,
82 shadowDOMTarget,
83 onClose: props.onClose,
84 hideDisabledQueries,
85 theme,
86 }),
87 )
88​
89 React.useEffect(() => {
90 devtools.setClient(queryClient)
91 }, [queryClient, devtools])
92​
93 React.useEffect(() => {
94 devtools.setOnClose(props.onClose ?? (() => {}))
95 }, [props.onClose, devtools])
96​
97 React.useEffect(() => {
98 devtools.setErrorTypes(errorTypes || [])
99 }, [errorTypes, devtools])
100​
101 React.useEffect(() => {
102 devtools.setTheme(theme)
103 }, [theme, devtools])
104​
105 React.useEffect(() => {
106 if (ref.current) {
107 devtools.mount(ref.current)
108 }
109​
110 return () => {
111 devtools.unmount()
112 }
113 }, [devtools])
114​
115 return (
116 <div
117 style={{ height: '500px', ...props.style }}
118 className="tsqd-parent-container"
119 ref={ref}
120 ></div>
121 )