-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdevtools.ts
More file actions
150 lines (132 loc) · 3.9 KB
/
devtools.ts
File metadata and controls
150 lines (132 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
ApplicationRef,
Component,
DestroyRef,
ElementRef,
EnvironmentInjector,
afterNextRender,
createComponent,
effect,
inject,
input,
} from '@angular/core'
import { PLUGIN_CONTAINER_ID, TanStackDevtoolsCore } from '@tanstack/devtools'
import type { ComponentRef, Type } from '@angular/core'
import type { TanStackDevtoolsPlugin } from '@tanstack/devtools'
import type {
TanStackDevtoolsAngularInit,
TanStackDevtoolsAngularPlugin,
} from './types'
@Component({
selector: 'tanstack-devtools',
standalone: true,
template: '<div #devtoolsHost></div>',
})
export class TanStackDevtoolsComponent {
plugins = input<TanStackDevtoolsAngularInit['plugins']>()
config = input<TanStackDevtoolsAngularInit['config']>()
eventBusConfig = input<TanStackDevtoolsAngularInit['eventBusConfig']>()
private hostRef = inject(ElementRef)
private appRef = inject(ApplicationRef)
private injector = inject(EnvironmentInjector)
private destroyRef = inject(DestroyRef)
private componentRefs: Array<ComponentRef<any>> = []
private devtools: TanStackDevtoolsCore | null = null
constructor() {
afterNextRender(() => {
const hostEl = this.hostRef.nativeElement.querySelector('div')
if (!hostEl) return
const pluginsMap = this.getPluginsMap()
this.devtools = new TanStackDevtoolsCore({
config: this.config(),
eventBusConfig: this.eventBusConfig(),
plugins: pluginsMap,
})
this.devtools.mount(hostEl)
})
effect(() => {
const plugins = this.plugins()
const config = this.config()
const eventBusConfig = this.eventBusConfig()
if (this.devtools) {
this.devtools.setConfig({
config,
eventBusConfig,
plugins: plugins?.map((p) => this.convertPlugin(p)) ?? [],
})
}
})
this.destroyRef.onDestroy(() => {
this.destroyAllComponents()
if (this.devtools) {
this.devtools.unmount()
this.devtools = null
}
})
}
private getPluginsMap(): Array<TanStackDevtoolsPlugin> {
const plugins = this.plugins()
if (!plugins) return []
return plugins.map((plugin) => this.convertPlugin(plugin))
}
private convertPlugin(
plugin: TanStackDevtoolsAngularPlugin,
): TanStackDevtoolsPlugin {
return {
id: plugin.id,
defaultOpen: plugin.defaultOpen,
name:
typeof plugin.name === 'string'
? plugin.name
: (e, theme) => {
this.renderComponent(plugin.name as Type<any>, e, {
theme,
...(plugin.inputs ?? {}),
})
},
render: (e, theme) => {
this.renderComponent(plugin.component, e, {
theme,
...(plugin.inputs ?? {}),
})
},
destroy: (pluginId) => {
this.destroyComponentsInContainer(`${PLUGIN_CONTAINER_ID}-${pluginId}`)
},
}
}
private renderComponent(
component: Type<any>,
container: HTMLElement,
inputs: Record<string, unknown>,
) {
const componentRef = createComponent(component, {
environmentInjector: this.injector,
hostElement: container,
})
for (const [key, value] of Object.entries(inputs)) {
componentRef.setInput(key, value)
}
this.appRef.attachView(componentRef.hostView)
componentRef.changeDetectorRef.detectChanges()
this.componentRefs.push(componentRef)
}
private destroyComponentsInContainer(containerId: string) {
this.componentRefs = this.componentRefs.filter((ref) => {
const el = ref.location.nativeElement as HTMLElement
if (el.parentElement?.id === containerId) {
this.appRef.detachView(ref.hostView)
ref.destroy()
return false
}
return true
})
}
private destroyAllComponents() {
for (const ref of this.componentRefs) {
this.appRef.detachView(ref.hostView)
ref.destroy()
}
this.componentRefs = []
}
}