From f47117fcc4a4c800938da4c6e734db6a0be4df84 Mon Sep 17 00:00:00 2001 From: spaenleh Date: Mon, 16 Dec 2024 15:24:16 +0100 Subject: [PATCH] chore: remove some code smells --- src/components/ui/UserSwitchWrapper.tsx | 2 +- .../MemberGeneralStatisticsCards.tsx | 2 +- .../context/MyAnalyticsDateRangeContext.tsx | 34 ++++++++++++------- .../analytics/layout/AnalyticsSidebar.tsx | 4 ++- src/modules/analytics/layout/Navigator.tsx | 4 ++- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/components/ui/UserSwitchWrapper.tsx b/src/components/ui/UserSwitchWrapper.tsx index 7f54c3245..0fbdf4fb3 100644 --- a/src/components/ui/UserSwitchWrapper.tsx +++ b/src/components/ui/UserSwitchWrapper.tsx @@ -16,7 +16,7 @@ type Props = { export function UserSwitchWrapper({ ButtonContent, -}: Props): JSX.Element | null { +}: Readonly): JSX.Element | null { const { i18n } = useTranslation(NS.Account); const { isAuthenticated, user, logout } = useAuth(); diff --git a/src/modules/account/home/memberAnalytics/MemberGeneralStatisticsCards.tsx b/src/modules/account/home/memberAnalytics/MemberGeneralStatisticsCards.tsx index 2aaa09ee8..fdba0b3bf 100644 --- a/src/modules/account/home/memberAnalytics/MemberGeneralStatisticsCards.tsx +++ b/src/modules/account/home/memberAnalytics/MemberGeneralStatisticsCards.tsx @@ -45,7 +45,7 @@ type Props = { }; export function MemberGeneralStatisticsCards({ actionsGroupedByTypes, -}: Props): JSX.Element { +}: Readonly): JSX.Element { const { t } = useTranslation(NS.Analytics); return ( diff --git a/src/modules/analytics/context/MyAnalyticsDateRangeContext.tsx b/src/modules/analytics/context/MyAnalyticsDateRangeContext.tsx index 1e97fb2b1..d18a1e800 100644 --- a/src/modules/analytics/context/MyAnalyticsDateRangeContext.tsx +++ b/src/modules/analytics/context/MyAnalyticsDateRangeContext.tsx @@ -1,6 +1,7 @@ import { createContext, useMemo, useState } from 'react'; import { addDays, intervalToDuration } from 'date-fns'; +import type { Duration } from 'date-fns'; import { DateRange, GroupByInterval } from '~analytics/config/type'; @@ -22,6 +23,20 @@ const defaultValue: { export const MyAnalyticsDateRangeDataContext = createContext(defaultValue); +function getGroupInterval(duration: Duration) { + if (duration.years && duration.years >= 1) { + return GroupByInterval.Year; + } + if (duration.months && duration.months > 2) { + return GroupByInterval.Month; + } + if (duration.days && duration.days < 8) { + return GroupByInterval.Day; + } else { + return GroupByInterval.Week; + } +} + const MyAnalyticsDateRangeProvider = ({ children, }: { @@ -33,19 +48,12 @@ const MyAnalyticsDateRangeProvider = ({ key: 'selection', }); - const { months, days, years } = intervalToDuration({ - start: dateRange.startDate, - end: dateRange.endDate, - }); - - const groupInterval = - years && years >= 1 - ? GroupByInterval.Year - : months && months > 2 - ? GroupByInterval.Month - : days && days < 8 - ? GroupByInterval.Day - : GroupByInterval.Week; + const groupInterval = getGroupInterval( + intervalToDuration({ + start: dateRange.startDate, + end: dateRange.endDate, + }), + ); const value = useMemo( () => ({ diff --git a/src/modules/analytics/layout/AnalyticsSidebar.tsx b/src/modules/analytics/layout/AnalyticsSidebar.tsx index 917b14473..a5ccc5a40 100644 --- a/src/modules/analytics/layout/AnalyticsSidebar.tsx +++ b/src/modules/analytics/layout/AnalyticsSidebar.tsx @@ -24,7 +24,9 @@ import { } from '~analytics/config/selectors'; import { DataContext } from '~analytics/context/DataProvider'; -export function AnalyticsSidebar({ itemId }: { itemId: string }): JSX.Element { +export function AnalyticsSidebar({ + itemId, +}: Readonly<{ itemId: string }>): JSX.Element { const { t } = useTranslation(NS.Analytics); const { descendantApps } = useContext(DataContext); const { data: item } = hooks.useItem(itemId); diff --git a/src/modules/analytics/layout/Navigator.tsx b/src/modules/analytics/layout/Navigator.tsx index 60e2a06f1..d49fdcb8d 100644 --- a/src/modules/analytics/layout/Navigator.tsx +++ b/src/modules/analytics/layout/Navigator.tsx @@ -18,7 +18,9 @@ import { const { useItem, useParents, useCurrentMember, useChildren } = hooks; -export function Navigator({ itemId }: { itemId: string }): JSX.Element | null { +export function Navigator({ + itemId, +}: Readonly<{ itemId: string }>): JSX.Element | null { const { data: currentMember } = useCurrentMember(); const { data: item, isLoading: isItemLoading } = useItem(itemId);