Open History to see the last commit
1'use client'
2import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
3import { onlineManager, useQueryClient } from '@tanstack/react-query'
4import * as React from 'react'
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 * @deprecated The devtools no longer add a `<style>` element, so they need no
22 * nonce. This option does nothing.
23 */
24 styleNonce?: string
25 /**
26 * The shadow root to add the devtools styles to. By default, the styles are
27 * added to the shadow root the devtools render in, or to the document when
28 * they do not render in a shadow root.
29 */
30 shadowDOMTarget?: ShadowRoot
31 /**
32 * The Trusted Types policy the devtools use to create their HTML templates.
33 * Pass one when your page enforces `require-trusted-types-for 'script'`.
34 * The devtools only pass it their own markup.
35 */
36 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
37β
38 /**
39 * Custom styles for the devtools panel
40 * @default { height: '100%', display: 'flex', flexDirection: 'column' }
41 * @example { height: '100%' }
42 * @example { height: '100%', width: '100%' }
43 */
44 style?: React.CSSProperties
45β
46 /**
47 * Callback function that is called when the devtools panel is closed
48 */
49 onClose?: () => void
50 /**
51 * Set this to true to hide disabled queries from the devtools panel.
52 */
53 hideDisabledQueries?: boolean
54 /**
55 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
56 * This overrides the theme selected in the devtools Settings menu. When omitted, the
57 * selected theme is used, or `'system'` if none is selected.
58 */
59 theme?: Theme
60}
61β
62/**
63 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
64 * @param props - The devtools panel options.
65 * @returns The element the devtools panel is mounted in.
66 */
67export function ReactQueryDevtoolsPanel(
68 props: DevtoolsPanelOptions,
69): React.ReactElement | null {
70 const queryClient = useQueryClient(props.client)
71 const ref = React.useRef<HTMLDivElement>(null)
72 const {
73 errorTypes,
74 shadowDOMTarget,
75 trustedTypesPolicy,
76 hideDisabledQueries,
77 theme,
78 } = props
79 const [devtools] = React.useState(
80 () =>
81 new TanstackQueryDevtoolsPanel({
82 client: queryClient,
83 queryFlavor: 'React Query',
84 version: '5',
85 onlineManager,
86 buttonPosition: 'bottom-left',
87 position: 'bottom',
88 initialIsOpen: true,
89 errorTypes,
90 shadowDOMTarget,
91 trustedTypesPolicy,
92 onClose: props.onClose,
93 hideDisabledQueries,
94 theme,
95 }),
96 )
97β
98 React.useEffect(() => {
99 devtools.setClient(queryClient)
100 }, [queryClient, devtools])
101β
102 React.useEffect(() => {
103 devtools.setOnClose(props.onClose ?? (() => {}))
104 }, [props.onClose, devtools])
105β
106 React.useEffect(() => {
107 devtools.setErrorTypes(errorTypes || [])
108 }, [errorTypes, devtools])
109β
110 React.useEffect(() => {
111 devtools.setTheme(theme)
112 }, [theme, devtools])
113β
114 React.useEffect(() => {
115 if (ref.current) {
116 devtools.mount(ref.current)
117 }
118β
119 return () => {
120 devtools.unmount()
121 }
122 }, [devtools])
123β
124 return (
125 <div
126 style={{
127 height: '100%',
128 display: 'flex',
129 flexDirection: 'column',
130 ...props.style,
131 }}
132 className="tsqd-parent-container"
133 ref={ref}
134 ></div>
135 )
136}