Open History to see the last commit
1import { onlineManager, useQueryClient } from '@tanstack/preact-query'
2import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
3import { useEffect, useRef, useState } from 'preact/hooks'
4import type { QueryClient } from '@tanstack/preact-query'
5import type { DevtoolsErrorType, Theme } from '@tanstack/query-devtools'
6import type { CSSProperties, VNode } from 'preact'
7β
8/**
9 * The props of `PreactQueryDevtoolsPanel`, which renders the devtools panel inline.
10 */
11export interface DevtoolsPanelOptions {
12 /**
13 * Use this to provide a custom QueryClient. Otherwise, the one from the
14 * nearest QueryClientProvider will be used.
15 */
16 client?: QueryClient
17 /**
18 * Custom error types to be shown in the devtools.
19 */
20 errorTypes?: Array<DevtoolsErrorType>
21 /**
22 * @deprecated The devtools no longer add a `<style>` element, so they need no
23 * nonce. This option does nothing.
24 */
25 styleNonce?: string
26 /**
27 * The shadow root to add the devtools styles to. By default, the styles are
28 * added to the shadow root the devtools render in, or to the document when
29 * they do not render in a shadow root.
30 */
31 shadowDOMTarget?: ShadowRoot
32 /**
33 * The Trusted Types policy the devtools use to create their HTML templates.
34 * Pass one when your page enforces `require-trusted-types-for 'script'`.
35 * The devtools only pass it their own markup.
36 */
37 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
38 /**
39 * Custom styles for the devtools panel container.
40 * @default { height: '100%', display: 'flex', flexDirection: 'column' }
41 * @example { height: '100%' }
42 * @example { height: '100%', width: '100%' }
43 */
44 style?: CSSProperties
45 /**
46 * Callback function when the devtools panel is closed.
47 */
48 onClose?: () => unknown
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 * This overrides the theme selected in the devtools Settings menu. When omitted, the
56 * selected theme is used, or `'system'` if none is selected.
57 */
58 theme?: Theme
59}
60β
61/**
62 * Renders the TanStack Query devtools panel inline, for the given or nearest `QueryClient`.
63 * @param props - The devtools panel options.
64 * @returns The element the devtools panel is mounted in.
65 */
66export function PreactQueryDevtoolsPanel(
67 props: DevtoolsPanelOptions,
68): VNode | null {
69 const queryClient = useQueryClient(props.client)
70 const ref = useRef<HTMLDivElement>(null)
71 const {
72 errorTypes,
73 shadowDOMTarget,
74 trustedTypesPolicy,
75 hideDisabledQueries,
76 theme,
77 } = props
78 const [devtools] = useState(
79 () =>
80 new TanstackQueryDevtoolsPanel({
81 client: queryClient,
82 queryFlavor: 'Preact Query',
83 version: '5',
84 onlineManager,
85 buttonPosition: 'bottom-left',
86 position: 'bottom',
87 initialIsOpen: true,
88 errorTypes,
89 shadowDOMTarget,
90 trustedTypesPolicy,
91 onClose: props.onClose,
92 hideDisabledQueries,
93 theme,
94 }),
95 )
96β
97 useEffect(() => {
98 devtools.setClient(queryClient)
99 }, [queryClient, devtools])
100β
101 useEffect(() => {
102 devtools.setOnClose(props.onClose ?? (() => {}))
103 }, [props.onClose, devtools])
104β
105 useEffect(() => {
106 devtools.setErrorTypes(errorTypes || [])
107 }, [errorTypes, devtools])
108β
109 useEffect(() => {
110 devtools.setTheme(theme)
111 }, [theme, devtools])
112β
113 useEffect(() => {
114 if (ref.current) {
115 devtools.mount(ref.current)
116 }
117 return () => {
118 devtools.unmount()
119 }
120 }, [devtools])
121β
122 return (
123 <div
124 style={{
125 height: '100%',
126 display: 'flex',
127 flexDirection: 'column',
128 ...props.style,
129 }}
130 className="tsqd-parent-container"
131 ref={ref}
132 />
133 )
134}