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

PreactQueryDevtoolsPanel.tsx

120 lines3.3 KB

1import { useEffect, useRef, useState } from 'preact/hooks'
2import { onlineManager, useQueryClient } from '@tanstack/preact-query'
3import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
4import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
5import type { QueryClient } from '@tanstack/preact-query'
6import type { CSSProperties, VNode } from 'preact'
7​
8/**
9 * The props of `PreactQueryDevtoolsPanel`, which renders the devtools panel inline.
10 */
11export interface DevtoolsPanelOptions {
12 /**
13 * Use this to provide a custom QueryClient. Otherwise, the one from the
14 * nearest QueryClientProvider will be used.
15 */
16 client?: QueryClient
17 /**
18 * Custom error types to be shown in the devtools.
19 */
20 errorTypes?: Array<DevtoolsErrorType>
21 /**
22 * Use this to pass a nonce to the style tag that is added to the document
23 * head. This is useful if you are using a Content Security Policy (CSP)
24 * nonce to allow inline styles.
25 */
26 styleNonce?: string
27 /**
28 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
29 * within the shadow DOM instead of within the head tag in the light DOM.
30 */
31 shadowDOMTarget?: ShadowRoot
32 /**
33 * Custom styles for the devtools panel container.
34 */
35 style?: CSSProperties
36 /**
37 * Callback function when the devtools panel is closed.
38 */
39 onClose?: () => unknown
40 /**
41 * Use this to hide disabled queries from the devtools panel.
42 */
43 hideDisabledQueries?: boolean
44 /**
45 * Use this to set the theme of the devtools panel.
46 * This overrides the theme selected in the devtools Settings menu. When omitted, the
47 * selected theme is used, or `'system'` if none is selected.
48 */
49 theme?: Theme
51​
52/**
53 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
54 * @param props - The devtools panel options.
55 * @returns The element the devtools panel is mounted in.
56 */
57export function PreactQueryDevtoolsPanel(
58 props: DevtoolsPanelOptions,
59): VNode | null {
60 const queryClient = useQueryClient(props.client)
61 const ref = useRef<HTMLDivElement>(null)
62 const {
63 errorTypes,
64 styleNonce,
65 shadowDOMTarget,
66 hideDisabledQueries,
67 theme,
68 } = props
69 const [devtools] = useState(
70 () =>
71 new TanstackQueryDevtoolsPanel({
72 client: queryClient,
73 queryFlavor: 'Preact 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 useEffect(() => {
89 devtools.setClient(queryClient)
90 }, [queryClient, devtools])
91​
92 useEffect(() => {
93 devtools.setOnClose(props.onClose ?? (() => {}))
94 }, [props.onClose, devtools])
95​
96 useEffect(() => {
97 devtools.setErrorTypes(errorTypes || [])
98 }, [errorTypes, devtools])
99​
100 useEffect(() => {
101 devtools.setTheme(theme)
102 }, [theme, devtools])
103​
104 useEffect(() => {
105 if (ref.current) {
106 devtools.mount(ref.current)
107 }
108 return () => {
109 devtools.unmount()
110 }
111 }, [devtools])
112​
113 return (
114 <div
115 style={{ height: '500px', ...props.style }}
116 className="tsqd-parent-container"
117 ref={ref}
118 />
119 )