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