Open History to see the last commit
1import { TanstackQueryDevtools } from '@tanstack/query-devtools'
2import {
3 ChangeDetectionStrategy,
4 Component,
5 ElementRef,
6 Input,
7 ViewChild,
8 booleanAttribute,
9 inject,
10} from '@angular/core'
11import {
12 QUERY_CLIENT,
13 onlineManager,
14} from '@tanstack/angular-query-experimental'
15import type { QueryClient } from '@tanstack/angular-query-experimental'
16import type { AfterViewInit, OnDestroy } from '@angular/core'
17import type {
18 // DevToolsErrorType as DevToolsErrorTypeOriginal,
19 DevtoolsButtonPosition as DevtoolsButtonPositionOriginal,
20 DevtoolsPosition as DevtoolsPositionOriginal,
21} from '@tanstack/query-devtools'
22β
23// Alias types for decorators
24type DevtoolsButtonPosition = DevtoolsButtonPositionOriginal
25type DevtoolsPosition = DevtoolsPositionOriginal
26// type DevToolsErrorType = DevToolsErrorTypeOriginal
27β
28@Component({
29 selector: 'angular-query-devtools',
30 standalone: true,
31 template: `<div class="tsqd-parent-container" #ref></div>`,
32 changeDetection: ChangeDetectionStrategy.OnPush,
33})
34export class AngularQueryDevtools implements AfterViewInit, OnDestroy {
35 readonly #injectedClient = inject<QueryClient>(QUERY_CLIENT, {
36 optional: true,
37 })
38β
39 #clientFromAttribute: QueryClient | null = null
40β
41 #getAppliedQueryClient() {
42 if (!this.#clientFromAttribute && !this.#injectedClient) {
43 throw new Error(
44 `You must either provide a client via 'provideAngularQuery' or pass it to the 'client' attribute of angular-query-devtools.`,
45 )
46 }
47 return this.#clientFromAttribute ?? this.#injectedClient
48 }
49β
50 @ViewChild('ref') ref!: ElementRef
51β
52 #devtools: TanstackQueryDevtools
53β
54 constructor() {
55 this.#devtools = new TanstackQueryDevtools({
56 client: this.#getAppliedQueryClient()!,
57 queryFlavor: 'Angular Query',
58 version: '5',
59 onlineManager,
60 buttonPosition: this.buttonPosition,
61 position: this.position,
62 initialIsOpen: this.initialIsOpen,
63 // errorTypes,
64 })
65 }
66β
67 #initialIsOpen = false
68 /**
69 * Add this attribute if you want the dev tools to default to being open
70 */
71 @Input({ transform: booleanAttribute })
72 set initialIsOpen(value: boolean) {
73 this.#initialIsOpen = value
74 this.#devtools.setInitialIsOpen(value)
75 }
76 get initialIsOpen() {
77 return this.#initialIsOpen
78 }
79β
80 #buttonPosition: DevtoolsButtonPosition = 'bottom-left'
81 /**
82 * The position of the TanStack logo to open and close the devtools panel.
83 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
84 * Defaults to 'bottom-left'.
85 */
86 @Input()
87 set buttonPosition(value: DevtoolsButtonPosition) {
88 this.#buttonPosition = value
89 this.#devtools.setButtonPosition(value)
90 }
91 get buttonPosition() {
92 return this.#buttonPosition
93 }
94β
95 #position: DevtoolsPosition = 'bottom'
96 /**
97 * The position of the Angular Query devtools panel.
98 * 'top' | 'bottom' | 'left' | 'right'
99 * Defaults to 'bottom'.
100 */
101 @Input()
102 set position(value: DevtoolsPosition) {
103 this.#position = value
104 this.#devtools.setPosition(value)
105 }
106 get position() {
107 return this.#position
108 }
109β
110 /**
111 * Custom instance of QueryClient
112 */
113 @Input()
114 set client(client: QueryClient | undefined) {
115 this.#clientFromAttribute = client ?? null
116 this.#devtools.setClient(this.#getAppliedQueryClient()!)
117 }
118β
119 // TODO: needs to tested. When re-adding don't forget to re-add to devtools.md too
120 // #errorTypes: Array<DevToolsErrorType> = []
121 /**
122 * Use this so you can define custom errors that can be shown in the devtools.
123 */
124 // @Input()
125 // set errorTypes(value: Array<DevToolsErrorType>) {
126 // this.#errorTypes = value
127 // this.#devtools?.setErrorTypes(value)
128 // }
129 // get errorTypes(): Array<DevToolsErrorType> {
130 // return this.#errorTypes
131 // }
132β
133 ngAfterViewInit() {
134 this.#devtools.mount(this.ref.nativeElement)
135 }
136β
137 ngOnDestroy() {
138 this.#devtools.unmount()
139 }
140}