Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(date formatting): clean up date formatting in the app #7584

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions web/src/components/Time.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import { useAtomValue } from 'jotai';
import { useParams } from 'react-router-dom';
import { RouteParameters } from 'types';
import { TimeRange } from 'utils/constants';
import { formatDate, formatDateRange } from 'utils/formatting';
import { formatDate } from 'utils/formatting';
import { getZoneTimezone } from 'utils/helpers';
import { timeRangeAtom } from 'utils/state/atoms';

export function FormattedTime({
datetime,
language,
timeRange,
className,
zoneId,
isTimeHeader = false,
endDatetime,
}: {
datetime: Date;
language: string;
timeRange?: TimeRange;
className?: string;
zoneId?: string;
isTimeHeader?: boolean;
endDatetime?: Date;
}) {
const timeRange = useAtomValue(timeRangeAtom);
const { zoneId: pathZoneId } = useParams<RouteParameters>();
const timeZoneZoneId = zoneId || pathZoneId;
const timezone = getZoneTimezone(timeZoneZoneId);
if (timeRange) {
return (
<time dateTime={datetime.toISOString()} className={className}>
{formatDate(datetime, language, timeRange, timezone)}
</time>
);
}
return (
<time dateTime={datetime.toISOString()} className={className}>
{endDatetime && formatDateRange(datetime, endDatetime, language, timezone)}
{formatDate(datetime, language, timeRange, timezone, isTimeHeader, endDatetime)}
</time>
);
}
4 changes: 1 addition & 3 deletions web/src/components/TimeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { ZoneKey } from 'types';
import { selectedDatetimeIndexAtom, timeRangeAtom } from 'utils/state/atoms';
import { selectedDatetimeIndexAtom } from 'utils/state/atoms';

import { FormattedTime } from './Time';

