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

ReactQueryDevtoolsPanel.tsx

123 lines3.3 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 * This overrides the theme selected in the devtools Settings menu. When omitted, the
49 * selected theme is used, or `'system'` if none is selected.
50 */
51 theme?: Theme
53​
54/**
55 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
56 * @param props - The devtools panel options.
57 * @returns The element the devtools panel is mounted in.
58 */
59export function ReactQueryDevtoolsPanel(
60 props: DevtoolsPanelOptions,
61): React.ReactElement | null {
62 const queryClient = useQueryClient(props.client)
63 const ref = React.useRef<HTMLDivElement>(null)
64 const {
65 errorTypes,
66 styleNonce,
67 shadowDOMTarget,
68 hideDisabledQueries,
69 theme,
70 } = props
71 const [devtools] = React.useState(
72 () =>
73 new TanstackQueryDevtoolsPanel({
74 client: queryClient,
75 queryFlavor: 'React Query',
76 version: '5',
77 onlineManager,
78 buttonPosition: 'bottom-left',
79 position: 'bottom',
80 initialIsOpen: true,
81 errorTypes,
82 styleNonce,
83 shadowDOMTarget,
84 onClose: props.onClose,
85 hideDisabledQueries,
86 theme,
87 }),
88 )
89​
90 React.useEffect(() => {
91 devtools.setClient(queryClient)
92 }, [queryClient, devtools])
93​
94 React.useEffect(() => {
95 devtools.setOnClose(props.onClose ?? (() => {}))
96 }, [props.onClose, devtools])
97​
98 React.useEffect(() => {
99 devtools.setErrorTypes(errorTypes || [])
100 }, [errorTypes, devtools])
101​
102 React.useEffect(() => {
103 devtools.setTheme(theme)
104 }, [theme, devtools])
105​
106 React.useEffect(() => {
107 if (ref.current) {
108 devtools.mount(ref.current)
109 }
110​
111 return () => {
112 devtools.unmount()
113 }
114 }, [devtools])
115​
116 return (
117 <div
118 style={{ height: '500px', ...props.style }}
119 className="tsqd-parent-container"
120 ref={ref}
121 ></div>
122 )