Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

PreactQueryDevtools.tsx

139 lines3.7 KB

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