Open History to see the last commit
1import {
2 DestroyRef,
3 Injector,
4 PLATFORM_ID,
5 assertInInjectionContext,
6 computed,
7 effect,
8 inject,
9 runInInjectionContext,
10 untracked,
11} from '@angular/core'
12import { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
13import {
14 QueryClient,
15 onlineManager,
16} from '@tanstack/angular-query-experimental'
17import { isPlatformBrowser } from '@angular/common'
18import type { ElementRef } from '@angular/core'
19import type { DevtoolsErrorType } from '@tanstack/query-devtools'
20β
21/**
22 * Inject a TanStack Query devtools panel and render it in the DOM.
23 *
24 * Devtools panel allows programmatic control over the devtools, for example if you want to render
25 * the devtools as part of your own devtools.
26 *
27 * Consider `withDevtools` instead if you don't need this.
28 * @param optionsFn - A function that returns devtools panel options.
29 * @param injector - The Angular injector to use.
30 * @returns DevtoolsPanelRef
31 * @see https://tanstack.com/query/v5/docs/framework/angular/devtools
32 */
33export function injectDevtoolsPanel(
34 optionsFn: () => DevtoolsPanelOptions,
35 injector?: Injector,
36): DevtoolsPanelRef {
37 !injector && assertInInjectionContext(injectDevtoolsPanel)
38 const currentInjector = injector ?? inject(Injector)
39β
40 return runInInjectionContext(currentInjector, () => {
41 const options = computed(optionsFn)
42 let devtools: TanstackQueryDevtoolsPanel | null = null
43β
44 const isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
45β
46 const destroy = () => {
47 devtools?.unmount()
48 devtools = null
49 }
50β
51 if (!isBrowser)
52 return {
53 destroy,
54 }
55β
56 const destroyRef = inject(DestroyRef)
57β
58 effect(() => {
59 const injectedClient = currentInjector.get(QueryClient, null)
60 const {
61 client = injectedClient,
62 errorTypes = [],
63 styleNonce,
64 shadowDOMTarget,
65 onClose,
66 hostElement,
67 } = options()
68β
69 untracked(() => {
70 if (!client) throw new Error('No QueryClient found')
71 if (!devtools && hostElement) {
72 devtools = new TanstackQueryDevtoolsPanel({
73 client,
74 queryFlavor: 'Angular Query',
75 version: '5',
76 buttonPosition: 'bottom-left',
77 position: 'bottom',
78 initialIsOpen: true,
79 errorTypes,
80 styleNonce,
81 shadowDOMTarget,
82 onClose,
83 onlineManager,
84 })
85 devtools.mount(hostElement.nativeElement)
86 } else if (devtools && hostElement) {
87 devtools.setClient(client)
88 devtools.setErrorTypes(errorTypes)
89 onClose && devtools.setOnClose(onClose)
90 } else if (devtools && !hostElement) {
91 destroy()
92 }
93 })
94 })
95β
96 destroyRef.onDestroy(destroy)
97β
98 return {
99 destroy,
100 }
101 })
102}
103β
104/**
105 * A devtools panel, which can be manually destroyed.
106 */
107export interface DevtoolsPanelRef {
108 /**
109 * Destroy the panel, removing it from the DOM and stops listening to signal changes.
110 */
111 destroy: () => void
112}
113β
114export interface DevtoolsPanelOptions {
115 /**
116 * Custom instance of QueryClient
117 */
118 client?: QueryClient
119 /**
120 * Use this so you can define custom errors that can be shown in the devtools.
121 */
122 errorTypes?: Array<DevtoolsErrorType>
123 /**
124 * 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.
125 */
126 styleNonce?: string
127 /**
128 * Use this so you can attach the devtool's styles to specific element in the DOM.
129 */
130 shadowDOMTarget?: ShadowRoot
131β
132 /**
133 * Callback function that is called when the devtools panel is closed
134 */
135 onClose?: () => unknown
136β
137 /**
138 * Element where to render the devtools panel. When set to undefined or null, the devtools panel will not be created, or destroyed if existing.
139 * If changed from undefined to a ElementRef, the devtools panel will be created.
140 */
141 hostElement?: ElementRef
142}