60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
|
|
"use client";
|
||
|
|
import { useEffect, useRef, useState } from "react";
|
||
|
|
|
||
|
|
// Buscador de oportunidades para las páginas de previsión y tubería (ventas y renovaciones). Filtra las
|
||
|
|
// FILAS de todas las tablas de la página por nombre de oportunidad o de cliente (sin tildes ni mayúsculas).
|
||
|
|
// No recalcula las tarjetas ni los totales: es una ayuda para ubicar una oportunidad, no un filtro de datos.
|
||
|
|
const norm = (s) => String(s || "").normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase();
|
||
|
|
|
||
|
|
export default function BuscadorOportunidades() {
|
||
|
|
const [q, setQ] = useState("");
|
||
|
|
const [vis, setVis] = useState(null); // { mostradas, total } mientras hay búsqueda
|
||
|
|
const qRef = useRef("");
|
||
|
|
qRef.current = q;
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
let temporizador = null;
|
||
|
|
const aplicar = () => {
|
||
|
|
const t = norm(qRef.current).trim();
|
||
|
|
const filas = [...document.querySelectorAll("main table tbody tr, .tw table tbody tr")].filter((tr) => tr.querySelector("a.opp"));
|
||
|
|
let ok = 0;
|
||
|
|
for (const tr of new Set(filas)) {
|
||
|
|
const coincide = !t || norm(tr.textContent).includes(t);
|
||
|
|
tr.hidden = !coincide;
|
||
|
|
if (coincide) ok++;
|
||
|
|
}
|
||
|
|
setVis(t ? { mostradas: ok, total: new Set(filas).size } : null);
|
||
|
|
};
|
||
|
|
aplicar();
|
||
|
|
// Si la página se vuelve a pintar (cambia un filtro), se vuelve a aplicar la búsqueda.
|
||
|
|
const obs = new MutationObserver(() => {
|
||
|
|
clearTimeout(temporizador);
|
||
|
|
temporizador = setTimeout(aplicar, 120);
|
||
|
|
});
|
||
|
|
obs.observe(document.body, { childList: true, subtree: true });
|
||
|
|
return () => {
|
||
|
|
obs.disconnect();
|
||
|
|
clearTimeout(temporizador);
|
||
|
|
document.querySelectorAll("tr[hidden]").forEach((tr) => { tr.hidden = false; });
|
||
|
|
};
|
||
|
|
}, [q]);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="busca-opp">
|
||
|
|
<input
|
||
|
|
type="search"
|
||
|
|
value={q}
|
||
|
|
onChange={(e) => setQ(e.target.value)}
|
||
|
|
placeholder="🔍 Buscar oportunidad o cliente…"
|
||
|
|
aria-label="Buscar oportunidad o cliente"
|
||
|
|
/>
|
||
|
|
{vis && (
|
||
|
|
<span className="busca-n">
|
||
|
|
{vis.mostradas === 0 ? "Sin coincidencias en las tablas" : `${vis.mostradas} de ${vis.total} filas`}
|
||
|
|
{" · "}<button type="button" onClick={() => setQ("")}>Limpiar</button>
|
||
|
|
</span>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|