diff --git a/src/components/Equipo_Computo/Pregunta2.tsx b/src/components/Equipo_Computo/Pregunta2.tsx index 5aa3b5f..e3e0680 100644 --- a/src/components/Equipo_Computo/Pregunta2.tsx +++ b/src/components/Equipo_Computo/Pregunta2.tsx @@ -32,61 +32,74 @@ interface RawOsEntry { total: string; } -function transformPlatform(raw: RawOsEntry[]): OsEntry[] { - const order: Record = { - "Windows 11": 1, - "Windows 10": 2, - "Windows 7/8": 3, - "Windows XP/Vista": 4, - Linux: 5, +const PLATFORM_OS_LIST: Record = { + "pc-desktop": [ + "Windows 11", + "Windows 10", + "Windows 7/8", + "Windows XP/Vista", + "Linux", + ], - "MAC OS (SEQUOIA)": 6, - "Mac OS (13 - Ventura, 14 - Sonoma)": 7, - "Mac OS X (Mojave, Catalina, 11 - Big Sur, 12 - Monterrey)": 8, - "Mac OS X (Snow Leopard, Lion, Mountain Lion, Mavericks)": 9, - "Mac OS X (Yosemite, El Capitan, Sierra, High Sierra)": 10, + "apple-desktop": [ + "MAC OS (SEQUOIA)", + "Mac OS (13 - Ventura, 14 - Sonoma)", + "Mac OS X (Mojave, Catalina, 11 - Big Sur, 12 - Monterrey)", + "Mac OS X (Snow Leopard, Lion, Mountain Lion, Mavericks)", + "Mac OS X (Yosemite, El Capitan, Sierra, High Sierra)", + ], - "Chrome OS": 11, + "pc-laptop": [ + "Windows 11", + "Windows 10", + "Windows 7/8", + "Windows XP/Vista", + "Chrome OS", + ], - "Linux (CentOS, Fedora, Ubuntu, Red Hat Enterprise, entre otros)": 12, - "UNIX (AIX, MAC OS SERVER, SOLARIS, ENTRE OTROS)": 13, + "apple-laptop": [ + "Mac OS (13 - Ventura, 14 - Sonoma)", + "Mac OS X (Mojave, Catalina, 11 - Big Sur, 12 - Monterrey)", + "Mac OS X (Snow Leopard, Lion, Mountain Lion, Mavericks)", + "Mac OS X (Yosemite, El Capitan, Sierra, High Sierra)", + ], - "Windows Server 2022/2023": 14, - "Windows Server 2016/2019": 15, - "Windows Server 2008/2012": 16, - "Windows Server 2000/2003": 17, - }; + servers: [ + "Linux (CentOS, Fedora, Ubuntu, Red Hat Enterprise, entre otros)", + "UNIX (AIX, MAC OS SERVER, SOLARIS, ENTRE OTROS)", + "Windows Server 2022/2023", + "Windows Server 2016/2019", + "Windows Server 2008/2012", + "Windows Server 2000/2003", + ], +}; - function cleanName(rawName: string): string { - return rawName +function transformPlatform( + raw: RawOsEntry[], + platform: PlatformKey +): OsEntry[] { + const fixedList = PLATFORM_OS_LIST[platform]; + + const clean = (str: string) => + str .normalize("NFKD") .replace(/\u00A0/g, " ") - .replace(/\s+/g, " ") .trim(); - } - // mapeamos y limpiamos antes del sort - const rows: OsEntry[] = raw.map((item) => { - const cleaned = cleanName(item.sistema_operativo); - return { - os: cleaned, - count: Number(item.total), - }; + const mapRaw: Record = {}; + raw.forEach((item) => { + const name = clean(item.sistema_operativo); + mapRaw[name] = Number(item.total); }); - rows.sort((a, b) => { - const orderA = (order as Record)[a.os] ?? 999; - const orderB = (order as Record)[b.os] ?? 999; - return orderA - orderB; - }); + const rows: OsEntry[] = fixedList.map((os) => ({ + os, + count: mapRaw[clean(os)] ?? 0, + })); - const total = rows.reduce((acc, r) => acc + r.count, 0); + const total = rows.reduce((a, r) => a + r.count, 0); - rows.push({ - os: "Total", - count: total, - isTotal: true, - }); + rows.push({ os: "Total", count: total, isTotal: true }); return rows; } @@ -94,7 +107,6 @@ function transformPlatform(raw: RawOsEntry[]): OsEntry[] { export default function Pregunta2() { const [activeTab, setActiveTab] = useState("pc-desktop"); const [data, setData] = useState(null); - const [loading, setLoading] = useState(true); const cargarDatos = async (baja: boolean) => { const token = Cookies.get("token"); @@ -111,65 +123,22 @@ export default function Pregunta2() { const json = res.data; const formatted: PlatformData = { - "pc-desktop": transformPlatform(json[0]), - "apple-desktop": transformPlatform(json[1]), - "pc-laptop": transformPlatform(json[2]), - "apple-laptop": transformPlatform(json[3]), - servers: transformPlatform(json[4]), + "pc-desktop": transformPlatform(json[0], "pc-desktop"), + "apple-desktop": transformPlatform(json[1], "apple-desktop"), + "pc-laptop": transformPlatform(json[2], "pc-laptop"), + "apple-laptop": transformPlatform(json[3], "apple-laptop"), + servers: transformPlatform(json[4], "servers"), }; setData(formatted); }) .catch((err) => console.error("Error cargando datos", err)) - .finally(() => { - setLoading(false); - }); }; useEffect(() => { cargarDatos(false); }, []); - if (loading) - return ( -
-
- Censo de equipos de cómputo - Sitema Operativo -
- -
- Número de sistemas Operativos por cada categoría y perfil de usuario. - { - cargarDatos(estado); - }} - /> -
-
Cargando datos...
-
- ); - - if (!data) - return ( -
-
- Censo de equipos de cómputo - Sitema Operativo -
- -
- Número de sistemas Operativos por cada categoría y perfil de usuario. - { - cargarDatos(estado); - }} - /> -
-
Error al cargar los datos.
-
- ); - - const currentData = data[activeTab]; - return (
@@ -203,7 +172,7 @@ export default function Pregunta2() { {/* Tabla */}
- {currentData.map((item, index) => ( + {data && data[activeTab].map((item, index) => (