Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-devtools-panel.ts

110 lines3.2 KB

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 { QueryClient, onlineManager } from '@tanstack/query-core'
13import { isPlatformBrowser } from '@angular/common'
14import type { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
15import type {
16 DevtoolsPanelOptions,
17 InjectDevtoolsPanel,
18 InjectDevtoolsPanelOptions,
19} from './types'
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 injectDevtoolsPanelFn - A function that returns devtools panel options.
29 * @param options - Additional configuration
30 * @returns DevtoolsPanelRef
31 * @see https://tanstack.com/query/v5/docs/framework/angular/devtools
32 */
33export const injectDevtoolsPanel: InjectDevtoolsPanel = (
34 injectDevtoolsPanelFn: () => DevtoolsPanelOptions,
35 options?: InjectDevtoolsPanelOptions,
36) => {
37 !options?.injector && assertInInjectionContext(injectDevtoolsPanel)
38 const currentInjector = options?.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 queryOptions = computed(injectDevtoolsPanelFn)
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 } = queryOptions()
67​
68 untracked(() => {
69 if (!client) throw new Error('No QueryClient found')
70 if (!devtools && hostElement) {
71 import('@tanstack/query-devtools')
72 .then((queryDevtools) => {
73 devtools = new queryDevtools.TanstackQueryDevtoolsPanel({
74 client,
75 queryFlavor: 'Angular Query',
76 version: '5',
77 buttonPosition: 'bottom-left',
78 position: 'bottom',
79 initialIsOpen: true,
80 errorTypes,
81 styleNonce,
82 shadowDOMTarget,
83 onClose,
84 onlineManager,
85 })
86 devtools.mount(hostElement.nativeElement)
87 })
88 .catch((error) => {
89 console.error(
90 'Install @tanstack/query-devtools or reinstall without --omit=optional.',
91 error,
92 )
93 })
94 } else if (devtools && hostElement) {
95 devtools.setClient(client)
96 devtools.setErrorTypes(errorTypes)
97 onClose && devtools.setOnClose(onClose)
98 } else if (devtools && !hostElement) {
99 destroy()
100 }
101 })
102 })
103​
104 destroyRef.onDestroy(destroy)
105​
106 return {
107 destroy,
108 }
109 })