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