2026-05-25 05:25:57 +08:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import type { Post } from "../../../types/post";
|
|
|
|
|
|
|
|
|
|
export type DayGroup = {
|
|
|
|
|
dayKey: string;
|
|
|
|
|
dayLabel: string;
|
|
|
|
|
items: Post[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function localeFor(lang: string): string {
|
2026-05-26 07:36:53 +08:00
|
|
|
const locales: Record<string, string> = {
|
|
|
|
|
zh: "zh-CN",
|
|
|
|
|
en: "en-US",
|
|
|
|
|
ja: "ja-JP",
|
|
|
|
|
ko: "ko-KR",
|
|
|
|
|
vi: "vi-VN",
|
|
|
|
|
id: "id-ID",
|
|
|
|
|
ms: "ms-MY",
|
|
|
|
|
};
|
|
|
|
|
return locales[lang] ?? "en-US";
|
2026-05-25 05:25:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dayKey(iso: string): string {
|
|
|
|
|
const d = new Date(iso);
|
|
|
|
|
return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dayLabel(iso: string, lang: string): string {
|
|
|
|
|
const d = new Date(iso);
|
|
|
|
|
const today = new Date();
|
|
|
|
|
const yesterday = new Date();
|
|
|
|
|
yesterday.setDate(today.getDate() - 1);
|
|
|
|
|
const isSameDay = (a: Date, b: Date) =>
|
|
|
|
|
a.getFullYear() === b.getFullYear() &&
|
|
|
|
|
a.getMonth() === b.getMonth() &&
|
|
|
|
|
a.getDate() === b.getDate();
|
|
|
|
|
if (isSameDay(d, today)) {
|
2026-05-26 10:03:12 +08:00
|
|
|
return lang === "zh-CN" ? "今天" : "Today";
|
2026-05-25 05:25:57 +08:00
|
|
|
}
|
|
|
|
|
if (isSameDay(d, yesterday)) {
|
2026-05-26 10:03:12 +08:00
|
|
|
return lang === "zh-CN" ? "昨天" : "Yesterday";
|
2026-05-25 05:25:57 +08:00
|
|
|
}
|
|
|
|
|
return new Intl.DateTimeFormat(localeFor(lang), {
|
|
|
|
|
month: "long",
|
|
|
|
|
day: "numeric",
|
|
|
|
|
}).format(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useGroupedByDay(posts: Post[], lang: string): DayGroup[] {
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
const groups: DayGroup[] = [];
|
|
|
|
|
const seen = new Map<string, DayGroup>();
|
|
|
|
|
for (const p of posts) {
|
|
|
|
|
const k = dayKey(p.publishedAt);
|
|
|
|
|
let g = seen.get(k);
|
|
|
|
|
if (!g) {
|
|
|
|
|
g = { dayKey: k, dayLabel: dayLabel(p.publishedAt, lang), items: [] };
|
|
|
|
|
seen.set(k, g);
|
|
|
|
|
groups.push(g);
|
|
|
|
|
}
|
|
|
|
|
g.items.push(p);
|
|
|
|
|
}
|
|
|
|
|
return groups;
|
|
|
|
|
}, [posts, lang]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { dayKey as _dayKey, dayLabel as _dayLabel };
|