Open History to see the last commit
1import {
2 DestroyRef,
3 PLATFORM_ID,
4 assertInInjectionContext,
5 computed,
6 effect,
7 inject,
8 untracked,
9} from '@angular/core'
10import { QueryClient, onlineManager } from '@tanstack/query-core'
11import { isPlatformBrowser } from '@angular/common'
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 let optionsEffect: ReturnType<typeof effect> | undefined
37 let manuallyDestroyed = false
38 let loadGeneration = 0
39β
40 const unmount = () => {
41 devtools?.unmount()
42 devtools = null
43 }
44β
45 const destroy = () => {
46 if (manuallyDestroyed) return
47β
48 manuallyDestroyed = true
49 loadGeneration++
50 optionsEffect?.destroy()
51 unmount()
52 }
53β
54 if (!isBrowser)
55 return {
56 destroy,
57 }
58β
59 optionsEffect = effect(() => {
60 const generation = ++loadGeneration
61 const {
62 client = injectedClient,
63 errorTypes = [],
64 theme = 'system',
65 styleNonce,
66 shadowDOMTarget,
67 onClose,
68 hostElement,
69 } = queryOptions()
70β
71 untracked(() => {
72 if (!client) throw new Error('No QueryClient found')
73 if (!devtools && hostElement) {
74 import('@tanstack/query-devtools')
75 .then((queryDevtools) => {
76 if (
77 manuallyDestroyed ||
78 destroyRef.destroyed ||
79 generation !== loadGeneration
80 ) {
81 return
82 }
83β
84 devtools = new queryDevtools.TanstackQueryDevtoolsPanel({
85 client,
86 queryFlavor: 'Angular Query',
87 version: '5',
88 buttonPosition: 'bottom-left',
89 position: 'bottom',
90 initialIsOpen: true,
91 errorTypes,
92 theme,
93 styleNonce,
94 shadowDOMTarget,
95 onClose,
96 onlineManager,
97 })
98 devtools.mount(hostElement.nativeElement)
99 })
100 .catch((error) => {
101 console.error('Failed to load @tanstack/query-devtools.', error)
102 })
103 } else if (devtools && hostElement) {
104 devtools.setClient(client)
105 devtools.setErrorTypes(errorTypes)
106 devtools.setTheme(theme)
107 devtools.setOnClose(onClose ?? (() => {}))
108 } else if (devtools && !hostElement) {
109 unmount()
110 }
111 })
112 })
113β
114 destroyRef.onDestroy(destroy)
115β
116 return {
117 destroy,
118 }
119}