Open History to see the last commit
1import { isPlatformBrowser } from '@angular/common'
2import {
3 DestroyRef,
4 PLATFORM_ID,
5 assertInInjectionContext,
6 computed,
7 effect,
8 inject,
9 untracked,
10} from '@angular/core'
11import { QueryClient, onlineManager } from '@tanstack/angular-query'
12import type { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
13import type { DevtoolsPanelOptions, InjectDevtoolsPanel } from './types'
14β
15/**
16 * Inject a TanStack Query devtools panel and render it in the DOM.
17 *
18 * Devtools panel allows programmatic control over the devtools, for example if you want to render
19 * the devtools as part of your own devtools.
20 *
21 * Consider `withDevtools` instead if you don't need this.
22 * @param injectDevtoolsPanelFn - A function that returns devtools panel options.
23 * @returns DevtoolsPanelRef
24 * @see https://tanstack.com/query/v5/docs/framework/angular/devtools
25 */
26export const injectDevtoolsPanel: InjectDevtoolsPanel = (
27 injectDevtoolsPanelFn: () => DevtoolsPanelOptions,
28) => {
29 assertInInjectionContext(injectDevtoolsPanel)
30 const destroyRef = inject(DestroyRef)
31 const isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
32 const injectedClient = inject(QueryClient, { optional: true })
33β
34 const queryOptions = computed(injectDevtoolsPanelFn)
35 let devtools: TanstackQueryDevtoolsPanel | null = null
36 // oxlint-disable-next-line prefer-const -- assigned after the server early return, but `destroy` may run before it
37 let optionsEffect: ReturnType<typeof effect> | undefined
38 let manuallyDestroyed = false
39 let loadGeneration = 0
40β
41 const unmount = () => {
42 devtools?.unmount()
43 devtools = null
44 }
45β
46 const destroy = () => {
47 if (manuallyDestroyed) return
48β
49 manuallyDestroyed = true
50 loadGeneration++
51 optionsEffect?.destroy()
52 unmount()
53 }
54β
55 if (!isBrowser)
56 return {
57 destroy,
58 }
59β
60 optionsEffect = effect(() => {
61 const generation = ++loadGeneration
62 const {
63 client = injectedClient,
64 errorTypes = [],
65 theme,
66 shadowDOMTarget,
67 trustedTypesPolicy,
68 hideDisabledQueries,
69 onClose,
70 hostElement,
71 } = queryOptions()
72β
73 untracked(() => {
74 if (!client) throw new Error('No QueryClient found')
75 if (!devtools && hostElement) {
76 import('@tanstack/query-devtools')
77 .then((queryDevtools) => {
78 if (
79 manuallyDestroyed ||
80 destroyRef.destroyed ||
81 generation !== loadGeneration
82 ) {
83 return
84 }
85β
86 devtools = new queryDevtools.TanstackQueryDevtoolsPanel({
87 client,
88 queryFlavor: 'Angular Query',
89 version: '5',
90 buttonPosition: 'bottom-left',
91 position: 'bottom',
92 initialIsOpen: true,
93 errorTypes,
94 theme,
95 shadowDOMTarget,
96 trustedTypesPolicy,
97 hideDisabledQueries,
98 onClose,
99 onlineManager,
100 })
101 devtools.mount(hostElement.nativeElement)
102 })
103 .catch((error) => {
104 console.error('Failed to load @tanstack/query-devtools.', error)
105 })
106 } else if (devtools && hostElement) {
107 devtools.setClient(client)
108 devtools.setErrorTypes(errorTypes)
109 devtools.setTheme(theme)
110 devtools.setOnClose(onClose ?? (() => {}))
111 } else if (devtools && !hostElement) {
112 unmount()
113 }
114 })
115 })
116β
117 destroyRef.onDestroy(destroy)
118β
119 return {
120 destroy,
121 }
122}