"use client"; import { useState, useMemo } from "react"; import { actAncla, actEstadoFinal } from "../../lib/derive"; import { actividadUrl } from "../../lib/constants"; const AG_DIAS = ["Lun", "Mar", "Mié", "Jue", "Vie", "Sáb", "Dom"]; const AG_MESES = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]; const AG_MESES_LARGO = [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre", ]; const pad = (n) => (n < 10 ? "0" + n : "" + n); const iso = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; const hoyISO = () => iso(new Date()); // Semana Lunes-Domingo, igual que el resto del informe. function inicioSemana(d) { const x = new Date(d); const dow = (x.getDay() + 6) % 7; x.setDate(x.getDate() - dow); x.setHours(0, 0, 0, 0); return x; } function fechaISO(a) { const f = actAncla(a); return f ? String(f).slice(0, 10) : null; } function diasParaVencer(a) { const f = fechaISO(a); if (!f) return null; const d = new Date(f + "T12:00:00"); const hoy = new Date(); hoy.setHours(12, 0, 0, 0); return Math.round((d - hoy) / 86400000); } function fechaTxt(f) { if (!f) return "—"; const [y, m, d] = f.split("-"); return `${d}/${m}/${y}`; } function Item({ a }) { const venc = a.estadoFinal === "Vencida"; const hecha = a.estadoFinal === "Completada"; const ref = a.referente && a.referente.nombre ? a.referente.nombre : a.asunto || "—"; const sub = [a.owner.split(" ").slice(0, 2).join(" ")]; if (venc) sub.push(`vencida hace ${Math.abs(a.dv)} día${Math.abs(a.dv) === 1 ? "" : "s"}`); if (hecha) sub.push("completada"); return ( {a.tipo} · {ref} {sub.join(" · ")} ); } export default function AgendaClient({ actividades }) { const [vista, setVista] = useState("semana"); const [ancla, setAncla] = useState(() => new Date()); const [tipos, setTipos] = useState(new Set(["Cita", "Llamada", "Tarea"])); const [verHechas, setVerHechas] = useState(false); const [diaSel, setDiaSel] = useState(null); const list = useMemo(() => { return actividades .filter((a) => tipos.has(a.tipo) && a.estado !== "cancelada" && fechaISO(a)) .map((a) => ({ ...a, fecha: fechaISO(a), dv: diasParaVencer(a), estadoFinal: actEstadoFinal(a) })) .filter((a) => verHechas || a.estadoFinal !== "Completada"); }, [actividades, tipos, verHechas]); const lunesHoy = inicioSemana(new Date()); const domingoHoy = new Date(lunesHoy); domingoHoy.setDate(lunesHoy.getDate() + 6); const estaSem = list.filter((a) => a.fecha >= iso(lunesHoy) && a.fecha <= iso(domingoHoy)).length; const venc = list.filter((a) => a.estadoFinal === "Vencida").length; const pend = list.filter((a) => a.estadoFinal === "Pendiente").length; const toggleTipo = (t) => { setTipos((prev) => { const next = new Set(prev); if (next.has(t)) next.delete(t); else next.add(t); return next; }); }; const cursor = vista === "mes" ? new Date(ancla.getFullYear(), ancla.getMonth(), 1) : ancla; const mover = (d) => { setDiaSel(null); setAncla((prev) => { const next = new Date(prev); if (vista === "semana") next.setDate(next.getDate() + d * 7); else next.setMonth(next.getMonth() + d); return next; }); }; let cuerpo, lbl; if (vista === "semana") { const lunes = inicioSemana(cursor); const dias = Array.from({ length: 7 }, (_, i) => { const d = new Date(lunes); d.setDate(lunes.getDate() + i); return d; }); lbl = `${dias[0].getDate()} ${AG_MESES[dias[0].getMonth()]} – ${dias[6].getDate()} ${AG_MESES[dias[6].getMonth()]} ${dias[6].getFullYear()}`; const hoy = hoyISO(); cuerpo = (
Sin actividades este día.
)}{v.length} actividades planificadas cuya fecha ya pasó y siguen abiertas · la más antigua lleva {masViejo} días
No hay actividades vencidas con este filtro.
)}Solo lectura: muestra lo que ya está planificado en Dynamics. Clic en una actividad la abre en Dynamics.