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

inject-devtools-panel.ts

150 lines4.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 { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
13import {
14 QueryClient,
15 onlineManager,
16} from '@tanstack/angular-query-experimental'
17import { isPlatformBrowser } from '@angular/common'
18import type { ElementRef } from '@angular/core'
19import type { DevtoolsErrorType } from '@tanstack/query-devtools'
20​
21export interface InjectDevtoolsPanelOptions {
22 /**
23 * The `Injector` in which to create the devtools panel.
24 *
25 * If this is not provided, the current injection context will be used instead (via `inject`).
26 */
27 injector?: Injector
29​
30/**
31 * Inject a TanStack Query devtools panel and render it in the DOM.
32 *
33 * Devtools panel allows programmatic control over the devtools, for example if you want to render
34 * the devtools as part of your own devtools.
35 *
36 * Consider `withDevtools` instead if you don't need this.
37 * @param injectDevtoolsPanelFn - A function that returns devtools panel options.
38 * @param options - Additional configuration
39 * @returns DevtoolsPanelRef
40 * @see https://tanstack.com/query/v5/docs/framework/angular/devtools
41 */
42export function injectDevtoolsPanel(
43 injectDevtoolsPanelFn: () => DevtoolsPanelOptions,
44 options?: InjectDevtoolsPanelOptions,
45): DevtoolsPanelRef {
46 !options?.injector && assertInInjectionContext(injectDevtoolsPanel)
47 const currentInjector = options?.injector ?? inject(Injector)
48​
49 return runInInjectionContext(currentInjector, () => {
50 const destroyRef = inject(DestroyRef)
51 const isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
52 const injectedClient = inject(QueryClient, { optional: true })
53​
54 const queryOptions = computed(injectDevtoolsPanelFn)
55 let devtools: TanstackQueryDevtoolsPanel | null = null
56​
57 const destroy = () => {
58 devtools?.unmount()
59 devtools = null
60 }
61​
62 if (!isBrowser)
63 return {
64 destroy,
65 }
66​
67 effect(() => {
68 const {
69 client = injectedClient,
70 errorTypes = [],
71 styleNonce,
72 shadowDOMTarget,
73 onClose,
74 hostElement,
75 } = queryOptions()
76​
77 untracked(() => {
78 if (!client) throw new Error('No QueryClient found')
79 if (!devtools && hostElement) {
80 devtools = new TanstackQueryDevtoolsPanel({
81 client,
82 queryFlavor: 'Angular Query',
83 version: '5',
84 buttonPosition: 'bottom-left',
85 position: 'bottom',
86 initialIsOpen: true,
87 errorTypes,
88 styleNonce,
89 shadowDOMTarget,
90 onClose,
91 onlineManager,
92 })
93 devtools.mount(hostElement.nativeElement)
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 })
111​
112/**
113 * A devtools panel, which can be manually destroyed.
114 */
115export interface DevtoolsPanelRef {
116 /**
117 * Destroy the panel, removing it from the DOM and stops listening to signal changes.
118 */
119 destroy: () => void
121​
122export interface DevtoolsPanelOptions {
123 /**
124 * Custom instance of QueryClient
125 */
126 client?: QueryClient
127 /**
128 * Use this so you can define custom errors that can be shown in the devtools.
129 */
130 errorTypes?: Array<DevtoolsErrorType>
131 /**
132 * Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
133 */
134 styleNonce?: string
135 /**
136 * Use this so you can attach the devtool's styles to specific element in the DOM.
137 */
138 shadowDOMTarget?: ShadowRoot
139​
140 /**
141 * Callback function that is called when the devtools panel is closed
142 */
143 onClose?: () => unknown
144​
145 /**
146 * Element where to render the devtools panel. When set to undefined or null, the devtools panel will not be created, or destroyed if existing.
147 * If changed from undefined to a ElementRef, the devtools panel will be created.
148 */
149 hostElement?: ElementRef