Open History to see the last commit
1import { isPlatformBrowser } from '@angular/common'
2import * as queryDevtools from '@tanstack/query-devtools'
3import {
4 injectQueryClient,
5 onlineManager,
6} from '@tanstack/angular-query-experimental'
7import {
8 ChangeDetectionStrategy,
9 Component,
10 ElementRef,
11 Input,
12 PLATFORM_ID,
13 ViewChild,
14 booleanAttribute,
15 inject,
16} from '@angular/core'
17import { QueryClient } from '@tanstack/angular-query-experimental'
18import type {
19 AfterViewInit,
20 OnChanges,
21 OnDestroy,
22 SimpleChanges,
23} from '@angular/core'
24import type {
25 DevToolsErrorType,
26 TanstackQueryDevtools,
27} from '@tanstack/query-devtools'
28β
29@Component({
30 selector: 'angular-query-devtools',
31 standalone: true,
32 template: `<div class="tsqd-parent-container" #ref></div>`,
33 changeDetection: ChangeDetectionStrategy.OnPush,
34 host: { ngSkipHydration: 'true' },
35})
36export class AngularQueryDevtools
37 implements AfterViewInit, OnChanges, OnDestroy
38{
39 /*
40 * It is intentional that there are no default values on inputs.
41 * Core devtools will set defaults when values are undefined.
42 * */
43β
44 /**
45 * Add this attribute if you want the dev tools to default to being open
46 * @example
47 * <angular-query-devtools initialIsOpen />
48 */
49 @Input({ transform: booleanAttribute }) initialIsOpen?: boolean
50β
51 /**
52 * The position of the TanStack logo to open and close the devtools panel.
53 * `top-left` | `top-right` | `bottom-left` | `bottom-right` | `relative`
54 * Defaults to `bottom-right`.
55 * If `relative`, the button is placed in the location that you render the devtools.
56 * @example
57 * <angular-query-devtools buttonPosition="top-right" />
58 */
59 @Input() buttonPosition?: queryDevtools.DevtoolsButtonPosition
60β
61 /**
62 * The position of the Angular Query devtools panel.
63 * `top` | `bottom` | `left` | `right`
64 * Defaults to `bottom`.
65 * @example
66 * <angular-query-devtools position="bottom" />
67 */
68 @Input() position?: queryDevtools.DevtoolsPosition
69β
70 /**
71 * Custom instance of QueryClient
72 * @example
73 * <angular-query-devtools [client]="queryClient" />
74 */
75 @Input() client?: QueryClient
76β
77 /**
78 * 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.
79 * @example
80 * <angular-query-devtools styleNonce="YourRandomNonceValue" />
81 */
82 @Input() styleNonce?: string
83β
84 /**
85 * Use this so you can attach the devtool's styles to specific element in the DOM.
86 */
87 @Input() shadowDOMTarget?: ShadowRoot
88β
89 /**
90 * Use this so you can define custom errors that can be shown in the devtools.
91 */
92 @Input() errorTypes?: Array<DevToolsErrorType>
93β
94 @ViewChild('ref') ref!: ElementRef
95β
96 #devtools?: TanstackQueryDevtools
97β
98 readonly #isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
99β
100 readonly #injectedClient: QueryClient | null = this.#isBrowser
101 ? injectQueryClient({
102 optional: true,
103 })
104 : null
105β
106 ngOnChanges(changes: SimpleChanges) {
107 if (!this.#devtools) return
108 if (changes['client']) {
109 this.#devtools.setClient(this.#getAppliedQueryClient())
110 }
111 if (changes['buttonPosition'] && this.buttonPosition !== undefined) {
112 this.#devtools.setButtonPosition(this.buttonPosition)
113 }
114 if (changes['position'] && this.position !== undefined) {
115 this.#devtools.setPosition(this.position)
116 }
117 if (changes['initialIsOpen'] && this.initialIsOpen !== undefined) {
118 this.#devtools.setInitialIsOpen(this.initialIsOpen)
119 }
120 if (changes['errorTypes'] && this.errorTypes !== undefined) {
121 this.#devtools.setErrorTypes(this.errorTypes)
122 }
123 }
124β
125 ngAfterViewInit() {
126 if (!this.#isBrowser) return
127 const devtools = new queryDevtools.TanstackQueryDevtools({
128 client: this.#getAppliedQueryClient(),
129 queryFlavor: 'Angular Query',
130 version: '5',
131 onlineManager,
132 buttonPosition: this.buttonPosition,
133 position: this.position,
134 initialIsOpen: this.initialIsOpen,
135 errorTypes: this.errorTypes,
136 styleNonce: this.styleNonce,
137 shadowDOMTarget: this.shadowDOMTarget,
138 })
139 devtools.mount(this.ref.nativeElement)
140 this.#devtools = devtools
141 }
142β
143 ngOnDestroy() {
144 this.#devtools?.unmount()
145 }
146β
147 #getAppliedQueryClient() {
148 const client = this.client ?? this.#injectedClient
149 if (!client) {
150 throw new Error(
151 'You must either provide a client via `provideAngularQuery` ' +
152 'or pass it to the `client` attribute of `<angular-query-devtools>`.',
153 )
154 }
155 return client
156 }
157}