informe-comercial-alfa/app/m/components/CalendarioDia.js

66 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useMemo, useState } from "react";
import Link from "next/link";
import { usePathname, useSearchParams } from "next/navigation";
import Tarjeta from "./Tarjeta";
import { diaCortoES } from "../../lib/campo";
import { minAHora } from "../../lib/agendador";
// «Calendario»: franja de la semana (L–V, flechas para cambiar) con puntos por día, y el día elegido por horas.
const HORAS = [8, 9, 10, 11, 12, 13, 14, 15, 16];
const NOM = ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"];
export default function CalendarioDia({ items, dias, hoy, off, conOwner }) {
const path = usePathname();
const sp = useSearchParams();
const [sel, setSel] = useState(dias.includes(hoy) ? hoy : dias[0]);
const href = (n) => {
const p = new URLSearchParams(sp.toString());
if (n) p.set("sem", String(n)); else p.delete("sem");
return `${path}${p.toString() ? `?${p.toString()}` : ""}`;
};
const porDia = useMemo(() => {
const m = {};
for (const i of items) (m[i.dia] ||= []).push(i);
return m;
}, [items]);
const del = (porDia[sel] || []).sort((a, b) => (a.min ?? 1e9) - (b.min ?? 1e9));
const tareas = del.filter((i) => i.esTarea);
const conHora = del.filter((i) => !i.esTarea);
return (
<>
<div className="m-franja">
<Link className="m-fl" href={href(off - 1)} aria-label="Semana anterior">‹</Link>
{dias.map((d) => {
const n = (porDia[d] || []).length;
return (
<button key={d} type="button" className={`m-dsem${sel === d ? " on" : ""}${d === hoy ? " hoy" : ""}`} onClick={() => setSel(d)}>
{NOM[new Date(`${d}T12:00:00Z`).getUTCDay()]}<b>{Number(d.slice(8, 10))}</b>
<span className="m-puntos" aria-label={`${n} actividades`}>{"●".repeat(Math.min(n, 4))}</span>
</button>
);
})}
<Link className="m-fl" href={href(off + 1)} aria-label="Semana siguiente">›</Link>
</div>
<p className="m-fecha">{diaCortoES(sel)} · {del.length} actividad{del.length === 1 ? "" : "es"}</p>
<div className="m-horas">
{HORAS.map((h) => {
const aqui = conHora.filter((i) => Math.floor((i.min ?? -1) / 60) === h);
if (h === 13 && !aqui.length) return <div key={h} className="m-hora alm"><span>13:00</span><div>almuerzo</div></div>;
return (
<div key={h} className="m-hora">
<span>{minAHora(h * 60)}</span>
<div className="m-lista">{aqui.map((i) => <Tarjeta key={i.id} it={i} conOwner={conOwner} />)}</div>
</div>
);
})}
{conHora.filter((i) => i.min == null || Math.floor(i.min / 60) < 8 || Math.floor(i.min / 60) > 16).length > 0 && (
<div className="m-hora"><span>otras</span><div className="m-lista">{conHora.filter((i) => i.min == null || Math.floor(i.min / 60) < 8 || Math.floor(i.min / 60) > 16).map((i) => <Tarjeta key={i.id} it={i} conOwner={conOwner} />)}</div></div>
)}
{tareas.length > 0 && (
<div className="m-hora"><span>☑</span><div className="m-lista">{tareas.map((i) => <Tarjeta key={i.id} it={i} conOwner={conOwner} />)}</div></div>
)}
</div>
</>
);
}