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

PreactQueryDevtoolsPanel.tsx

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