Open History to see the last commit
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 * @default { height: '100%', display: 'flex', flexDirection: 'column' }
35 * @example { height: '100%' }
36 * @example { height: '100%', width: '100%' }
37 */
38 style?: CSSProperties
39 /**
40 * Callback function when the devtools panel is closed.
41 */
42 onClose?: () => unknown
43 /**
44 * Use this to hide disabled queries from the devtools panel.
45 */
46 hideDisabledQueries?: boolean
47 /**
48 * Use this to set the theme of the devtools panel.
49 * This overrides the theme selected in the devtools Settings menu. When omitted, the
50 * selected theme is used, or `'system'` if none is selected.
51 */
52 theme?: Theme
53}
54β
55/**
56 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
57 * @param props - The devtools panel options.
58 * @returns The element the devtools panel is mounted in.
59 */
60export function PreactQueryDevtoolsPanel(
61 props: DevtoolsPanelOptions,
62): VNode | null {
63 const queryClient = useQueryClient(props.client)
64 const ref = useRef<HTMLDivElement>(null)
65 const {
66 errorTypes,
67 styleNonce,
68 shadowDOMTarget,
69 hideDisabledQueries,
70 theme,
71 } = props
72 const [devtools] = useState(
73 () =>
74 new TanstackQueryDevtoolsPanel({
75 client: queryClient,
76 queryFlavor: 'Preact Query',
77 version: '5',
78 onlineManager,
79 buttonPosition: 'bottom-left',
80 position: 'bottom',
81 initialIsOpen: true,
82 errorTypes,
83 styleNonce,
84 shadowDOMTarget,
85 onClose: props.onClose,
86 hideDisabledQueries,
87 theme,
88 }),
89 )
90β
91 useEffect(() => {
92 devtools.setClient(queryClient)
93 }, [queryClient, devtools])
94β
95 useEffect(() => {
96 devtools.setOnClose(props.onClose ?? (() => {}))
97 }, [props.onClose, devtools])
98β
99 useEffect(() => {
100 devtools.setErrorTypes(errorTypes || [])
101 }, [errorTypes, devtools])
102β
103 useEffect(() => {
104 devtools.setTheme(theme)
105 }, [theme, devtools])
106β
107 useEffect(() => {
108 if (ref.current) {
109 devtools.mount(ref.current)
110 }
111 return () => {
112 devtools.unmount()
113 }
114 }, [devtools])
115β
116 return (
117 <div
118 style={{
119 height: '100%',
120 display: 'flex',
121 flexDirection: 'column',
122 ...props.style,
123 }}
124 className="tsqd-parent-container"
125 ref={ref}
126 />
127 )
128}