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

devtools.tsx

119 lines3.4 KB

1import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
2import { onlineManager, useQueryClient } from '@tanstack/solid-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/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 * Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
39 */
40 styleNonce?: string
41 /**
42 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
43 * within the shadow DOM instead of within the head tag in the light DOM.
44 */
45 shadowDOMTarget?: ShadowRoot
46 /**
47 * Set this to true to hide disabled queries from the devtools panel.
48 */
49 hideDisabledQueries?: boolean
50 /**
51 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
52 * This overrides the theme selected in the devtools Settings menu. When omitted, the
53 * selected theme is used, or `'system'` if none is selected.
54 */
55 theme?: Theme
57​
58/**
59 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
60 * nearest `QueryClient`.
61 * @param props - The devtools options.
62 * @returns The element the devtools are mounted in.
63 */
64export default function SolidQueryDevtools(props: DevtoolsOptions) {
65 const queryClient = useQueryClient(props.client)
66 const client = createMemo(() => queryClient)
67 let ref!: HTMLDivElement
68 const devtools = new TanstackQueryDevtools({
69 client: client(),
70 queryFlavor: 'Solid Query',
71 version: '5',
72 onlineManager,
73 buttonPosition: props.buttonPosition,
74 position: props.position,
75 initialIsOpen: props.initialIsOpen,
76 errorTypes: props.errorTypes,
77 styleNonce: props.styleNonce,
78 shadowDOMTarget: props.shadowDOMTarget,
79 hideDisabledQueries: props.hideDisabledQueries,
80 theme: props.theme,
81 })
82​
83 createEffect(() => {
84 devtools.setClient(client())
85 })
86​
87 createEffect(() => {
88 const buttonPos = props.buttonPosition
89 if (buttonPos) {
90 devtools.setButtonPosition(buttonPos)
91 }
92 })
93​
94 createEffect(() => {
95 const pos = props.position
96 if (pos) {
97 devtools.setPosition(pos)
98 }
99 })
100​
101 createEffect(() => {
102 devtools.setInitialIsOpen(props.initialIsOpen || false)
103 })
104​
105 createEffect(() => {
106 devtools.setErrorTypes(props.errorTypes || [])
107 })
108​
109 createEffect(() => {
110 devtools.setTheme(props.theme)
111 })
112​
113 onMount(() => {
114 devtools.mount(ref)
115 onCleanup(() => devtools.unmount())
116 })
117​
118 return <div class="tsqd-parent-container" ref={ref} />