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 destroyRef = inject(DestroyRef)
42 const isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
43 const injectedClient = inject(QueryClient, { optional: true })
44β
45 const options = computed(optionsFn)
46 let devtools: TanstackQueryDevtoolsPanel | null = null
47β
48 const destroy = () => {
49 devtools?.unmount()
50 devtools = null
51 }
52β
53 if (!isBrowser)
54 return {
55 destroy,
56 }
57β
58 effect(() => {
59 const {
60 client = injectedClient,
61 errorTypes = [],
62 styleNonce,
63 shadowDOMTarget,
64 onClose,
65 hostElement,
66 } = options()
67β
68 untracked(() => {
69 if (!client) throw new Error('No QueryClient found')
70 if (!devtools && hostElement) {
71 devtools = new TanstackQueryDevtoolsPanel({
72 client,
73 queryFlavor: 'Angular Query',
74 version: '5',
75 buttonPosition: 'bottom-left',
76 position: 'bottom',
77 initialIsOpen: true,
78 errorTypes,
79 styleNonce,
80 shadowDOMTarget,
81 onClose,
82 onlineManager,
83 })
84 devtools.mount(hostElement.nativeElement)
85 } else if (devtools && hostElement) {
86 devtools.setClient(client)
87 devtools.setErrorTypes(errorTypes)
88 onClose && devtools.setOnClose(onClose)
89 } else if (devtools && !hostElement) {
90 destroy()
91 }
92 })
93 })
94β
95 destroyRef.onDestroy(destroy)
96β
97 return {
98 destroy,
99 }
100 })
101}
102β
103/**
104 * A devtools panel, which can be manually destroyed.
105 */
106export interface DevtoolsPanelRef {
107 /**
108 * Destroy the panel, removing it from the DOM and stops listening to signal changes.
109 */
110 destroy: () => void
111}
112β
113export interface DevtoolsPanelOptions {
114 /**
115 * Custom instance of QueryClient
116 */
117 client?: QueryClient
118 /**
119 * Use this so you can define custom errors that can be shown in the devtools.
120 */
121 errorTypes?: Array<DevtoolsErrorType>
122 /**
123 * 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.
124 */
125 styleNonce?: string
126 /**
127 * Use this so you can attach the devtool's styles to specific element in the DOM.
128 */
129 shadowDOMTarget?: ShadowRoot
130β
131 /**
132 * Callback function that is called when the devtools panel is closed
133 */
134 onClose?: () => unknown
135β
136 /**
137 * Element where to render the devtools panel. When set to undefined or null, the devtools panel will not be created, or destroyed if existing.
138 * If changed from undefined to a ElementRef, the devtools panel will be created.
139 */
140 hostElement?: ElementRef
141}