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

PreactQueryDevtoolsPanel.tsx

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