import { Component, DestroyRef, inject, model } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { MatButtonToggleModule } from '@angular/material/button-toggle'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { ClusterChoice, ClusterMode } from '../../api/tea-eeg.types'; export const MODO_ETIQUETA: Record = { auto: 'Auto', local: 'Local', remote: 'Remoto', sync: 'Sync', }; /** Where the TEA·EEG work runs. Remote asks for the scheduler address; the address is kept while * another mode is selected so switching back doesn't lose it. */ @Component({ selector: 'app-cluster-selector', imports: [MatButtonToggleModule, MatFormFieldModule, MatInputModule, ReactiveFormsModule], templateUrl: './cluster-selector.html', styleUrl: './cluster-selector.scss', }) export class ClusterSelector { private readonly destroyRef = inject(DestroyRef); /** The picked mode and, for 'remote', the scheduler address. */ readonly choice = model({ mode: 'auto' }); readonly modos = (Object.entries(MODO_ETIQUETA) as [ClusterMode, string][]); readonly scheduler = new FormControl('', { nonNullable: true }); constructor() { this.scheduler.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((value) => this.applyScheduler(value)); } setMode(mode: ClusterMode): void { this.choice.set(mode === 'remote' ? { mode, scheduler: this.scheduler.value.trim() || undefined } : { mode }); } private applyScheduler(value: string): void { if (this.choice().mode !== 'remote') return; this.choice.set({ mode: 'remote', scheduler: value.trim() || undefined }); } }