|
| 1 | +import { createEffect, createMemo, onCleanup, onMount } from 'solid-js' |
| 2 | +import { onlineManager, useQueryClient } from '@tanstack/solid-query' |
| 3 | +import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools' |
| 4 | +import type { DevtoolsErrorType } from '@tanstack/query-devtools' |
| 5 | +import type { QueryClient } from '@tanstack/solid-query' |
| 6 | + |
| 7 | +export interface DevtoolsPanelOptions { |
| 8 | + /** |
| 9 | + * Custom instance of QueryClient |
| 10 | + */ |
| 11 | + client?: QueryClient |
| 12 | + /** |
| 13 | + * Use this so you can define custom errors that can be shown in the devtools. |
| 14 | + */ |
| 15 | + errorTypes?: Array<DevtoolsErrorType> |
| 16 | + /** |
| 17 | + * 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. |
| 18 | + */ |
| 19 | + styleNonce?: string |
| 20 | + /** |
| 21 | + * Use this so you can attach the devtool's styles to specific element in the DOM. |
| 22 | + */ |
| 23 | + shadowDOMTarget?: ShadowRoot |
| 24 | + |
| 25 | + /** |
| 26 | + * Custom styles for the devtools panel |
| 27 | + * @default { height: '500px' } |
| 28 | + * @example { height: '100%' } |
| 29 | + * @example { height: '100%', width: '100%' } |
| 30 | + */ |
| 31 | + style?: CSSStyleValue |
| 32 | + |
| 33 | + /** |
| 34 | + * Callback function that is called when the devtools panel is closed |
| 35 | + */ |
| 36 | + onClose?: () => unknown |
| 37 | + /** |
| 38 | + * Set this to true to hide disabled queries from the devtools panel. |
| 39 | + */ |
| 40 | + hideDisabledQueries?: boolean |
| 41 | +} |
| 42 | + |
| 43 | +export default function SolidQueryDevtoolsPanel(props: DevtoolsPanelOptions) { |
| 44 | + const queryClient = useQueryClient() |
| 45 | + const client = createMemo(() => props.client || queryClient) |
| 46 | + let ref!: HTMLDivElement |
| 47 | + const { errorTypes, styleNonce, shadowDOMTarget, hideDisabledQueries } = props |
| 48 | + const devtools = new TanstackQueryDevtoolsPanel({ |
| 49 | + client: client(), |
| 50 | + queryFlavor: 'Solid Query', |
| 51 | + version: '5', |
| 52 | + onlineManager, |
| 53 | + buttonPosition: 'bottom-left', |
| 54 | + position: 'bottom', |
| 55 | + initialIsOpen: true, |
| 56 | + errorTypes, |
| 57 | + styleNonce, |
| 58 | + shadowDOMTarget, |
| 59 | + onClose: props.onClose, |
| 60 | + hideDisabledQueries, |
| 61 | + }) |
| 62 | + createEffect(() => { |
| 63 | + devtools.setClient(client()) |
| 64 | + }) |
| 65 | + createEffect(() => { |
| 66 | + devtools.setOnClose(props.onClose ?? (() => {})) |
| 67 | + }) |
| 68 | + |
| 69 | + createEffect(() => { |
| 70 | + devtools.setErrorTypes(props.errorTypes || []) |
| 71 | + }) |
| 72 | + |
| 73 | + onMount(() => { |
| 74 | + devtools.mount(ref) |
| 75 | + onCleanup(() => devtools.unmount()) |
| 76 | + }) |
| 77 | + |
| 78 | + return ( |
| 79 | + <div |
| 80 | + style={{ height: '500px', ...props.style }} |
| 81 | + class="tsqd-parent-container" |
| 82 | + ref={ref} |
| 83 | + /> |
| 84 | + ) |
| 85 | +} |
0 commit comments