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

devtools.tsx

127 lines3.7 KB

1import { TanstackQueryDevtools } from '@tanstack/query-devtools'
2import { onlineManager, useQueryClient } from '@tanstack/solid-query'
3import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
4import type {
5 DevtoolsButtonPosition,
6 DevtoolsErrorType,
7 DevtoolsPosition,
8 Theme,
9} from '@tanstack/query-devtools'
10import type { QueryClient } from '@tanstack/solid-query'
11​
12interface DevtoolsOptions {
13 /**
14 * Set this true if you want the dev tools to default to being open
15 */
16 initialIsOpen?: boolean
17 /**
18 * The position of the TanStack logo to open and close the devtools panel.
19 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
20 * @defaultValue bottom-right
21 */
22 buttonPosition?: DevtoolsButtonPosition
23 /**
24 * The position of the Solid Query devtools panel.
25 * 'top' | 'bottom' | 'left' | 'right'
26 * @defaultValue bottom
27 */
28 position?: DevtoolsPosition
29 /**
30 * Custom instance of QueryClient
31 */
32 client?: QueryClient
33 /**
34 * Use this so you can define custom errors that can be shown in the devtools.
35 */
36 errorTypes?: Array<DevtoolsErrorType>
37 /**
38 * @deprecated The devtools no longer add a `<style>` element, so they need no
39 * nonce. This option does nothing.
40 */
41 styleNonce?: string
42 /**
43 * The shadow root to add the devtools styles to. By default, the styles are
44 * added to the shadow root the devtools render in, or to the document when
45 * they do not render in a shadow root.
46 */
47 shadowDOMTarget?: ShadowRoot
48 /**
49 * The Trusted Types policy the devtools use to create their HTML templates.
50 * Pass one when your page enforces `require-trusted-types-for 'script'`.
51 * The devtools only pass it their own markup.
52 */
53 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
54 /**
55 * Set this to true to hide disabled queries from the devtools panel.
56 */
57 hideDisabledQueries?: boolean
58 /**
59 * Set this to 'light', 'dark', or 'system' to change 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
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 default function SolidQueryDevtools(props: DevtoolsOptions) {
73 const queryClient = useQueryClient(props.client)
74 const client = createMemo(() => queryClient)
75 let ref!: HTMLDivElement
76 const devtools = new TanstackQueryDevtools({
77 client: client(),
78 queryFlavor: 'Solid Query',
79 version: '5',
80 onlineManager,
81 buttonPosition: props.buttonPosition,
82 position: props.position,
83 initialIsOpen: props.initialIsOpen,
84 errorTypes: props.errorTypes,
85 shadowDOMTarget: props.shadowDOMTarget,
86 trustedTypesPolicy: props.trustedTypesPolicy,
87 hideDisabledQueries: props.hideDisabledQueries,
88 theme: props.theme,
89 })
90​
91 createEffect(() => {
92 devtools.setClient(client())
93 })
94​
95 createEffect(() => {
96 const buttonPos = props.buttonPosition
97 if (buttonPos) {
98 devtools.setButtonPosition(buttonPos)
99 }
100 })
101​
102 createEffect(() => {
103 const pos = props.position
104 if (pos) {
105 devtools.setPosition(pos)
106 }
107 })
108​
109 createEffect(() => {
110 devtools.setInitialIsOpen(props.initialIsOpen || false)
111 })
112​
113 createEffect(() => {
114 devtools.setErrorTypes(props.errorTypes || [])
115 })
116​
117 createEffect(() => {
118 devtools.setTheme(props.theme)
119 })
120​
121 onMount(() => {
122 devtools.mount(ref)
123 onCleanup(() => devtools.unmount())
124 })
125​
126 return <div class="tsqd-parent-container" ref={ref} />