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 * @defaultValue system
61 */
62 theme?: Theme
63}
64β
65/**
66 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
67 * nearest `QueryClient`.
68 * @param props - The devtools options.
69 * @returns The element the devtools are mounted in.
70 */
71export function PreactQueryDevtools(props: DevtoolsOptions): VNode | null {
72 const queryClient = useQueryClient(props.client)
73 const ref = useRef<HTMLDivElement>(null)
74 const {
75 buttonPosition,
76 position,
77 initialIsOpen,
78 errorTypes,
79 styleNonce,
80 shadowDOMTarget,
81 hideDisabledQueries,
82 theme,
83 } = props
84 const [devtools] = useState(
85 () =>
86 new TanstackQueryDevtools({
87 client: queryClient,
88 queryFlavor: 'Preact Query',
89 version: '5',
90 onlineManager,
91 buttonPosition,
92 position,
93 initialIsOpen,
94 errorTypes,
95 styleNonce,
96 shadowDOMTarget,
97 hideDisabledQueries,
98 theme,
99 }),
100 )
101β
102 useEffect(() => {
103 devtools.setClient(queryClient)
104 }, [queryClient, devtools])
105β
106 useEffect(() => {
107 if (buttonPosition) {
108 devtools.setButtonPosition(buttonPosition)
109 }
110 }, [buttonPosition, devtools])
111β
112 useEffect(() => {
113 if (position) {
114 devtools.setPosition(position)
115 }
116 }, [position, devtools])
117β
118 useEffect(() => {
119 devtools.setInitialIsOpen(initialIsOpen || false)
120 }, [initialIsOpen, devtools])
121β
122 useEffect(() => {
123 devtools.setErrorTypes(errorTypes || [])
124 }, [errorTypes, devtools])
125β
126 useEffect(() => {
127 devtools.setTheme(theme)
128 }, [theme, devtools])
129β
130 useEffect(() => {
131 if (ref.current) {
132 devtools.mount(ref.current)
133 }
134 return () => {
135 devtools.unmount()
136 }
137 }, [devtools])
138β
139 return <div dir="ltr" className="tsqd-parent-container" ref={ref} />
140}