Open History to see the last commit
1import { useRef, useState, useEffect } from 'preact/hooks'
2import { onlineManager, useQueryClient } from '@tanstack/preact-query'
3import { TanstackQueryDevtools } from '@tanstack/query-devtools'
4import type {
5 DevtoolsButtonPosition,
6 DevtoolsErrorType,
7 DevtoolsPosition,
8 Theme,
9} from '@tanstack/query-devtools'
10import type { QueryClient } from '@tanstack/preact-query'
11import type { VNode } from 'preact'
12β
13export interface DevtoolsOptions {
14 /**
15 * Set this true if you want the dev tools to default to being open
16 */
17 initialIsOpen?: boolean
18 /**
19 * The position of the TanStack logo to open and close the devtools panel.
20 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
21 *
22 * @defaultValue bottom-right
23 */
24 buttonPosition?: DevtoolsButtonPosition
25 /**
26 * The position of the Preact Query devtools panel.
27 * 'top' | 'bottom' | 'left' | 'right'
28 *
29 * @defaultValue bottom
30 */
31 position?: DevtoolsPosition
32 /**
33 * Use this to provide a custom QueryClient. Otherwise, the one from the
34 * nearest QueryClientProvider will be used.
35 */
36 client?: QueryClient
37 /**
38 * Use this to provide custom error type handlers for the devtools panel.
39 */
40 errorTypes?: Array<DevtoolsErrorType>
41 /**
42 * Use this to pass a nonce to the style tag that is added to the document
43 * head. This is useful if you are using a Content Security Policy (CSP)
44 * nonce to allow inline styles.
45 */
46 styleNonce?: string
47 /**
48 * Use this to render the devtools inside a Shadow DOM.
49 */
50 shadowDOMTarget?: ShadowRoot
51 /**
52 * Use this to hide disabled queries from the devtools panel.
53 */
54 hideDisabledQueries?: boolean
55 /**
56 * Use this to set the theme of the devtools panel.
57 *
58 * @defaultValue system
59 */
60 theme?: Theme
61}
62β
63export function PreactQueryDevtools(props: DevtoolsOptions): VNode | null {
64 const queryClient = useQueryClient(props.client)
65 const ref = useRef<HTMLDivElement>(null)
66 const {
67 buttonPosition,
68 position,
69 initialIsOpen,
70 errorTypes,
71 styleNonce,
72 shadowDOMTarget,
73 hideDisabledQueries,
74 theme,
75 } = props
76 const [devtools] = useState(
77 () =>
78 new TanstackQueryDevtools({
79 client: queryClient,
80 queryFlavor: 'Preact Query',
81 version: '5',
82 onlineManager,
83 buttonPosition,
84 position,
85 initialIsOpen,
86 errorTypes,
87 styleNonce,
88 shadowDOMTarget,
89 hideDisabledQueries,
90 theme,
91 }),
92 )
93β
94 useEffect(() => {
95 devtools.setClient(queryClient)
96 }, [queryClient, devtools])
97β
98 useEffect(() => {
99 if (buttonPosition) {
100 devtools.setButtonPosition(buttonPosition)
101 }
102 }, [buttonPosition, devtools])
103β
104 useEffect(() => {
105 if (position) {
106 devtools.setPosition(position)
107 }
108 }, [position, devtools])
109β
110 useEffect(() => {
111 devtools.setInitialIsOpen(initialIsOpen || false)
112 }, [initialIsOpen, devtools])
113β
114 useEffect(() => {
115 devtools.setErrorTypes(errorTypes || [])
116 }, [errorTypes, devtools])
117β
118 useEffect(() => {
119 devtools.setTheme(theme)
120 }, [theme, devtools])
121β
122 useEffect(() => {
123 if (ref.current) {
124 devtools.mount(ref.current)
125 }
126 return () => {
127 devtools.unmount()
128 }
129 }, [devtools])
130β
131 return <div dir="ltr" className="tsqd-parent-container" ref={ref} />
132}