Open History to see the last commit
1import { onlineManager, useQueryClient } from '@tanstack/preact-query'
2import { TanstackQueryDevtools } from '@tanstack/query-devtools'
3import { useEffect, useRef, useState } from 'preact/hooks'
4import type { QueryClient } from '@tanstack/preact-query'
5import type {
6 DevtoolsButtonPosition,
7 DevtoolsErrorType,
8 DevtoolsPosition,
9 Theme,
10} from '@tanstack/query-devtools'
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 * @deprecated The devtools no longer add a `<style>` element, so they need no
45 * nonce. This option does nothing.
46 */
47 styleNonce?: string
48 /**
49 * The shadow root to add the devtools styles to. By default, the styles are
50 * added to the shadow root the devtools render in, or to the document when
51 * they do not render in a shadow root.
52 */
53 shadowDOMTarget?: ShadowRoot
54 /**
55 * The Trusted Types policy the devtools use to create their HTML templates.
56 * Pass one when your page enforces `require-trusted-types-for 'script'`.
57 * The devtools only pass it their own markup.
58 */
59 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
60 /**
61 * Use this to hide disabled queries from the devtools panel.
62 */
63 hideDisabledQueries?: boolean
64 /**
65 * Use this to set the theme of the devtools panel.
66 * This overrides the theme selected in the devtools Settings menu. When omitted, the
67 * selected theme is used, or `'system'` if none is selected.
68 */
69 theme?: Theme
70}
71β
72/**
73 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
74 * nearest `QueryClient`.
75 * @param props - The devtools options.
76 * @returns The element the devtools are mounted in.
77 */
78export function PreactQueryDevtools(props: DevtoolsOptions): VNode | null {
79 const queryClient = useQueryClient(props.client)
80 const ref = useRef<HTMLDivElement>(null)
81 const {
82 buttonPosition,
83 position,
84 initialIsOpen,
85 errorTypes,
86 shadowDOMTarget,
87 trustedTypesPolicy,
88 hideDisabledQueries,
89 theme,
90 } = props
91 const [devtools] = useState(
92 () =>
93 new TanstackQueryDevtools({
94 client: queryClient,
95 queryFlavor: 'Preact Query',
96 version: '5',
97 onlineManager,
98 buttonPosition,
99 position,
100 initialIsOpen,
101 errorTypes,
102 shadowDOMTarget,
103 trustedTypesPolicy,
104 hideDisabledQueries,
105 theme,
106 }),
107 )
108β
109 useEffect(() => {
110 devtools.setClient(queryClient)
111 }, [queryClient, devtools])
112β
113 useEffect(() => {
114 if (buttonPosition) {
115 devtools.setButtonPosition(buttonPosition)
116 }
117 }, [buttonPosition, devtools])
118β
119 useEffect(() => {
120 if (position) {
121 devtools.setPosition(position)
122 }
123 }, [position, devtools])
124β
125 useEffect(() => {
126 devtools.setInitialIsOpen(initialIsOpen || false)
127 }, [initialIsOpen, devtools])
128β
129 useEffect(() => {
130 devtools.setErrorTypes(errorTypes || [])
131 }, [errorTypes, devtools])
132β
133 useEffect(() => {
134 devtools.setTheme(theme)
135 }, [theme, devtools])
136β
137 useEffect(() => {
138 if (ref.current) {
139 devtools.mount(ref.current)
140 }
141 return () => {
142 devtools.unmount()
143 }
144 }, [devtools])
145β
146 return <div className="tsqd-parent-container" ref={ref} />
147}