Skip to content

Commit c3dd77c

Browse files
siiickPascal-Delange
authored andcommitted
refactor: Add FormattedDatesRange component
1 parent c30dc69 commit c3dd77c

File tree

4 files changed

+88
-57
lines changed

4 files changed

+88
-57
lines changed

packages/app-builder/src/components/PaginationButtons.tsx

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ type CursorPaginationsButtonsProps = PaginatedResponse<ItemWithId> & {
2626
hideBoundaries?: boolean;
2727
};
2828

29-
function getStartAndEndFormatted(
30-
startTs: string | undefined,
31-
endTs: string | undefined,
32-
language: string,
33-
) {
29+
function FormattedDatesRange({
30+
startTs,
31+
endTs,
32+
language,
33+
}: {
34+
startTs: string | undefined;
35+
endTs: string | undefined;
36+
language: string;
37+
}) {
38+
const { t } = useTranslation(['common']);
3439
if (!startTs || !endTs) {
35-
return { startFormatted: '', endFormatted: '' };
40+
return null;
3641
}
3742

3843
const startDate = new Date(startTs);
@@ -47,23 +52,73 @@ function getStartAndEndFormatted(
4752
isSameLocalDay &&
4853
startDate.getHours() === endDate.getHours() &&
4954
startDate.getMinutes() === endDate.getMinutes();
50-
const isSameSecond = isSameMinute && startDate.getSeconds() === endDate.getSeconds();
51-
52-
const startFormatted = formatDateTime(startTs, {
53-
language,
54-
dateStyle: 'medium',
55-
timeStyle: isSameMinute ? 'medium' : 'short',
56-
});
57-
58-
const endFormatted = !isSameSecond
59-
? formatDateTime(endTs, {
60-
language,
61-
dateStyle: isSameLocalDay ? undefined : 'medium',
62-
timeStyle: isSameMinute ? 'medium' : 'short',
63-
})
64-
: null;
65-
66-
return { startFormatted, endFormatted };
55+
56+
if (isSameMinute && startDate.getSeconds() === endDate.getSeconds())
57+
return (
58+
<Trans
59+
t={t}
60+
i18nKey="common:items_displayed_same_datetime"
61+
components={{ StartToEnd: <span /> }}
62+
values={{
63+
date: formatDateTime(startDate, {
64+
language,
65+
dateStyle: 'medium',
66+
timeStyle: undefined,
67+
}),
68+
time: formatDateTime(startDate, {
69+
language,
70+
dateStyle: undefined,
71+
timeStyle: 'short',
72+
}),
73+
}}
74+
/>
75+
);
76+
77+
if (isSameLocalDay || isSameMinute)
78+
return (
79+
<Trans
80+
t={t}
81+
i18nKey="common:items_displayed_same_date"
82+
components={{ StartToEnd: <span /> }}
83+
values={{
84+
date: formatDateTime(startDate, {
85+
language,
86+
dateStyle: 'medium',
87+
timeStyle: undefined,
88+
}),
89+
start: formatDateTime(startTs, {
90+
language,
91+
dateStyle: undefined,
92+
timeStyle: isSameMinute ? 'medium' : 'short',
93+
}),
94+
end: formatDateTime(endTs, {
95+
language,
96+
dateStyle: undefined,
97+
timeStyle: isSameMinute ? 'medium' : 'short',
98+
}),
99+
}}
100+
/>
101+
);
102+
103+
return (
104+
<Trans
105+
t={t}
106+
i18nKey="common:items_displayed_dates"
107+
components={{ StartToEnd: <span /> }}
108+
values={{
109+
start: formatDateTime(startTs, {
110+
language,
111+
dateStyle: 'medium',
112+
timeStyle: 'short',
113+
}),
114+
end: formatDateTime(endTs, {
115+
language,
116+
dateStyle: 'medium',
117+
timeStyle: 'short',
118+
}),
119+
}}
120+
/>
121+
);
67122
}
68123

69124
export function CursorPaginationButtons({
@@ -73,7 +128,6 @@ export function CursorPaginationButtons({
73128
onPaginationChange,
74129
hideBoundaries,
75130
}: CursorPaginationsButtonsProps) {
76-
const { t } = useTranslation(['common']);
77131
const language = useFormatLanguage();
78132

79133
const startTs = items[0]?.createdAt;
@@ -95,38 +149,12 @@ export function CursorPaginationButtons({
95149
onPaginationChange(pagination);
96150
};
97151

98-
const { startFormatted, endFormatted } = getStartAndEndFormatted(startTs, endTs, language);
99-
100152
const previousDisabled = !hasPreviousPage;
101153
const nextDisabled = !hasNextPage;
102154

103-
const sameSecondBoundaries = (
104-
<span>
105-
{t('common:items_displayed', {
106-
time: startFormatted,
107-
})}
108-
</span>
109-
);
110-
const defaultBoundaries = (
111-
<Trans
112-
t={t}
113-
i18nKey="common:items_displayed_datetime"
114-
components={{ StartToEnd: <span /> }}
115-
values={{
116-
start: startFormatted,
117-
end: endFormatted,
118-
}}
119-
/>
120-
);
121-
122155
return (
123156
<div className="flex items-center justify-end gap-2">
124-
{hideBoundaries || (startFormatted === '' && endFormatted === '')
125-
? null
126-
: endFormatted === null
127-
? sameSecondBoundaries
128-
: defaultBoundaries}
129-
157+
{hideBoundaries ? null : <FormattedDatesRange {...{ startTs, endTs, language }} />}
130158
<Button onClick={fetchPrevious} variant="secondary" disabled={previousDisabled}>
131159
<Icon icon="arrow-left" className="size-4" />
132160
</Button>

packages/app-builder/src/locales/ar/common.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
"loading": "جارٍ التحميل...",
4646
"understand": "أعي ذلك",
4747
"global_error": "حدث خطأ ما.",
48-
"items_displayed_datetime": "<StartToEnd>{{start}} من {{end}}</StartToEnd>",
49-
"items_displayed": "عرض {{time}}",
48+
"items_displayed_dates": "<StartToEnd>من {{start}} إلى {{end}}</StartToEnd>",
49+
"items_displayed_same_date": "<StartToEnd>في {{date}} من {{start}} إلى {{end}}</StartToEnd>",
50+
"items_displayed_same_datetime": "<StartToEnd>في {{date}} الساعة {{time}}</StartToEnd>",
5051
"page_displayed_of_total": "صفحة <PageCount>{{currentPage}} من {{pageCount}}</PageCount>",
5152
"help_center.to_navigate": "للانتقال",
5253
"help_center.to_switch_tabs": "للتبديل بين التبويبات",

packages/app-builder/src/locales/en/common.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
"loading": "Loading...",
6969
"understand": "I understand",
7070
"global_error": "Something went wrong.",
71-
"items_displayed_datetime": "<StartToEnd>{{start}} to {{end}}</StartToEnd>",
72-
"items_displayed": "at {{time}}",
71+
"items_displayed_dates": "<StartToEnd>From {{start}} to {{end}}</StartToEnd>",
72+
"items_displayed_same_date": "<StartToEnd>On {{date}} from {{start}} to {{end}}</StartToEnd>",
73+
"items_displayed_same_datetime": "<StartToEnd>On {{date}} at {{time}}</StartToEnd>",
7374
"page_displayed_of_total": "Page <PageCount>{{currentPage}} of {{pageCount}}</PageCount>",
7475
"help_center.to_navigate": "to navigate",
7576
"help_center.to_switch_tabs": "to switch tabs",

packages/app-builder/src/locales/fr/common.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
"help_center.to_navigate": "naviguer",
4545
"help_center.to_switch_tabs": "pour changer d'onglet",
4646
"hide": "Cacher",
47-
"items_displayed_datetime": "<StartToEnd>{{start}} à {{end}}</StartToEnd>",
48-
"items_displayed": "à {{time}}",
47+
"items_displayed_dates": "<StartToEnd>Du {{start}} au {{end}}<StartToEnd>",
48+
"items_displayed_same_date": "<StartToEnd>Le {{date}} de {{start}} à {{end}}</StartToEnd>",
49+
"items_displayed_same_datetime": "<StartToEnd>Le {{date}} à {{time}}</StartToEnd>",
4950
"loading": "Chargement...",
5051
"more_options": "Plus d'options",
5152
"or": "ou",

0 commit comments

Comments
 (0)