diff --git a/src/app/game/berry-dash/chatroom/page.tsx b/src/app/game/berry-dash/chatroom/page.tsx index b68e0a3..f73473c 100644 --- a/src/app/game/berry-dash/chatroom/page.tsx +++ b/src/app/game/berry-dash/chatroom/page.tsx @@ -5,6 +5,7 @@ import { BackButton } from '@/app/components/BackButton' import { DiscordButton } from '@/app/components/DiscordButton' import { GetIconForUser } from '@/util/bd' import { getCookie } from '@/util/cookie' +import { formatHumanTime } from '@/util/time' import { faTrashCan } from '@fortawesome/free-regular-svg-icons' import { faPencil } from '@fortawesome/free-solid-svg-icons' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' @@ -30,6 +31,8 @@ interface Message { birdColor: BirdColor overlayColor: BirdColor customIcon: string | null + timestamp: number + editedAt: number editing: boolean } @@ -233,7 +236,20 @@ export default function BerryDashChatroom () { )} -

{item.username}

+

+ {item.username}{' '} + + {formatHumanTime(item.timestamp)} + + {item.editedAt != 0 && ( + <> + {' '} + + (edited) + + + )} +

{atob(item.content)}

diff --git a/src/util/time.ts b/src/util/time.ts new file mode 100644 index 0000000..5ad3dca --- /dev/null +++ b/src/util/time.ts @@ -0,0 +1,27 @@ +export const formatHumanTime = (unixTimestamp: number) => { + const date = new Date(unixTimestamp * 1000) + const now = new Date() + + const isToday = + date.getFullYear() === now.getFullYear() && + date.getMonth() === now.getMonth() && + date.getDate() === now.getDate() + + const yesterday = new Date() + yesterday.setDate(now.getDate() - 1) + const isYesterday = + date.getFullYear() === yesterday.getFullYear() && + date.getMonth() === yesterday.getMonth() && + date.getDate() === yesterday.getDate() + + const timeStr = date.toLocaleTimeString(undefined, { + hour: 'numeric', + minute: '2-digit' + }) + + if (isToday) return timeStr + if (isYesterday) return `Yesterday at ${timeStr}` + + const dateStr = date.toLocaleDateString() + return `${dateStr}, ${timeStr}` +}