Files
WebApp/src/app/gobernanza/tea/preview.ts
T
alberto 71777537e1 F1h: Gobernanza without Vista previa and Subir archivos, button spacing
Keep preview state in Modelos and reset the toggle on domain changes. Hide the form while collapsed so the current selections, run monitor and result dialog survive toggling. Keep the existing evaluation and PDF handlers.

Use one wrapping button-row class with space-3 gaps and block margins. Inside table cells use the existing cell padding between rows. Apply the same class to the indicators export and keep the existing domain navigation bottom margin.

Delete the training upload UI and hand-edit the client and OpenAPI. Preserve the generator format and update the Modelos component spec and Gobernanza page case to cover both toggle states.
2026-12-01 13:49:28 -06:00

243 lines
8.1 KiB
TypeScript

import { KeyValuePipe } from '@angular/common';
import { Component, inject, input, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { Api } from '../../api/api';
import { previewStatus } from '../../api/fn/preview/preview-status';
import { startPreview } from '../../api/fn/preview/start-preview';
import type { PreviewResult } from '../../api/models/preview-result';
import type { Run } from '../../api/models/run';
import { RunMonitor, errorText } from '../run-monitor';
import { downloadPreviewPdf } from './preview-pdf';
@Component({
selector: 'app-eeg-preview',
imports: [
FormsModule,
KeyValuePipe,
MatButtonModule,
MatFormFieldModule,
MatInputModule,
RunMonitor,
],
template: `
<form
class="button-row"
[hidden]="!active()"
(ngSubmit)="start(edf.files?.[0], ados.files?.[0], birth.value)"
>
<input
#edf
type="file"
accept=".edf"
required
hidden
aria-label="EDF"
(change)="edfFiles.set(edf.files)"
/>
<button mat-stroked-button type="button" (click)="edf.click()">EDF</button>
@for (file of edfFiles(); track $index) {
<span>{{ file.name }}</span>
}
<input
#ados
type="file"
accept=".pdf"
hidden
aria-label="ADOS-2 (PDF)"
(change)="adosFiles.set(ados.files)"
/>
<button mat-stroked-button type="button" (click)="ados.click()">ADOS-2 (PDF)</button>
@for (file of adosFiles(); track $index) {
<span>{{ file.name }}</span>
}
<mat-form-field appearance="outline">
<mat-label>Fecha de nacimiento</mat-label>
<input matInput #birth type="date" />
</mat-form-field>
<button mat-stroked-button type="submit" [disabled]="busy() || !edfFiles()?.length">
Vista previa
</button>
</form>
@if (error()) {
<p role="alert">{{ error() }}</p>
}
@if (runId(); as id) {
<app-run-monitor [runId]="id" (finished)="finish($event, dialog)" />
}
<dialog #dialog aria-label="Vista previa del estudio">
@if (result(); as r) {
<h3>Resultado de la vista previa</h3>
<p>
{{ r.eeg.level }} ·
{{ r.eeg.valid === true ? 'Válido' : r.eeg.valid === false ? 'No válido' : '—' }}
</p>
@if (r.eeg.error) {
<p role="alert">{{ r.eeg.error }}</p>
}
<h4>Comprobaciones</h4>
<table class="plain">
<thead>
<tr>
<th>Comprobación</th>
<th>Electrodo</th>
<th>Resultado</th>
<th>Rango</th>
<th>Anormal</th>
</tr>
</thead>
<tbody>
@for (check of r.eeg.checks; track $index) {
<tr>
<td>{{ check['code'] }}</td>
<td>{{ check['electrode'] ?? '—' }}</td>
<td>{{ check['value'] ?? check['text'] ?? '—' }} {{ check['unit'] }}</td>
<td>{{ check['low'] ?? '—' }} / {{ check['high'] ?? '—' }}</td>
<td>
{{ check['abnormal'] === true ? 'Sí' : check['abnormal'] === false ? 'No' : '—' }}
</td>
</tr>
}
</tbody>
</table>
<h4>Métricas</h4>
<table class="plain">
<thead>
<tr>
<th>Métrica</th>
<th>Valor</th>
<th>z</th>
<th>p5 / mediana / p95</th>
<th>Fuera de rango</th>
</tr>
</thead>
<tbody>
@for (metric of r.eeg.metrics; track metric.code) {
<tr>
<th>{{ metric.code }}</th>
<td>{{ metric.value ?? '—' }}</td>
<td>{{ metric.score?.z ?? '—' }}</td>
<td>
{{ metric.score?.p5 ?? '—' }} / {{ metric.score?.median ?? '—' }} /
{{ metric.score?.p95 ?? '—' }}
</td>
<td>{{ metric.score?.outside ?? '—' }}</td>
</tr>
@for (site of metric.electrodes | keyvalue; track site.key) {
<tr>
<td>{{ site.key }}</td>
<td>{{ site.value.value ?? '—' }}</td>
<td>{{ site.value.score?.z ?? '—' }}</td>
<td>
{{ site.value.score?.p5 ?? '—' }} / {{ site.value.score?.median ?? '—' }} /
{{ site.value.score?.p95 ?? '—' }}
</td>
<td>{{ site.value.score?.outside ?? '—' }}</td>
</tr>
}
@for (site of metric.zones | keyvalue; track site.key) {
<tr>
<td>{{ site.key }}</td>
<td>{{ site.value.value ?? '—' }}</td>
<td>{{ site.value.score?.z ?? '—' }}</td>
<td>
{{ site.value.score?.p5 ?? '—' }} / {{ site.value.score?.median ?? '—' }} /
{{ site.value.score?.p95 ?? '—' }}
</td>
<td>{{ site.value.score?.outside ?? '—' }}</td>
</tr>
}
}
</tbody>
</table>
@if (r.ados2; as ados2) {
<h4>ADOS-2</h4>
<p>{{ ados2.classification }} · CSS {{ ados2.css ?? '—' }}</p>
<dl>
@for (item of ados2.items | keyvalue: unsorted; track item.key) {
<dt>{{ ados2.titles[item.key] ?? item.key }}</dt>
<dd>{{ item.value ?? '—' }}</dd>
}
</dl>
}
@if (r.warnings.length) {
<h4>Advertencias</h4>
<ul>
@for (warning of r.warnings; track $index) {
<li>{{ warning }}</li>
}
</ul>
}
}
@if (pdfError()) {
<p role="alert">{{ pdfError() }}</p>
}
<form class="button-row" method="dialog">
@if (result(); as r) {
<button mat-stroked-button type="button" [disabled]="exporting()" (click)="pdf(r)">
Guardar como PDF
</button>
}
<button mat-stroked-button>Cerrar</button>
</form>
</dialog>
`,
})
export class EegPreview {
readonly active = input.required<boolean>();
private readonly api = inject(Api);
protected readonly edfFiles = signal<FileList | null>(null);
protected readonly adosFiles = signal<FileList | null>(null);
protected readonly busy = signal(false);
protected readonly error = signal('');
protected readonly runId = signal('');
protected readonly result = signal<PreviewResult | null>(null);
protected readonly unsorted = () => 0;
protected readonly exporting = signal(false);
protected readonly pdfError = signal('');
protected async pdf(result: PreviewResult): Promise<void> {
this.exporting.set(true);
this.pdfError.set('');
try {
await downloadPreviewPdf(result);
} catch (error) {
this.pdfError.set(errorText(error));
} finally {
this.exporting.set(false);
}
}
protected async start(edf?: File, ados2?: File, birth_date?: string): Promise<void> {
if (!edf) return;
this.busy.set(true);
this.error.set('');
this.result.set(null);
try {
const { run_id } = await this.api.invoke(startPreview, {
body: { edf, ados2, birth_date: birth_date || undefined },
});
this.runId.set(run_id);
} catch (error) {
this.error.set(errorText(error));
this.busy.set(false);
}
}
protected async finish(run: Run, dialog: HTMLDialogElement): Promise<void> {
try {
if (run.state !== 'succeeded') return;
const { result } = await this.api.invoke(previewStatus, { run_id: run.id });
this.result.set(result);
if (result) dialog.showModal();
else this.error.set('La vista previa no devolvió un resultado.');
} catch (error) {
this.error.set(errorText(error));
} finally {
this.busy.set(false);
}
}
}