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

devtools.tsx

111 lines3.0 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 React Query logo to open and close the devtools panel.
19 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
20 * Defaults to 'bottom-right'.
21 */
22 buttonPosition?: DevtoolsButtonPosition
23 /**
24 * The position of the React Query devtools panel.
25 * 'top' | 'bottom' | 'left' | 'right'
26 * Defaults to '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 so you can attach the devtool's styles to specific element in the DOM.
43 */
44 shadowDOMTarget?: ShadowRoot
45 /**
46 * Set this to true to hide disabled queries from the devtools panel.
47 */
48 hideDisabledQueries?: boolean
49 /**
50 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
51 * Defaults to 'system'.
52 */
53 theme?: Theme
55​
56export default function SolidQueryDevtools(props: DevtoolsOptions) {
57 const queryClient = useQueryClient(props.client)
58 const client = createMemo(() => queryClient)
59 let ref!: HTMLDivElement
60 const devtools = new TanstackQueryDevtools({
61 client: client(),
62 queryFlavor: 'Solid Query',
63 version: '5',
64 onlineManager,
65 buttonPosition: props.buttonPosition,
66 position: props.position,
67 initialIsOpen: props.initialIsOpen,
68 errorTypes: props.errorTypes,
69 styleNonce: props.styleNonce,
70 shadowDOMTarget: props.shadowDOMTarget,
71 hideDisabledQueries: props.hideDisabledQueries,
72 theme: props.theme,
73 })
74​
75 createEffect(() => {
76 devtools.setClient(client())
77 })
78​
79 createEffect(() => {
80 const buttonPos = props.buttonPosition
81 if (buttonPos) {
82 devtools.setButtonPosition(buttonPos)
83 }
84 })
85​
86 createEffect(() => {
87 const pos = props.position
88 if (pos) {
89 devtools.setPosition(pos)
90 }
91 })
92​
93 createEffect(() => {
94 devtools.setInitialIsOpen(props.initialIsOpen || false)
95 })
96​
97 createEffect(() => {
98 devtools.setErrorTypes(props.errorTypes || [])
99 })
100​
101 createEffect(() => {
102 devtools.setTheme(props.theme || 'system')
103 })
104​
105 onMount(() => {
106 devtools.mount(ref)
107 onCleanup(() => devtools.unmount())
108 })
109​
110 return <div class="tsqd-parent-container" ref={ref}></div>