Skip to content

Commit

Permalink
chore: remove some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Dec 16, 2024
1 parent 4862b65 commit f47117f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/ui/UserSwitchWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Props = {

export function UserSwitchWrapper({
ButtonContent,
}: Props): JSX.Element | null {
}: Readonly<Props>): JSX.Element | null {
const { i18n } = useTranslation(NS.Account);
const { isAuthenticated, user, logout } = useAuth();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Props = {
};
export function MemberGeneralStatisticsCards({
actionsGroupedByTypes,
}: Props): JSX.Element {
}: Readonly<Props>): JSX.Element {
const { t } = useTranslation(NS.Analytics);

return (
Expand Down
34 changes: 21 additions & 13 deletions src/modules/analytics/context/MyAnalyticsDateRangeContext.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
}: {
Expand All @@ -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(
() => ({
Expand Down
4 changes: 3 additions & 1 deletion src/modules/analytics/layout/AnalyticsSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/analytics/layout/Navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f47117f

Please sign in to comment.