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'
54 * Defaults to 'bottom-right'.
55 * @example
56 * <angular-query-devtools buttonPosition="top-right" />
57 */
58 @Input() buttonPosition?: queryDevtools.DevtoolsButtonPosition
59β
60 /**
61 * The position of the Angular Query devtools panel.
62 * 'top' | 'bottom' | 'left' | 'right'
63 * Defaults to 'bottom'.
64 * @example
65 * <angular-query-devtools position="bottom" />
66 */
67 @Input({ alias: 'position' }) position?: queryDevtools.DevtoolsPosition
68β
69 /**
70 * Custom instance of QueryClient
71 * @example
72 * <angular-query-devtools [client]="queryClient" />
73 */
74 @Input() client?: QueryClient
75β
76 /**
77 * 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.
78 * @example
79 * <angular-query-devtools styleNonce="YourRandomNonceValue" />
80 */
81 @Input() styleNonce?: string
82β
83 /**
84 * Use this so you can define custom errors that can be shown in the devtools.
85 */
86 @Input() errorTypes?: Array<DevToolsErrorType>
87β
88 @ViewChild('ref') ref!: ElementRef
89β
90 #devtools?: TanstackQueryDevtools
91β
92 readonly #isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
93β
94 readonly #injectedClient: QueryClient | null = this.#isBrowser
95 ? injectQueryClient({
96 optional: true,
97 })
98 : null
99β
100 ngOnChanges(changes: SimpleChanges) {
101 if (!this.#devtools) return
102 if (changes['client']) {
103 this.#devtools.setClient(this.#getAppliedQueryClient())
104 }
105 if (changes['buttonPosition'] && this.buttonPosition !== undefined) {
106 this.#devtools.setButtonPosition(this.buttonPosition)
107 }
108 if (changes['position'] && this.position !== undefined) {
109 this.#devtools.setPosition(this.position)
110 }
111 if (changes['initialIsOpen'] && this.initialIsOpen !== undefined) {
112 this.#devtools.setInitialIsOpen(this.initialIsOpen)
113 }
114 if (changes['errorTypes'] && this.errorTypes !== undefined) {
115 this.#devtools.setErrorTypes(this.errorTypes)
116 }
117 }
118β
119 ngAfterViewInit() {
120 if (!this.#isBrowser) return
121 const devtools = new queryDevtools.TanstackQueryDevtools({
122 client: this.#getAppliedQueryClient(),
123 queryFlavor: 'Angular Query',
124 version: '5',
125 onlineManager,
126 buttonPosition: this.buttonPosition,
127 position: this.position,
128 initialIsOpen: this.initialIsOpen,
129 errorTypes: this.errorTypes,
130 styleNonce: this.styleNonce,
131 })
132 devtools.mount(this.ref.nativeElement)
133 this.#devtools = devtools
134 }
135β
136 ngOnDestroy() {
137 this.#devtools?.unmount()
138 }
139β
140 #getAppliedQueryClient() {
141 const client = this.client ?? this.#injectedClient
142 if (!client) {
143 throw new Error(
144 'You must either provide a client via `provideAngularQuery` ' +
145 'or pass it to the `client` attribute of `<angular-query-devtools>`.',
146 )
147 }
148 return client
149 }
150}