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

devtools.tsx

100 lines2.8 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} from '@tanstack/query-devtools'
9import type { QueryClient } from '@tanstack/solid-query'
10​
11interface DevtoolsOptions {
12 /**
13 * Set this true if you want the dev tools to default to being open
14 */
15 initialIsOpen?: boolean
16 /**
17 * The position of the React Query logo to open and close the devtools panel.
18 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
19 * Defaults to 'bottom-right'.
20 */
21 buttonPosition?: DevtoolsButtonPosition
22 /**
23 * The position of the React Query devtools panel.
24 * 'top' | 'bottom' | 'left' | 'right'
25 * Defaults to 'bottom'.
26 */
27 position?: DevtoolsPosition
28 /**
29 * Custom instance of QueryClient
30 */
31 client?: QueryClient
32 /**
33 * Use this so you can define custom errors that can be shown in the devtools.
34 */
35 errorTypes?: Array<DevtoolsErrorType>
36 /**
37 * 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.
38 */
39 styleNonce?: string
40 /**
41 * Use this so you can attach the devtool's styles to specific element in the DOM.
42 */
43 shadowDOMTarget?: ShadowRoot
44 /**
45 * Set this to true to hide disabled queries from the devtools panel.
46 */
47 hideDisabledQueries?: boolean
49​
50export default function SolidQueryDevtools(props: DevtoolsOptions) {
51 const queryClient = useQueryClient(props.client)
52 const client = createMemo(() => queryClient)
53 let ref!: HTMLDivElement
54 const devtools = new TanstackQueryDevtools({
55 client: client(),
56 queryFlavor: 'Solid Query',
57 version: '5',
58 onlineManager,
59 buttonPosition: props.buttonPosition,
60 position: props.position,
61 initialIsOpen: props.initialIsOpen,
62 errorTypes: props.errorTypes,
63 styleNonce: props.styleNonce,
64 shadowDOMTarget: props.shadowDOMTarget,
65 hideDisabledQueries: props.hideDisabledQueries,
66 })
67​
68 createEffect(() => {
69 devtools.setClient(client())
70 })
71​
72 createEffect(() => {
73 const buttonPos = props.buttonPosition
74 if (buttonPos) {
75 devtools.setButtonPosition(buttonPos)
76 }
77 })
78​
79 createEffect(() => {
80 const pos = props.position
81 if (pos) {
82 devtools.setPosition(pos)
83 }
84 })
85​
86 createEffect(() => {
87 devtools.setInitialIsOpen(props.initialIsOpen || false)
88 })
89​
90 createEffect(() => {
91 devtools.setErrorTypes(props.errorTypes || [])
92 })
93​
94 onMount(() => {
95 devtools.mount(ref)
96 onCleanup(() => devtools.unmount())
97 })
98​
99 return <div class="tsqd-parent-container" ref={ref}></div>