import { Component, computed, inject, input, signal } from '@angular/core'; import { toObservable, toSignal } from '@angular/core/rxjs-interop'; import { catchError, map, of, startWith, switchMap } from 'rxjs'; import { PlotlyChart } from '../../../../shared/plotly/plotly-chart'; import { CamaCurvasService, EscenarioConFactores, SeleccionRechazada } from './cama-curvas.service'; import { CamaFactoresRiesgo } from './cama-factores-riesgo.component'; import { figuraSupervivencia } from './cama-supervivencia-figura'; type Lectura = { tipo: 'cargando' } | { tipo: 'rechazada'; motivo: string } | { tipo: 'error' } | { tipo: 'lista'; datos: EscenarioConFactores }; /** * «Análisis de supervivencia» and its «Factores de riesgo» of the clinical scenario the patient's own data place her in; when they place * her in several, a menu of those. The scenario decides the curve and the label at once, computed from the data ingested so far with the * model in production. */ @Component({ selector: 'app-cama-analisis-supervivencia', imports: [PlotlyChart, CamaFactoresRiesgo], templateUrl: './cama-analisis-supervivencia.component.html', styleUrl: './cama-analisis.component.scss', }) export class CamaAnalisisSupervivencia { readonly pacId = input.required(); private readonly service = inject(CamaCurvasService); /** The menu; `undefined` while it loads, `null` when there is no model in production. */ readonly menu = toSignal(toObservable(this.pacId).pipe(switchMap(id => this.service.escenarios(id)))); private readonly elegido = signal(null); /** Her scenarios; every scenario only if the server could not place her. */ readonly opciones = computed(() => { const m = this.menu(); return m ? (m.deLaPaciente.length ? m.deLaPaciente : m.escenarios) : []; }); readonly escenario = computed(() => this.elegido() ?? this.menu()?.predeterminado ?? null); readonly lectura = toSignal(toObservable(this.escenario).pipe(switchMap(nombre => nombre === null ? of(null) : this.service.consulta(nombre).pipe( map((datos): Lectura => ({ tipo: 'lista', datos })), catchError(error => of(error instanceof SeleccionRechazada ? { tipo: 'rechazada', motivo: error.message } : { tipo: 'error' })), startWith({ tipo: 'cargando' })))), { initialValue: null as Lectura | null }); readonly figura = computed(() => { const l = this.lectura(); return l?.tipo === 'lista' ? figuraSupervivencia(l.datos.respuesta.curvas) : null; }); readonly factores = computed(() => { const l = this.lectura(); return l?.tipo === 'lista' ? l.datos.factores : null; }); elegir(nombre: string): void { this.elegido.set(nombre); } }