From 1ae738d048785ceb4acb2646d7129307598a5a52 Mon Sep 17 00:00:00 2001 From: Cadence Banulis Date: Fri, 18 Oct 2024 10:29:40 +0200 Subject: [PATCH] feat: add scroll to chart functionality --- web/src/features/charts/BreakdownChart.tsx | 4 +++- web/src/features/charts/CarbonChart.tsx | 10 +++++++-- web/src/features/charts/ChartTitle.tsx | 7 +++++-- web/src/features/charts/EmissionChart.tsx | 3 ++- web/src/features/charts/NetExchangeChart.tsx | 8 +++++-- .../features/charts/NotEnoughDataMessage.tsx | 4 ++-- web/src/features/charts/PriceChart.tsx | 10 +++++++-- .../bar-breakdown/BarBreakdownChart.tsx | 5 +++-- .../bar-breakdown/elements/BySource.tsx | 4 +++- web/src/features/panels/zone/ZoneDetails.tsx | 21 +++++++++++++++++-- web/src/utils/constants.ts | 9 ++++++++ 11 files changed, 68 insertions(+), 17 deletions(-) diff --git a/web/src/features/charts/BreakdownChart.tsx b/web/src/features/charts/BreakdownChart.tsx index 3972394b5a..8c7bb1712f 100644 --- a/web/src/features/charts/BreakdownChart.tsx +++ b/web/src/features/charts/BreakdownChart.tsx @@ -7,7 +7,7 @@ import { Factory, Zap } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { ElectricityModeType } from 'types'; import trackEvent from 'utils/analytics'; -import { TimeAverages, TrackEvent } from 'utils/constants'; +import { Charts, TimeAverages, TrackEvent } from 'utils/constants'; import { formatCo2 } from 'utils/formatting'; import { dataSourcesCollapsedBreakdownAtom, @@ -76,6 +76,7 @@ function BreakdownChart({ if (!hasEnoughDataToDisplay) { return ( ); @@ -87,6 +88,7 @@ function BreakdownChart({ translationKey={`country-history.${titleDisplayMode}${titleMixMode}`} badge={badge} unit={valueAxisLabel} + id={Charts.ORIGIN_CHART} />
; if (!hasEnoughDataToDisplay) { - return ; + return ( + + ); } return ( @@ -58,6 +63,7 @@ function CarbonChart({ datetimes, timeAverage }: CarbonChartProps) { translationKey="country-history.carbonintensity" badge={badge} unit={'gCO₂eq / kWh'} + id={Charts.CARBON_CHART} />
-

{t(`${translationKey}.${timeAverage}`)}

+

+ {t(`${translationKey}.${timeAverage}`)} +

{badge}
{unit &&
{unit}
} diff --git a/web/src/features/charts/EmissionChart.tsx b/web/src/features/charts/EmissionChart.tsx index 48d13741e4..644d8a158d 100644 --- a/web/src/features/charts/EmissionChart.tsx +++ b/web/src/features/charts/EmissionChart.tsx @@ -5,7 +5,7 @@ import { useAtom } from 'jotai'; import { Factory, Zap } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import trackEvent from 'utils/analytics'; -import { TimeAverages, TrackEvent } from 'utils/constants'; +import { Charts, TimeAverages, TrackEvent } from 'utils/constants'; import { formatCo2 } from 'utils/formatting'; import { dataSourcesCollapsedEmissionAtom } from 'utils/state/atoms'; @@ -52,6 +52,7 @@ function EmissionChart({ timeAverage, datetimes }: EmissionChartProps) { translationKey="country-history.emissions" badge={badge} unit={'CO₂eq'} + id={Charts.EMISSION_CHART} /> - +
- +

{t('country-history.not-enough-data')}

diff --git a/web/src/features/charts/PriceChart.tsx b/web/src/features/charts/PriceChart.tsx index ad0466fd8e..2f472c7fba 100644 --- a/web/src/features/charts/PriceChart.tsx +++ b/web/src/features/charts/PriceChart.tsx @@ -1,5 +1,5 @@ import { useTranslation } from 'react-i18next'; -import { TimeAverages } from 'utils/constants'; +import { Charts, TimeAverages } from 'utils/constants'; import { ChartTitle } from './ChartTitle'; import { DisabledMessage } from './DisabledMessage'; @@ -51,7 +51,12 @@ function PriceChart({ datetimes, timeAverage }: PriceChartProps) { const hasEnoughDataToDisplay = datetimes?.length > 2; if (!hasEnoughDataToDisplay) { - return ; + return ( + + ); } return ( @@ -59,6 +64,7 @@ function PriceChart({ datetimes, timeAverage }: PriceChartProps) {
{isPriceDisabled && ( diff --git a/web/src/features/charts/bar-breakdown/BarBreakdownChart.tsx b/web/src/features/charts/bar-breakdown/BarBreakdownChart.tsx index 42e92508c5..d22e76c08f 100644 --- a/web/src/features/charts/bar-breakdown/BarBreakdownChart.tsx +++ b/web/src/features/charts/bar-breakdown/BarBreakdownChart.tsx @@ -10,7 +10,7 @@ import { useTranslation } from 'react-i18next'; import { ElectricityModeType, ZoneKey } from 'types'; import useResizeObserver from 'use-resize-observer'; import trackEvent from 'utils/analytics'; -import { TrackEvent } from 'utils/constants'; +import { Charts, TrackEvent } from 'utils/constants'; import { dataSourcesCollapsedBarBreakdownAtom, displayByEmissionsAtom, @@ -88,7 +88,7 @@ function BarBreakdownChart({ if (!currentZoneDetail) { return (
- + {!displayByEmissions && ( diff --git a/web/src/features/charts/bar-breakdown/elements/BySource.tsx b/web/src/features/charts/bar-breakdown/elements/BySource.tsx index b6a945de75..569b8c6f7a 100644 --- a/web/src/features/charts/bar-breakdown/elements/BySource.tsx +++ b/web/src/features/charts/bar-breakdown/elements/BySource.tsx @@ -38,12 +38,14 @@ export default function BySource({ estimatedPercentage, unit, estimationMethod, + id, }: { className?: string; hasEstimationPill?: boolean; estimatedPercentage?: number; unit?: string | number; estimationMethod?: EstimationMethods; + id: string; }) { const { t } = useTranslation(); const [timeAverage] = useAtom(timeAverageAtom); @@ -64,7 +66,7 @@ export default function BySource({ className={`text-md relative flex flex-row justify-between font-bold ${className}`} >
-

{text}

+

{text}

{hasEstimationPill && ( ; } @@ -190,3 +192,18 @@ function ZoneDetailsContent({ return children as JSX.Element; } + +const useScrollHashIntoView = (isLoading: boolean) => { + const { hash, pathname, search } = useLocation(); + const navigate = useNavigate(); + useEffect(() => { + const hashElement = hash ? document.querySelector(hash) : null; + if (!isLoading && hashElement) { + hashElement.scrollIntoView({ + behavior: 'smooth', + inline: 'nearest', + }); + } + navigate(`${pathname}${search}`); + }, [hash, isLoading, navigate, pathname, search]); +}; diff --git a/web/src/utils/constants.ts b/web/src/utils/constants.ts index ff5ba9466b..2c0535c7a8 100644 --- a/web/src/utils/constants.ts +++ b/web/src/utils/constants.ts @@ -38,6 +38,15 @@ export enum LeftPanelToggleOptions { EMISSIONS = 'emissions', } +export enum Charts { + ELECTRICITY_PRICES_CHART = 'electricity_prices_chart', + ORIGIN_CHART = 'origin_chart', + BAR_BREAKDOWN_CHART = 'bar_breakdown_chart', + CARBON_CHART = 'carbon_chart', + EMISSION_CHART = 'emission_chart', + NET_EXCHANGE_CHART = 'net_exchange_chart', +} + export enum TrackEvent { DATA_SOURCES_CLICKED = 'Data Sources Clicked', APP_BANNER_CTA_CLICKED = 'App Banner CTA Clicked',