Expand All @@ -13,14 +13,12 @@ export function TimeDisplay({
zoneId?: ZoneKey;
}) {
const { i18n } = useTranslation();
const timeRange = useAtomValue(timeRangeAtom);
const selectedDatetime = useAtomValue(selectedDatetimeIndexAtom);

return (
<FormattedTime
datetime={selectedDatetime.datetime}
language={i18n.languages[0]}
timeRange={timeRange}
className={className}
zoneId={zoneId}
/>
Expand Down
9 changes: 1 addition & 8 deletions web/src/features/charts/tooltips/AreaGraphTooltipHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { HorizontalDivider } from 'components/Divider';
import EstimationBadge from 'components/EstimationBadge';
import { FormattedTime } from 'components/Time';
import { useGetEstimationTranslation } from 'hooks/getEstimationTranslation';
import { useAtomValue } from 'jotai';
import { CircleDashed, TrendingUpDown } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { EstimationMethods, TimeRange } from 'utils/constants';
import { endDatetimeAtom } from 'utils/state/atoms';
import { EstimationMethods } from 'utils/constants';

import ProductionSourceIcon from '../ProductionsSourceIcons';

interface AreaGraphToolTipHeaderProps {
squareColor: string;
datetime: Date;
timeRange: TimeRange;
title: string;
hasEstimationPill?: boolean;
estimatedPercentage?: number;
Expand All @@ -24,7 +21,6 @@ interface AreaGraphToolTipHeaderProps {
export default function AreaGraphToolTipHeader({
squareColor,
datetime,
timeRange,
title,
hasEstimationPill = false,
estimatedPercentage,
Expand All @@ -37,7 +33,6 @@ export default function AreaGraphToolTipHeader({
estimationMethod,
estimatedPercentage
);
const endDatetime = useAtomValue(endDatetimeAtom);
return (
<>
<div className="flex items-center gap-1 font-bold">
Expand All @@ -60,10 +55,8 @@ export default function AreaGraphToolTipHeader({
)}
</div>
<FormattedTime
endDatetime={endDatetime}
datetime={datetime}
language={i18n.languages[0]}
timeRange={timeRange}
className="text-sm"
/>
<HorizontalDivider />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Meta, StoryObj } from '@storybook/react';
import { TimeRange } from 'utils/constants';

import { BreakdownChartTooltipContent } from './BreakdownChartTooltip';

Expand All @@ -17,7 +16,6 @@ export const Example: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 599,
timeRange: TimeRange.H24,
capacity: 500,
co2Intensity: 130,
co2IntensitySource: 'IPCC 2014; EU-ETS, ENTSO-E 2021',
Expand All @@ -35,7 +33,6 @@ export const WithoutCapacity: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 599,
timeRange: TimeRange.H24,
capacity: undefined,
co2Intensity: 130,
co2IntensitySource: 'IPCC 2014; EU-ETS, ENTSO-E 2021',
Expand All @@ -53,7 +50,6 @@ export const isStoring: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 80,
timeRange: TimeRange.H24,
capacity: 700,
co2Intensity: 130,
co2IntensitySource: 'IPCC 2014; EU-ETS, ENTSO-E 2021',
Expand All @@ -75,7 +71,6 @@ export const IsDischarging: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 80,
timeRange: TimeRange.H24,
capacity: 700,
co2Intensity: 130,
co2IntensitySource: 'IPCC 2014; EU-ETS, ENTSO-E 2021',
Expand All @@ -95,7 +90,6 @@ export const LongSource: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 80,
timeRange: TimeRange.H24,
capacity: 700,
co2Intensity: 130,
co2IntensitySource:
Expand All @@ -115,7 +109,6 @@ export const Exporting: Story = {
args: {
datetime: new Date('2022-11-28T07:00:00.000Z'),
usage: 80,
timeRange: TimeRange.H24,
capacity: 700,
co2Intensity: 450,
co2IntensitySource:
Expand Down
8 changes: 1 addition & 7 deletions web/src/features/charts/tooltips/BreakdownChartTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { getZoneName } from 'translation/translation';
import { ElectricityModeType, Maybe, ZoneDetail } from 'types';
import { EstimationMethods, modeColor, TimeRange } from 'utils/constants';
import { EstimationMethods, modeColor } from 'utils/constants';
import { formatCo2, formatEnergy, formatPower } from 'utils/formatting';
import { round } from 'utils/helpers';
import {
displayByEmissionsAtom,
isConsumptionAtom,
isHourlyAtom,
timeRangeAtom,
} from 'utils/state/atoms';

import { getGenerationTypeKey, getRatioPercent } from '../graphUtils';
Expand Down Expand Up @@ -107,7 +106,6 @@ export default function BreakdownChartTooltip({
selectedLayerKey,
}: InnerAreaGraphTooltipProps) {
const displayByEmissions = useAtomValue(displayByEmissionsAtom);
const timeRange = useAtomValue(timeRangeAtom);
const isConsumption = useAtomValue(isConsumptionAtom);

if (!zoneDetail || !selectedLayerKey) {
Expand Down Expand Up @@ -135,7 +133,6 @@ export default function BreakdownChartTooltip({
datetime={new Date(stateDatetime)}
isExchange={isExchange}
selectedLayerKey={selectedLayerKey}
timeRange={timeRange}
hasEstimationPill={hasEstimationPill}
estimatedPercentage={roundedEstimatedPercentage}
estimationMethod={estimationMethod}
Expand All @@ -150,7 +147,6 @@ interface BreakdownChartTooltipContentProperties {
totalElectricity: number;
totalEmissions: number;
co2Intensity: number;
timeRange: TimeRange;
displayByEmissions: boolean;
emissions: number;
zoneKey: string;
Expand All @@ -171,7 +167,6 @@ export function BreakdownChartTooltipContent({
usage,
totalElectricity,
displayByEmissions,
timeRange,
capacity,
emissions,
isExport,
Expand Down Expand Up @@ -206,7 +201,6 @@ export function BreakdownChartTooltipContent({
: modeColor[selectedLayerKey as ElectricityModeType]
}
datetime={datetime}
timeRange={timeRange}
title={title}
hasEstimationPill={isExchange ? false : hasEstimationPill}
estimatedPercentage={estimatedPercentage}
Expand Down
4 changes: 1 addition & 3 deletions web/src/features/charts/tooltips/CarbonChartTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { useCo2ColorScale } from 'hooks/theme';
import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { getCarbonIntensity, round } from 'utils/helpers';
import { isConsumptionAtom, timeRangeAtom } from 'utils/state/atoms';
import { isConsumptionAtom } from 'utils/state/atoms';

import { InnerAreaGraphTooltipProps } from '../types';
import AreaGraphToolTipHeader from './AreaGraphTooltipHeader';

export default function CarbonChartTooltip({ zoneDetail }: InnerAreaGraphTooltipProps) {
const timeRange = useAtomValue(timeRangeAtom);
const { t } = useTranslation();
const isConsumption = useAtomValue(isConsumptionAtom);
const co2ColorScale = useCo2ColorScale();
Expand Down Expand Up @@ -39,7 +38,6 @@ export default function CarbonChartTooltip({ zoneDetail }: InnerAreaGraphTooltip
>
<AreaGraphToolTipHeader
datetime={new Date(stateDatetime)}
timeRange={timeRange}
squareColor={co2ColorScale(intensity)}
title={t('tooltips.carbonintensity')}
hasEstimationPill={hasEstimationPill}
Expand Down
4 changes: 1 addition & 3 deletions web/src/features/charts/tooltips/EmissionChartTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { formatCo2 } from 'utils/formatting';
import { round } from 'utils/helpers';
import { isConsumptionAtom, timeRangeAtom } from 'utils/state/atoms';
import { isConsumptionAtom } from 'utils/state/atoms';

import { getTotalEmissionsAvailable } from '../graphUtils';
import { InnerAreaGraphTooltipProps } from '../types';
import AreaGraphToolTipHeader from './AreaGraphTooltipHeader';

export default function EmissionChartTooltip({ zoneDetail }: InnerAreaGraphTooltipProps) {
const timeRange = useAtomValue(timeRangeAtom);
const isConsumption = useAtomValue(isConsumptionAtom);
const { t } = useTranslation();

Expand All @@ -27,7 +26,6 @@ export default function EmissionChartTooltip({ zoneDetail }: InnerAreaGraphToolt
<div className="w-full rounded-md bg-white p-3 shadow-xl dark:border dark:border-gray-700 dark:bg-gray-800 sm:w-[410px]">
<AreaGraphToolTipHeader
datetime={new Date(stateDatetime)}
timeRange={timeRange}
squareColor="#a5292a"
title={t('country-panel.emissions')}
hasEstimationPill={hasEstimationPill}
Expand Down
4 changes: 1 addition & 3 deletions web/src/features/charts/tooltips/NetExchangeChartTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { formatCo2, scalePower } from 'utils/formatting';
import { getNetExchange, round } from 'utils/helpers';
import { displayByEmissionsAtom, isHourlyAtom, timeRangeAtom } from 'utils/state/atoms';
import { displayByEmissionsAtom, isHourlyAtom } from 'utils/state/atoms';

import { InnerAreaGraphTooltipProps } from '../types';
import AreaGraphToolTipHeader from './AreaGraphTooltipHeader';

export default function NetExchangeChartTooltip({
zoneDetail,
}: InnerAreaGraphTooltipProps) {
const timeRange = useAtomValue(timeRangeAtom);
const displayByEmissions = useAtomValue(displayByEmissionsAtom);
const isHourly = useAtomValue(isHourlyAtom);
const { t } = useTranslation();
Expand All @@ -33,7 +32,6 @@ export default function NetExchangeChartTooltip({
<div className="w-full rounded-md bg-white p-3 shadow-xl dark:border dark:border-gray-700 dark:bg-gray-800 sm:w-[350px]">
<AreaGraphToolTipHeader
datetime={new Date(stateDatetime)}
timeRange={timeRange}
squareColor="#7f7f7f"
title={t('tooltips.netExchange')}
/>
Expand Down
4 changes: 0 additions & 4 deletions web/src/features/charts/tooltips/PriceChartTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import getSymbolFromCurrency from 'currency-symbol-map';
import { useAtomValue } from 'jotai';
import { useTranslation } from 'react-i18next';
import { timeRangeAtom } from 'utils/state/atoms';

import { convertPrice } from '../bar-breakdown/utils';
import { InnerAreaGraphTooltipProps } from '../types';
import AreaGraphToolTipHeader from './AreaGraphTooltipHeader';

export default function PriceChartTooltip({ zoneDetail }: InnerAreaGraphTooltipProps) {
const timeRange = useAtomValue(timeRangeAtom);
const { t } = useTranslation();

if (!zoneDetail) {
Expand All @@ -26,7 +23,6 @@ export default function PriceChartTooltip({ zoneDetail }: InnerAreaGraphTooltipP
<div className="w-full rounded-md bg-white p-3 shadow-xl dark:border dark:border-gray-700 dark:bg-gray-800 sm:w-64">
<AreaGraphToolTipHeader
datetime={new Date(stateDatetime)}
timeRange={timeRange}
squareColor="#7f7f7f" // TODO: use price scale color
title={t('tooltips.price')}
/>
Expand Down
Loading
Loading