feature/app-selector

This commit is contained in:
2026-05-26 16:59:32 -06:00
parent 904fa965c8
commit dbf68986da
34 changed files with 792 additions and 169 deletions

View File

@@ -0,0 +1,70 @@
export type SystemType = 'fixed_asset' | 'inventory';
export const SYSTEM_LABELS: Record<SystemType, { name: string; code: string }> = {
fixed_asset: { name: 'Módulo de Activo Fijo', code: 'SCAF' },
inventory: { name: 'Módulo Control de Inventarios', code: 'SCAII' },
};
const VALID_SYSTEMS = new Set<string>(['fixed_asset', 'inventory']);
class SystemStore {
_activeSystem = $state<SystemType | null>(null);
_allowedSystems = $state<SystemType[]>([]);
_switching = $state(false);
get activeSystem() {
return this._activeSystem;
}
get allowedSystems() {
return this._allowedSystems;
}
get canSwitch() {
return this._allowedSystems.length > 1;
}
get switching() {
return this._switching;
}
get activeLabel() {
return this._activeSystem ? SYSTEM_LABELS[this._activeSystem] : null;
}
initialize(allowedSystems: SystemType[], cookieValue: string | null) {
this._allowedSystems = allowedSystems;
if (cookieValue && VALID_SYSTEMS.has(cookieValue) && allowedSystems.includes(cookieValue as SystemType)) {
this._activeSystem = cookieValue as SystemType;
} else if (allowedSystems.length === 1) {
this._activeSystem = allowedSystems[0];
} else {
this._activeSystem = null;
}
}
async setActiveSystem(system: SystemType): Promise<boolean> {
if (!this._allowedSystems.includes(system) || this._switching) return false;
this._switching = true;
try {
const res = await fetch('/api-sveltekit/system/set-active', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ system }),
credentials: 'include',
});
if (res.ok) {
this._activeSystem = system;
return true;
}
return false;
} catch {
return false;
} finally {
this._switching = false;
}
}
clear() {
this._activeSystem = null;
this._allowedSystems = [];
}
}
export const systemStore = new SystemStore();