Open History to see the last commit
1import {
2 createEffect,
3 createMemo,
4 createSignal,
5 onCleanup,
6 onMount,
7 sharedConfig,
8 splitProps,
9 untrack,
10} from 'solid-js'
11import { onlineManager, useQueryClient } from '@tanstack/solid-query'
12import { isServer } from 'solid-js/web'
13import { TanstackQueryDevtools } from '@tanstack/query-devtools'
14import type {
15 DevToolsErrorType,
16 DevtoolsButtonPosition,
17 DevtoolsPosition,
18} from '@tanstack/query-devtools'
19import type { QueryClient } from '@tanstack/solid-query'
20import type { Component, ComponentProps, JSX } from 'solid-js'
21β
22interface DevtoolsOptions {
23 /**
24 * Set this true if you want the dev tools to default to being open
25 */
26 initialIsOpen?: boolean
27 /**
28 * The position of the React Query logo to open and close the devtools panel.
29 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
30 * Defaults to 'bottom-right'.
31 */
32 buttonPosition?: DevtoolsButtonPosition
33 /**
34 * The position of the React Query devtools panel.
35 * 'top' | 'bottom' | 'left' | 'right'
36 * Defaults to 'bottom'.
37 */
38 position?: DevtoolsPosition
39 /**
40 * Custom instance of QueryClient
41 */
42 client?: QueryClient
43 /**
44 * Use this so you can define custom errors that can be shown in the devtools.
45 */
46 errorTypes?: Array<DevToolsErrorType>
47 /**
48 * 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.
49 */
50 styleNonce?: string
51 /**
52 * Use this so you can attach the devtool's styles to specific element in the DOM.
53 */
54 shadowDOMTarget?: ShadowRoot
55}
56β
57export default function SolidQueryDevtools(props: DevtoolsOptions) {
58 const queryClient = useQueryClient()
59 const client = createMemo(() => props.client || queryClient)
60 let ref!: HTMLDivElement
61 const devtools = new TanstackQueryDevtools({
62 client: client(),
63 queryFlavor: 'Solid Query',
64 version: '5',
65 onlineManager,
66 buttonPosition: props.buttonPosition,
67 position: props.position,
68 initialIsOpen: props.initialIsOpen,
69 errorTypes: props.errorTypes,
70 styleNonce: props.styleNonce,
71 shadowDOMTarget: props.shadowDOMTarget,
72 })
73β
74 createEffect(() => {
75 devtools.setClient(client())
76 })
77β
78 createEffect(() => {
79 const buttonPos = props.buttonPosition
80 if (buttonPos) {
81 devtools.setButtonPosition(buttonPos)
82 }
83 })
84β
85 createEffect(() => {
86 const pos = props.position
87 if (pos) {
88 devtools.setPosition(pos)
89 }
90 })
91β
92 createEffect(() => {
93 devtools.setInitialIsOpen(props.initialIsOpen || false)
94 })
95β
96 createEffect(() => {
97 devtools.setErrorTypes(props.errorTypes || [])
98 })
99β
100 onMount(() => {
101 devtools.mount(ref)
102 onCleanup(() => devtools.unmount())
103 })
104β
105 return <div class="tsqd-parent-container" ref={ref}></div>
106}
107β
108/*
109 This function has been taken from solid-start's codebase
110 This allows the devtools to be loaded only on the client and bypasses any server side rendering
111 https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
112*/
113export function clientOnly<T extends Component<any>>(
114 fn: () => Promise<{
115 default: T
116 }>,
117) {
118 if (isServer)
119 return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
120 props.fallback
121β
122 const [comp, setComp] = createSignal<T>()
123 fn().then((m) => setComp(() => m.default))
124 return (props: ComponentProps<T>) => {
125 let Comp: T | undefined
126 let m: boolean
127 const [, rest] = splitProps(props, ['fallback'])
128 if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
129 const [mounted, setMounted] = createSignal(!sharedConfig.context)
130 onMount(() => setMounted(true))
131 return createMemo(
132 () => (
133 (Comp = comp()),
134 (m = mounted()),
135 untrack(() => (Comp && m ? Comp(rest) : props.fallback))
136 ),
137 )
138 }
139}