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