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

angular-query-devtools.component.ts

159 lines4.5 KB

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