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