La carpeta con .git (C:\Mis Aplicaciones\informe-comercial) quedó desactualizada frente a lo que corre hoy en Coolify (deploy directo por Foundry desde esta carpeta, sin paso por git). Este commit fija esta carpeta como fuente de verdad del repo, con Renovaciones, Actividades, Agenda, Admin/Metas y Nuevas Oportunidades que la copia vieja no tenía. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
102 lines
3.9 KiB
JavaScript
102 lines
3.9 KiB
JavaScript
"use client";
|
|
import { useState, useRef, useEffect } from "react";
|
|
import { useRouter, useSearchParams, usePathname } from "next/navigation";
|
|
|
|
// Selector de Propietario con multi-selección — puerto de owner.js/owner-panel (App Informe).
|
|
// Estado en la URL (`owner=email1,email2`), no en un Set en memoria de módulo como el HTML: cada
|
|
// página server-side ya lee esa lista vía ownerFilter() (authz.js). Sin el param `owner` en la URL
|
|
// = "todos" — pero en el dropdown eso se representa con el set COMPLETO marcado (no un set vacío):
|
|
// así un clic sobre un ejecutivo ya marcado lo desmarca a él y deja a los demás como estaban, en
|
|
// vez de invertir la selección entera (bug real encontrado al probar: con "vacío = todos", el
|
|
// primer clic dejaba SOLO a esa persona marcada). No se puede llegar a 0 marcados: desmarcar el
|
|
// último no hace nada, en vez de una pantalla en blanco confusa.
|
|
export default function OwnerControl({ owners, current, locked, lockedLabel }) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const [open, setOpen] = useState(false);
|
|
const ref = useRef(null);
|
|
const currentKey = (current || []).join(",");
|
|
|
|
const effectiveSet = (list) => {
|
|
const arr = list && list.length ? list : owners.map((o) => o.email);
|
|
return new Set(arr);
|
|
};
|
|
|
|
// Estado local vía ref además de useState: dos clics seguidos en el dropdown ocurren en el mismo
|
|
// tick de JS, antes de que React aplique el primer setState — computar `next` desde el ref (no
|
|
// desde `selected`, el valor de render) evita perder el primer clic.
|
|
const [selected, setSelected] = useState(() => effectiveSet(current));
|
|
const selRef = useRef(selected);
|
|
useEffect(() => {
|
|
const next = effectiveSet(current);
|
|
selRef.current = next;
|
|
setSelected(next);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [currentKey]);
|
|
|
|
useEffect(() => {
|
|
function onDocClick(e) {
|
|
if (ref.current && !ref.current.contains(e.target)) setOpen(false);
|
|
}
|
|
document.addEventListener("mousedown", onDocClick);
|
|
return () => document.removeEventListener("mousedown", onDocClick);
|
|
}, []);
|
|
|
|
if (locked) {
|
|
return (
|
|
<div className="ctrl owner-ctrl">
|
|
<label>Propietario</label>
|
|
<button type="button" className="owner-btn" disabled>
|
|
{lockedLabel}
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function applySet(nextSet) {
|
|
selRef.current = nextSet;
|
|
setSelected(nextSet);
|
|
const params = new URLSearchParams(searchParams.toString());
|
|
if (nextSet.size === 0 || nextSet.size === owners.length) params.delete("owner");
|
|
else params.set("owner", [...nextSet].join(","));
|
|
const qs = params.toString();
|
|
router.push(qs ? `${pathname}?${qs}` : pathname, { scroll: false });
|
|
}
|
|
|
|
function toggle(email) {
|
|
const next = new Set(selRef.current);
|
|
if (next.has(email)) next.delete(email);
|
|
else next.add(email);
|
|
if (next.size === 0) return; // no se puede desmarcar al último
|
|
applySet(next);
|
|
}
|
|
|
|
const label =
|
|
selected.size === owners.length
|
|
? "Todos los propietarios"
|
|
: selected.size === 1
|
|
? owners.find((o) => o.email === [...selected][0])?.name || [...selected][0]
|
|
: `${selected.size} propietarios`;
|
|
|
|
return (
|
|
<div className="ctrl owner-ctrl" ref={ref}>
|
|
<label>Propietario</label>
|
|
<button type="button" className="owner-btn" onClick={() => setOpen((o) => !o)}>
|
|
{label}
|
|
</button>
|
|
<div className={`owner-panel${open ? " open" : ""}`}>
|
|
<div className="oa">
|
|
<a onClick={() => applySet(new Set(owners.map((o) => o.email)))}>Todos</a>
|
|
</div>
|
|
{owners.map((o) => (
|
|
<label className="owner-row" key={o.id}>
|
|
<input type="checkbox" checked={selected.has(o.email)} onChange={() => toggle(o.email)} />
|
|
{" "}
|
|
{o.name}
|
|
</label>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|