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β
53export default function SolidQueryDevtools(props: DevtoolsOptions) {
54 const queryClient = useQueryClient()
55 const client = createMemo(() => props.client || queryClient)
56 let ref!: HTMLDivElement
57 const devtools = new TanstackQueryDevtools({
58 client: client(),
59 queryFlavor: 'Solid Query',
60 version: '5',
61 onlineManager,
62 buttonPosition: props.buttonPosition,
63 position: props.position,
64 initialIsOpen: props.initialIsOpen,
65 errorTypes: props.errorTypes,
66 styleNonce: props.styleNonce,
67 })
68β
69 createEffect(() => {
70 devtools.setClient(client())
71 })
72β
73 createEffect(() => {
74 const buttonPos = props.buttonPosition
75 if (buttonPos) {
76 devtools.setButtonPosition(buttonPos)
77 }
78 })
79β
80 createEffect(() => {
81 const pos = props.position
82 if (pos) {
83 devtools.setPosition(pos)
84 }
85 })
86β
87 createEffect(() => {
88 devtools.setInitialIsOpen(props.initialIsOpen || false)
89 })
90β
91 createEffect(() => {
92 devtools.setErrorTypes(props.errorTypes || [])
93 })
94β
95 onMount(() => {
96 devtools.mount(ref)
97 onCleanup(() => devtools.unmount())
98 })
99β
100 return <div class="tsqd-parent-container" ref={ref}></div>
101}
102β
103/*
104 This function has been taken from solid-start's codebase
105 This allows the devtools to be loaded only on the client and bypasses any server side rendering
106 https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
107*/
108export function clientOnly<T extends Component<any>>(
109 fn: () => Promise<{
110 default: T
111 }>,
112) {
113 if (isServer)
114 return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
115 props.fallback
116β
117 const [comp, setComp] = createSignal<T>()
118 fn().then((m) => setComp(() => m.default))
119 return (props: ComponentProps<T>) => {
120 let Comp: T | undefined
121 let m: boolean
122 const [, rest] = splitProps(props, ['fallback'])
123 if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
124 const [mounted, setMounted] = createSignal(!sharedConfig.context)
125 onMount(() => setMounted(true))
126 return createMemo(
127 () => (
128 (Comp = comp()),
129 (m = mounted()),
130 untrack(() => (Comp && m ? Comp(rest) : props.fallback))
131 ),
132 )
133 }
134}