Skip to content

Commit de8aaa9

Browse files
committed
Replace direct new date call with a helper function
1 parent 06ec554 commit de8aaa9

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

community-dashboard/app/Base/utils/temporal.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import { isDefined } from '@togglecorp/fujs';
22

3+
export function getDateSafe(value: Date | number | string) {
4+
if (typeof value === 'string') {
5+
return new Date(`${value}T00:00`);
6+
}
7+
8+
return new Date(value);
9+
}
10+
311
export function resolveTime(date: Date | number | string, resolution: 'day' | 'month' | 'year'): Date {
4-
const newDate = new Date(date);
12+
const newDate = getDateSafe(date);
13+
514
if (resolution === 'day' || resolution === 'month' || resolution === 'year') {
615
newDate.setUTCHours(0, 0, 0, 0);
716
}
@@ -41,23 +50,23 @@ export function getTimestamps(
4150
}
4251

4352
export function formatDate(value: number | string) {
44-
const date = new Date(value);
53+
const date = getDateSafe(value);
4554
return new Intl.DateTimeFormat(
4655
navigator.language,
4756
{ year: 'numeric', month: 'short', day: 'numeric' },
4857
).format(date);
4958
}
5059

5160
export function formatMonth(value: number | string) {
52-
const date = new Date(value);
61+
const date = getDateSafe(value);
5362
return new Intl.DateTimeFormat(
5463
navigator.language,
5564
{ year: 'numeric', month: 'short' },
5665
).format(date);
5766
}
5867

5968
export function formatYear(value: number | string) {
60-
const date = new Date(value);
69+
const date = getDateSafe(value);
6170
return new Intl.DateTimeFormat(
6271
navigator.language,
6372
{ year: 'numeric' },

community-dashboard/app/components/Calendar/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
IoCalendarOutline,
1212
} from 'react-icons/io5';
1313

14+
import { getDateSafe } from '#utils/temporal';
15+
1416
import Button from '../Button';
1517
import NumberInput from '../NumberInput';
1618
import SelectInput from '../SelectInput';
@@ -124,7 +126,7 @@ function Calendar<P extends CalendarDateProps>(props: Props<P>) {
124126
} = props;
125127

126128
const today = new Date();
127-
const current = initialDate ? new Date(initialDate) : today;
129+
const current = initialDate ? getDateSafe(initialDate) : today;
128130
const currentYear = current.getFullYear();
129131
const currentMonth = current.getMonth();
130132

@@ -193,8 +195,8 @@ function Calendar<P extends CalendarDateProps>(props: Props<P>) {
193195
styles.monthSelectionPopup,
194196
)}
195197
optionsPopupContentClassName={styles.popupContent}
196-
optionRenderer={undefined}
197-
optionRendererParams={undefined}
198+
// optionRenderer={undefined}
199+
// optionRendererParams={undefined}
198200
/>
199201
</div>
200202
</div>

community-dashboard/app/components/CalendarHeatMapContainer/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { scaleQuantile } from 'd3-scale';
55
import ReactTooltip from 'react-tooltip';
66
import InformationCard from '#components/InformationCard';
77

8+
import { getDateSafe } from '#utils/temporal';
9+
810
import styles from './styles.css';
911

1012
const githubColors = [' #eeeeee', '#d6e685', '#8cc665', '#44a340', '#1e6823'];
@@ -24,15 +26,16 @@ function getDateRange(data: Data[] | null | undefined) {
2426
const startDate = sortedData[0].date;
2527
const endDate = sortedData[sortedData.length - 1].date;
2628

27-
if (getDifferenceInDays(new Date(endDate).getTime(), new Date(startDate).getTime()) <= 365) {
28-
const currentYear = new Date(startDate).getFullYear();
29+
if (getDifferenceInDays(getDateSafe(endDate).getTime(),
30+
getDateSafe(startDate).getTime()) <= 365) {
31+
const currentYear = getDateSafe(startDate).getFullYear();
2932
return {
3033
startDate: encodeDate(new Date(currentYear, 0, 1)),
3134
endDate: encodeDate(new Date(currentYear, 11, 31)),
3235
};
3336
}
34-
const startDateTime = new Date(startDate);
35-
const endDateTime = new Date(endDate);
37+
const startDateTime = getDateSafe(startDate);
38+
const endDateTime = getDateSafe(endDate);
3639

3740
return {
3841
startDate: encodeDate(new Date(startDateTime.getFullYear(), startDateTime.getMonth(), 1)),

community-dashboard/app/components/Page/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function Page(props: Props) {
195195
value={props.totalUserGroups}
196196
label="Total Groups"
197197
// eslint-disable-next-line react/destructuring-assignment
198-
secondaryValue={props.totalContributorsLastMonth}
198+
secondaryValue={props.totalUserGroupsLastMonth}
199199
secondaryValueDescription="active groups in the last 30 days"
200200
/>
201201
</>

community-dashboard/app/views/StatsBoard/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
formatYear,
5959
resolveTime,
6060
getTimestamps,
61+
getDateSafe,
6162
} from '#utils/temporal';
6263
import styles from './styles.css';
6364

@@ -208,14 +209,15 @@ function StatsBoard(props: Props) {
208209
React.useEffect(
209210
() => {
210211
const timestamps = contributionTimeStats?.map(
211-
(item) => new Date(item.date).getTime(),
212+
(item) => getDateSafe(item.date).getTime(),
212213
) ?? [];
214+
213215
if (timestamps.length <= 0) {
214216
timestamps.push(new Date().getTime());
215217
}
216218

217-
const minDate = new Date(Math.min(...timestamps));
218-
const maxDate = new Date(Math.max(...timestamps));
219+
const minDate = getDateSafe(Math.min(...timestamps));
220+
const maxDate = getDateSafe(Math.max(...timestamps));
219221

220222
const minDateYear = minDate.getFullYear();
221223
const maxDateYear = maxDate.getFullYear();
@@ -325,7 +327,7 @@ function StatsBoard(props: Props) {
325327
const totalContributionByDay = useMemo(() => {
326328
const dayWiseContribution = listToGroupList(
327329
contributionTimeStats,
328-
(d) => new Date(d.date).getDay(),
330+
(d) => getDateSafe(d.date).getDay(),
329331
(d) => d.totalSwipeTime,
330332
);
331333

0 commit comments

Comments
 (0)