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

inject-devtools-panel.ts

126 lines3.6 KB

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