Files
openmonetis/src/features/notes/lib/formatters.ts
Felipe Coutinho 367d78d43d fix(dashboard/anotacoes): corrigir divergência de fuso no formatter de datas
Intl.DateTimeFormat sem timeZone usava o fuso do servidor (UTC) no SSR
e o fuso do browser (BRT) no cliente, causando erro de hidratação.
Ambos os formatters passam a usar timeZone: "America/Sao_Paulo" explicitamente.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-02 22:08:29 +00:00

54 lines
1.4 KiB
TypeScript

type NoteTasksSummaryInput = {
type: string;
tasks?: Array<{ completed: boolean }> | null;
};
const NOTE_CREATED_AT_FORMATTER = new Intl.DateTimeFormat("pt-BR", {
dateStyle: "medium",
timeZone: "America/Sao_Paulo",
});
const NOTE_CREATED_AT_LONG_FORMATTER = new Intl.DateTimeFormat("pt-BR", {
dateStyle: "long",
timeStyle: "short",
timeZone: "America/Sao_Paulo",
});
const parseNoteDate = (value: string | Date | null | undefined) => {
if (!value) {
return null;
}
const parsed = value instanceof Date ? value : new Date(value);
return Number.isNaN(parsed.getTime()) ? null : parsed;
};
export const buildNoteDisplayTitle = (value: string | null | undefined) => {
const trimmed = value?.trim() ?? "";
return trimmed.length > 0 ? trimmed : "Anotação sem título";
};
export const getNoteTasksSummary = (note: NoteTasksSummaryInput) => {
if (note.type !== "tarefa") {
return "Nota";
}
const tasks = note.tasks ?? [];
const completed = tasks.filter((task) => task.completed).length;
return `${completed}/${tasks.length} concluídas`;
};
export const formatNoteCreatedAt = (
value: string | Date | null | undefined,
) => {
const parsed = parseNoteDate(value);
return parsed ? NOTE_CREATED_AT_FORMATTER.format(parsed) : "";
};
export const formatNoteCreatedAtLong = (
value: string | Date | null | undefined,
) => {
const parsed = parseNoteDate(value);
return parsed ? NOTE_CREATED_AT_LONG_FORMATTER.format(parsed) : null;
};