1 import { c } from 'ttag';
3 import { MINUTE } from '../../constants';
4 import { format as formatUTC, isNextDay, isSameDay, isSameMonth, isSameYear } from '../../date-fns-utc';
6 const getStartsTodayText = (title: string) => {
7 return c('Alarm notification').t`${title} starts today`;
10 const getStartsAtText = (title: string, formattedHour: string) => {
11 return c('Alarm notification').t`${title} starts at ${formattedHour}`;
14 const getStartsOnText = (title: string, formattedDate: string) => {
15 return c('Alarm notification').t`${title} starts on ${formattedDate}`;
18 const getStartsOnAtText = (title: string, formattedDate: string, formattedHour: string) => {
19 return c('Alarm notification').t`${title} starts on ${formattedDate} at ${formattedHour}`;
22 const getStartedAtText = (title: string, formattedHour: string) => {
23 return c('Alarm notification').t`${title} started at ${formattedHour}`;
26 const getStartedOnText = (title: string, formattedDate: string) => {
27 return c('Alarm notification').t`${title} started on ${formattedDate}`;
30 const getStartedOnAtText = (title: string, formattedDate: string, formattedHour: string) => {
31 return c('Alarm notification').t`${title} started on ${formattedDate} at ${formattedHour}`;
37 startFakeUTCDate: Date;
41 const getAlarmMessageText = ({ title, isAllDay, startFakeUTCDate, nowFakeUTCDate, formatOptions }: Arguments) => {
42 const formattedHour = formatUTC(startFakeUTCDate, 'p', formatOptions);
43 // because of browser timer imprecision, allow for a one-minute margin to determine simultaneity
44 const isNow = Math.abs(+startFakeUTCDate - +nowFakeUTCDate) <= MINUTE / 2;
45 const isInFuture = startFakeUTCDate > nowFakeUTCDate;
48 return c('Alarm notification').t`${title} starts now`;
52 if (isSameDay(nowFakeUTCDate, startFakeUTCDate)) {
54 return getStartsTodayText(title);
56 return getStartedAtText(title, formattedHour);
59 if (isNextDay(startFakeUTCDate, nowFakeUTCDate)) {
61 return c('Alarm notification').t`${title} started yesterday`;
63 return c('Alarm notification').t`${title} started yesterday at ${formattedHour}`;
66 if (isSameMonth(nowFakeUTCDate, startFakeUTCDate)) {
67 const formattedDate = formatUTC(startFakeUTCDate, 'eeee do', formatOptions);
69 return getStartedOnText(title, formattedDate);
71 return getStartedOnAtText(title, formattedDate, formattedHour);
74 if (isSameYear(nowFakeUTCDate, startFakeUTCDate)) {
75 const formattedDate = formatUTC(startFakeUTCDate, 'eeee do MMMM', formatOptions);
77 return getStartedOnText(title, formattedDate);
79 return getStartedOnAtText(title, formattedDate, formattedHour);
83 const formattedDateWithoutTime = formatUTC(startFakeUTCDate, 'PPPP', formatOptions);
84 return getStartedOnText(title, formattedDateWithoutTime);
86 const formattedDateWithTime = formatUTC(startFakeUTCDate, 'PPPPp', formatOptions);
87 return getStartedOnText(title, formattedDateWithTime);
90 if (isSameDay(nowFakeUTCDate, startFakeUTCDate)) {
92 return getStartsTodayText(title);
94 return getStartsAtText(title, formattedHour);
97 if (isNextDay(nowFakeUTCDate, startFakeUTCDate)) {
99 return c('Alarm notification').t`${title} starts tomorrow`;
101 return c('Alarm notification').t`${title} starts tomorrow at ${formattedHour}`;
104 if (isSameMonth(nowFakeUTCDate, startFakeUTCDate)) {
105 const formattedDate = formatUTC(startFakeUTCDate, 'eeee do', formatOptions);
107 return getStartsOnText(title, formattedDate);
109 return getStartsOnAtText(title, formattedDate, formattedHour);
112 if (isSameYear(nowFakeUTCDate, startFakeUTCDate)) {
113 const formattedDate = formatUTC(startFakeUTCDate, 'eeee do MMMM', formatOptions);
115 return getStartsOnText(title, formattedDate);
117 return getStartsOnAtText(title, formattedDate, formattedHour);
121 const formattedDateWithoutTime = formatUTC(startFakeUTCDate, 'PPPP', formatOptions);
122 return getStartsOnText(title, formattedDateWithoutTime);
125 const formattedDateWithTime = formatUTC(startFakeUTCDate, 'PPPPp', formatOptions);
126 return getStartsOnText(title, formattedDateWithTime);
129 export default getAlarmMessageText;