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

inject-devtools-panel.ts

115 lines3.3 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 hideDisabledQueries,
65 theme,
66 onClose,
67 hostElement,
68 } = queryOptions()
69​
70 untracked(() => {
71 if (!client) throw new Error('No QueryClient found')
72 if (!devtools && hostElement) {
73 import('@tanstack/query-devtools')
74 .then((queryDevtools) => {
75 devtools = new queryDevtools.TanstackQueryDevtoolsPanel({
76 client,
77 queryFlavor: 'Angular Query',
78 version: '5',
79 buttonPosition: 'bottom-left',
80 position: 'bottom',
81 initialIsOpen: true,
82 errorTypes,
83 styleNonce,
84 shadowDOMTarget,
85 hideDisabledQueries,
86 theme,
87 onClose,
88 onlineManager,
89 })
90 devtools.mount(hostElement.nativeElement)
91 })
92 .catch((error) => {
93 console.error(
94 'Install @tanstack/query-devtools or reinstall without --omit=optional.',
95 error,
96 )
97 })
98 } else if (devtools && hostElement) {
99 devtools.setClient(client)
100 devtools.setErrorTypes(errorTypes)
101 devtools.setTheme(theme)
102 onClose && devtools.setOnClose(onClose)
103 } else if (devtools && !hostElement) {
104 destroy()
105 }
106 })
107 })
108​
109 destroyRef.onDestroy(destroy)
110​
111 return {
112 destroy,
113 }
114 })