// Formatadores criados uma única vez: instanciar um Intl.DateTimeFormat custa ~10-40µs e // estas funções são chamadas dentro de v-for que repinta a cada poll. const FMT_DATA = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit", year: "numeric" }); const FMT_HORA = new Intl.DateTimeFormat("pt-BR", { hour: "2-digit", minute: "2-digit" }); const FMT_DIA_MES = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit" }); const FMT_DATA_HORA = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit" }); // ano com 2 dígitos: usado na lista de documentos, onde o espaço é apertado const FMT_DATA_HORA_ANO_CURTO = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit", year: "2-digit", hour: "2-digit", minute: "2-digit" }); // sem ano: usado nas solicitações de reset de senha, todas recentes const FMT_DATA_HORA_SEM_ANO = new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" }); const UM_DIA_MS = 86400000; function paraData(valor) { if (!valor) return null; const d = new Date(valor); return Number.isNaN(d.getTime()) ? null : d; } function formatarCom(fmt, valor) { const d = paraData(valor); return d ? fmt.format(d) : ""; } export function formatarData(valor) { return formatarCom(FMT_DATA, valor); } export function formatarHora(valor) { return formatarCom(FMT_HORA, valor); } export function formatarDataHora(valor) { return formatarCom(FMT_DATA_HORA, valor); } export function formatarDataHoraAnoCurto(valor) { return formatarCom(FMT_DATA_HORA_ANO_CURTO, valor); } export function formatarDataHoraSemAno(valor) { return formatarCom(FMT_DATA_HORA_SEM_ANO, valor); } export function ehNovoDia(atual, anterior) { if (!atual) return false; if (!anterior) return true; return new Date(atual).toDateString() !== new Date(anterior).toDateString(); } // rótulo curto de recência para listas de conversa: hora se for hoje, "Ontem", senão dd/mm export function formatarRecencia(valor) { const d = paraData(valor); if (!d) return ""; const hoje = new Date().setHours(0, 0, 0, 0); const dia = new Date(d).setHours(0, 0, 0, 0); if (dia === hoje) return FMT_HORA.format(d); if (dia === hoje - UM_DIA_MS) return "Ontem"; return FMT_DIA_MES.format(d); } /** * Agrupa uma lista em faixas de recência ("Hoje", "Ontem", "Últimos 7 dias", "Mais antigas"), * descartando as faixas vazias. Usado pela lista de conversas do chat e pela do WhatsApp. * * @param {Array} lista * @param {(item: any) => number|string|Date|null} obterTimestamp */ export function agruparPorRecencia(lista, obterTimestamp) { const hoje = new Date().setHours(0, 0, 0, 0); const ontem = hoje - UM_DIA_MS; const semana = hoje - 6 * UM_DIA_MS; const grupos = [ { label: "Hoje", items: [] }, { label: "Ontem", items: [] }, { label: "Últimos 7 dias", items: [] }, { label: "Mais antigas", items: [] } ]; for (const item of lista ?? []) { const bruto = obterTimestamp(item); const t = bruto ? new Date(bruto).getTime() : 0; if (t >= hoje) grupos[0].items.push(item); else if (t >= ontem) grupos[1].items.push(item); else if (t >= semana) grupos[2].items.push(item); else grupos[3].items.push(item); } return grupos.filter((g) => g.items.length > 0); }