feat(ui): incorporar filtros globales por dominio UX-06E.D.2
This commit is contained in:
@@ -12,28 +12,52 @@ export interface FilterState {
|
||||
editors: Partial<Record<TipoPeriodo, PeriodEditor>>;
|
||||
applied?: { tipo: TipoPeriodo; selection: AppliedPeriods };
|
||||
}
|
||||
export interface GlobalApplied { tipo: TipoPeriodo; selection: AppliedPeriods; revision: number; }
|
||||
export interface GlobalFilterState extends FilterState { applied: GlobalApplied; }
|
||||
export function newEditor(tipo: TipoPeriodo, first: TipoPeriodo): PeriodEditor {
|
||||
return { added: [], draft: tipo === first ? { mode: 'todos' } : { mode: 'empty' } };
|
||||
return { added: [], draft: tipo === 'semestre' && tipo === first ? { mode: 'todos' } : { mode: 'empty' } };
|
||||
}
|
||||
export function validDraft(draft: PeriodDraft, tipo: TipoPeriodo, types: TipoPeriodo[]): draft is AppliedPeriods {
|
||||
if (!types.includes(tipo) || draft.mode === 'empty') return false;
|
||||
if (draft.mode === 'todos') return tipo === types[0];
|
||||
if (draft.mode === 'todos') return tipo === 'semestre' && tipo === types[0];
|
||||
const parsed = parsePeriodos(draft.periodos.join(','), tipo);
|
||||
return parsed !== null && parsed.length === draft.periodos.length
|
||||
&& parsed.every((p, i) => p === draft.periodos[i]);
|
||||
}
|
||||
export function describePeriods(tipo: TipoPeriodo, draft: PeriodDraft): string {
|
||||
return draft.mode === 'todos' ? 'Todos los ' + (tipo === 'mes' ? 'meses' : 'semestres') + ' con reporte'
|
||||
return draft.mode === 'todos' ? 'Todos con reporte (semestres)'
|
||||
: draft.mode === 'empty' ? 'Sin periodos seleccionados' : draft.periodos.join(', ');
|
||||
}
|
||||
export function selectionKey(applied: Pick<GlobalApplied, 'tipo' | 'selection'>): string {
|
||||
return JSON.stringify([applied.tipo, applied.selection.mode,
|
||||
applied.selection.mode === 'explicit' ? [...applied.selection.periodos].sort() : []]);
|
||||
}
|
||||
export function pending(state: FilterState): boolean {
|
||||
const applied = state.applied;
|
||||
const draft = state.editors[state.tipo]!.draft;
|
||||
if (!applied || applied.tipo !== state.tipo || applied.selection.mode !== draft.mode) return true;
|
||||
if (draft.mode !== 'explicit' || applied.selection.mode !== 'explicit') return false;
|
||||
const periodos = applied.selection.periodos;
|
||||
return draft.periodos.length !== periodos.length
|
||||
|| draft.periodos.some(p => !periodos.includes(p));
|
||||
const draft = state.editors[state.tipo]?.draft;
|
||||
return !state.applied || !draft || draft.mode === 'empty'
|
||||
|| selectionKey(state.applied) !== selectionKey({ tipo: state.tipo, selection: draft });
|
||||
}
|
||||
export function domainTypes(items: readonly Indicador[]): TipoPeriodo[] {
|
||||
return (['semestre', 'mes'] as const).filter(tipo => items.some(item => item.tipos_periodo.includes(tipo)));
|
||||
}
|
||||
export type Compatibility = { compatible: true } | { compatible: false; reason: string };
|
||||
// Published capabilities only; this adapter never changes a selection or probes HTTP.
|
||||
// Future consumers may use the same capability list without defining a KPI contract here.
|
||||
export function filterCompatibility(applied: GlobalApplied | undefined, types: readonly TipoPeriodo[]): Compatibility {
|
||||
if (!types.length) return { compatible: false, reason: 'El catálogo no declara capacidades temporales suficientes para consultar.' };
|
||||
if (!applied) return { compatible: false, reason: 'No hay un filtro general aplicado.' };
|
||||
if (!types.includes(applied.tipo)) return { compatible: false, reason: 'Esta figura no admite la granularidad ' + applied.tipo + ' del filtro general.' };
|
||||
const mode = applied.selection.mode;
|
||||
if (!validDraft(applied.selection, applied.tipo, [...types])) return { compatible: false, reason: mode === 'todos'
|
||||
? 'Todos con reporte requiere semestre como primera granularidad publicada. Para meses elige periodos explícitos.'
|
||||
: 'Los periodos del filtro general no son válidos para esta granularidad.' };
|
||||
return { compatible: true };
|
||||
}
|
||||
export function applyGlobal(state: GlobalFilterState, types: TipoPeriodo[]): boolean {
|
||||
const draft = state.editors[state.tipo]?.draft;
|
||||
if (!draft || !validDraft(draft, state.tipo, types) || !pending(state)) return false;
|
||||
state.applied = { tipo: state.tipo, selection: structuredClone(draft), revision: state.applied.revision + 1 };
|
||||
return true;
|
||||
}
|
||||
export type CardActivation = 'pending' | 'auto-open' | 'manual-open' | 'manual-closed';
|
||||
|
||||
@@ -41,7 +65,7 @@ export type CardActivation = 'pending' | 'auto-open' | 'manual-open' | 'manual-c
|
||||
export class IndicadoresMemory {
|
||||
private readonly themes = new Map<Dominio, Tema>();
|
||||
private readonly expansions = new Map<string, CardActivation>();
|
||||
private readonly filters = new Map<string, FilterState>();
|
||||
private readonly filters = new Map<Dominio, GlobalFilterState>();
|
||||
theme(domain: Dominio): Tema { return this.themes.get(domain) ?? 'claro'; }
|
||||
rememberTheme(domain: Dominio, tema: Tema) { this.themes.set(domain, tema); }
|
||||
activation(domain: Dominio, indicador: string): CardActivation {
|
||||
@@ -50,28 +74,18 @@ export class IndicadoresMemory {
|
||||
rememberActivation(domain: Dominio, indicador: string, state: CardActivation) {
|
||||
this.expansions.set(JSON.stringify([domain, indicador]), state);
|
||||
}
|
||||
private key(domain: Dominio, indicador: string, figura: string) {
|
||||
return JSON.stringify([domain, indicador, figura]);
|
||||
saveGlobal(domain: Dominio, state: GlobalFilterState) {
|
||||
this.filters.set(domain, structuredClone(state));
|
||||
}
|
||||
save(domain: Dominio, indicador: string, figura: string, state: FilterState) {
|
||||
this.filters.set(this.key(domain, indicador, figura), structuredClone(state));
|
||||
}
|
||||
restore(domain: Dominio, indicador: Indicador, figura: string): FilterState {
|
||||
const stored = this.filters.get(this.key(domain, indicador.id, figura));
|
||||
const first = indicador.tipos_periodo[0];
|
||||
const state: FilterState = stored ? structuredClone(stored) : { tipo: first, editors: {} };
|
||||
if (!indicador.tipos_periodo.includes(state.tipo)) state.tipo = first;
|
||||
for (const tipo of ['semestre', 'mes'] as const) {
|
||||
if (!indicador.tipos_periodo.includes(tipo)) { delete state.editors[tipo]; continue; }
|
||||
const entry = state.editors[tipo] ?? newEditor(tipo, first);
|
||||
entry.added = [...new Set(entry.added.filter(p => parsePeriodos(p, tipo)?.[0] === p))];
|
||||
if (entry.draft.mode !== 'empty' && !validDraft(entry.draft, tipo, indicador.tipos_periodo)) entry.draft = { mode: 'empty' };
|
||||
if (entry.draft.mode === 'explicit' && entry.draft.periodos.some(p => !entry.added.includes(p))) entry.draft = { mode: 'empty' };
|
||||
state.editors[tipo] = entry;
|
||||
}
|
||||
if (state.applied && !validDraft(state.applied.selection, state.applied.tipo, indicador.tipos_periodo)) delete state.applied;
|
||||
// Only a new identity receives an initial application. A restored pending draft stays pending.
|
||||
if (!stored) state.applied = { tipo: first, selection: { mode: 'todos' } };
|
||||
restoreGlobal(domain: Dominio, types: TipoPeriodo[]): GlobalFilterState {
|
||||
const stored = this.filters.get(domain);
|
||||
const state: GlobalFilterState = stored ? structuredClone(stored) : {
|
||||
tipo: 'semestre', editors: { semestre: newEditor('semestre', 'semestre') },
|
||||
applied: { tipo: 'semestre', selection: { mode: 'todos' }, revision: 0 },
|
||||
};
|
||||
for (const tipo of types) state.editors[tipo] ??= newEditor(tipo, 'semestre');
|
||||
// Preserve an application whose capabilities changed: consumers explain incompatibility,
|
||||
// rather than silently replacing or trimming the user's temporal selection.
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user