diff --git a/src/lib/datetime.ts b/src/lib/datetime.ts index 7dafd50..35db356 100644 --- a/src/lib/datetime.ts +++ b/src/lib/datetime.ts @@ -57,11 +57,15 @@ function getLocalizedNowWord(locale: string) { return translations[locale as keyof typeof translations] || 'now'; } -export const humanDeltaFromNow = (start: string | Date | Dayjs, end: string | Date | Dayjs): { +export const humanDeltaFromNow = (start: string | Date | Dayjs, end?: string | Date | Dayjs): { message: string, delta: "PAST" | "FUTURE" | "NOW" } => { + if (!end) { + end = start + } + // check if end is in the past if (dayjs(end).isBefore(dayjs())) { return { @@ -112,29 +116,27 @@ export const pprintDateRange = (d1: string | Date | Dayjs, d2: string | Date | D * @param date2 */ export function formatDuration(date1: string | Date | Dayjs, date2: string | Date | Dayjs) { - const start = dayjs(date1) - const end = dayjs(date2) - const diff = end.diff(start) - const duration = dayjs.duration(diff) - const weeks = Math.floor(duration.asDays() / 7); - const days = duration.days() % 7; - const hours = duration.hours(); - const minutes = duration.minutes(); - const parts = []; + // ignore seconds and milliseconds + date1 = dayjs(date1).startOf('minute'); + date2 = dayjs(date2).startOf('minute'); - if (weeks > 0) { - parts.push(`${weeks} Wo`); - } - if (days > 0 || weeks > 0) { - parts.push(`${days} Tag${days > 1 ? 'e' : ''}`); - } - if (hours > 0) { - parts.push(`${hours} Std`); - } - if (minutes > 0) { - parts.push(`${minutes} Min`); - } + // get the difference in milliseconds + const diff = dayjs(date2).diff(dayjs(date1)); + const durationObj = dayjs.duration(diff); - return parts.join(', '); + // get the duration in weeks, days, hours and minutes + const weeks = Math.floor(durationObj.asWeeks()); + const days = durationObj.days(); + const hours = durationObj.hours(); + const minutes = durationObj.minutes(); + + // create a string array with the duration parts + const result: string[] = []; + if (weeks > 0) result.push(`${weeks} Wo`); + if (days > 0) result.push(`${days} Tag${days > 1 ? 'e' : ''}`); + if (hours > 0) result.push(`${hours} Std`); + if (minutes > 0) result.push(`${minutes} Min`); + + return result.join(', '); } \ No newline at end of file diff --git a/src/pages/chat/components/Announcement.module.css b/src/pages/chat/components/Announcement.module.css index 993b4e7..b331de3 100644 --- a/src/pages/chat/components/Announcement.module.css +++ b/src/pages/chat/components/Announcement.module.css @@ -3,17 +3,14 @@ position: relative; flex-direction: column; - box-shadow: var(--shadow); - background-color: var(--mantine-color-body); padding: var(--padding); border: var(--border); border-color: var(--mantine-primary-color-5); - border-radius: var(--mantine-radius-lg); + border-radius: var(--border-radius); - margin: var(--gap); max-width: 100%; word-break: break-all; @@ -24,4 +21,11 @@ font-size: var(--mantine-font-size-lg); font-weight: bold; color: var(--mantine-primary-color-5); + margin: 0; +} + +.subjectStack { + display: flex; + flex-direction: column; + justify-content: center; } \ No newline at end of file diff --git a/src/pages/chat/components/Announcement.tsx b/src/pages/chat/components/Announcement.tsx index 4d32197..5611f41 100644 --- a/src/pages/chat/components/Announcement.tsx +++ b/src/pages/chat/components/Announcement.tsx @@ -1,26 +1,37 @@ import InnerHtml from "@/components/InnerHtml"; import classes from './Announcement.module.css' -import {Group, ThemeIcon} from "@mantine/core"; +import {Group, Text, ThemeIcon, Tooltip} from "@mantine/core"; import {IconSpeakerphone} from "@tabler/icons-react"; +import {MessagesModel} from "@/models/MessageTypes.ts"; +import {getUserName} from "@/components/users/modals/util.tsx"; +import {humanDeltaFromNow, pprintDate} from "@/lib/datetime.ts"; -export default function Announcement({subject, content}: { - subject: string | null, - content: string, +export default function Announcement({announcement}: { + announcement: MessagesModel }) { + const senderName = getUserName(announcement.expand.sender) return