Skip to content

Commit 249edbe

Browse files
author
André Luiz Abdalla Silveira
committed
refactor(DateRangeInput): use shared constant, cn() helper, and typed error map
- Export MILLISECONDS_IN_ONE_SECOND from utils/date and reuse it - Replace template literal classNames with cn() utility - Type errorMessageIds with MessageDescriptor['id'] - Bump error message text size from xs to sm Closes #1784
1 parent fc5d6ad commit 249edbe

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

dashboard/src/components/DateRangeInput/DateRangeInput.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import {
99

1010
import { useNavigate, useSearch } from '@tanstack/react-router';
1111

12+
import type { MessageDescriptor } from 'react-intl';
1213
import { useIntl } from 'react-intl';
1314

1415
import { Input } from '@/components/ui/input';
15-
import { dateObjectToTimestampInSeconds, daysToSeconds } from '@/utils/date';
16+
import {
17+
dateObjectToTimestampInSeconds,
18+
daysToSeconds,
19+
MILLISECONDS_IN_ONE_SECOND,
20+
} from '@/utils/date';
1621
import { REDUCED_TIME_SEARCH } from '@/utils/constants/general';
22+
import { cn } from '@/lib/utils';
1723

1824
const ERROR_CLEAR_TIMEOUT = 3000;
19-
const MILLISECONDS_IN_SECOND = 1000;
2025
const ISO_DATE_LENGTH = 10;
2126
const MIN_DATE_STR = '2024-01-01';
2227

@@ -27,10 +32,12 @@ const getDefaultStartTimestamp = (): number =>
2732
getDefaultEndTimestamp() - daysToSeconds(REDUCED_TIME_SEARCH);
2833

2934
const timestampToDateString = (ts: number): string =>
30-
new Date(ts * MILLISECONDS_IN_SECOND).toISOString().slice(0, ISO_DATE_LENGTH);
35+
new Date(ts * MILLISECONDS_IN_ONE_SECOND)
36+
.toISOString()
37+
.slice(0, ISO_DATE_LENGTH);
3138

3239
const dateStringToTimestamp = (dateStr: string): number =>
33-
Math.floor(new Date(dateStr).getTime() / MILLISECONDS_IN_SECOND);
40+
Math.floor(new Date(dateStr).getTime() / MILLISECONDS_IN_ONE_SECOND);
3441

3542
type ErrorField = 'start' | 'startTooEarly' | 'end' | 'endFuture' | null;
3643

@@ -116,12 +123,15 @@ const DateRangeInput = (): JSX.Element => {
116123
[navigate, startTs, todayTs, triggerError],
117124
);
118125

119-
const errorMessageIds = {
126+
const errorMessageIds: Record<
127+
NonNullable<ErrorField>,
128+
MessageDescriptor['id']
129+
> = {
120130
start: 'dateRange.startAfterEnd',
121131
startTooEarly: 'dateRange.startTooEarly',
122132
end: 'dateRange.endBeforeStart',
123133
endFuture: 'dateRange.endAfterToday',
124-
} as const satisfies Record<NonNullable<ErrorField>, string>;
134+
};
125135

126136
const errorMessageId = errorField ? errorMessageIds[errorField] : null;
127137
const errorMessage = errorMessageId
@@ -137,7 +147,10 @@ const DateRangeInput = (): JSX.Element => {
137147
min={MIN_DATE_STR}
138148
max={endDateStr}
139149
onChange={handleStartChange}
140-
className={`w-[140px] cursor-pointer${errorField === 'start' ? 'border-red' : ''}`}
150+
className={cn(
151+
'w-[140px] cursor-pointer',
152+
errorField === 'start' ? 'border-red' : '',
153+
)}
141154
data-test-id="date-range-start"
142155
/>
143156
<span className="text-dim-gray"></span>
@@ -147,11 +160,14 @@ const DateRangeInput = (): JSX.Element => {
147160
min={startDateStr}
148161
max={todayDateStr}
149162
onChange={handleEndChange}
150-
className={`w-[140px] cursor-pointer${errorField === 'end' ? 'border-red' : ''}`}
163+
className={cn(
164+
'w-[140px] cursor-pointer',
165+
errorField === 'start' ? 'border-red' : '',
166+
)}
151167
data-test-id="date-range-end"
152168
/>
153169
</div>
154-
{errorMessage && <span className="text-red text-xs">{errorMessage}</span>}
170+
{errorMessage && <span className="text-red text-sm">{errorMessage}</span>}
155171
</div>
156172
);
157173
};

dashboard/src/utils/date.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const MILLISECONDS_IN_ONE_SECOND = 1000;
1+
export const MILLISECONDS_IN_ONE_SECOND = 1000;
22
export const MILLISECONDS_IN_ONE_HOUR = 3600000;
33
const SECONDS_IN_ONE_DAY = 86400;
44

0 commit comments

Comments
 (0)