Skip to content

Commit

Permalink
feat(client): Add chart sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprdhomme committed Nov 27, 2024
1 parent 679183b commit 512cab2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
30 changes: 30 additions & 0 deletions client/src/components/dataset-card/chart-sentence.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GeoJsonProperties } from "geojson";
import { useMemo } from "react";

interface ChartSentenceprops {
sentence: string;
feature?: GeoJsonProperties;
}

const ChartSentence = ({ sentence, feature }: ChartSentenceprops) => {
const resolvedSentence = useMemo(() => {
let res = `${sentence}`; // Creating a copy

if (!!feature) {
Object.entries(feature).forEach(([key, value]) => {
res = res.replace(`{${key}}`, value);
});
}

return res;
}, [sentence, feature]);

return (
<div className="flex items-start justify-start gap-4 pl-8 text-xs">
{!!feature && <div className="shrink-0 font-medium">Selected point</div>}
<div>{resolvedSentence}</div>
</div>
);
};

export default ChartSentence;
28 changes: 25 additions & 3 deletions client/src/components/dataset-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import useYearChartData from "@/hooks/use-year-chart-data";
import CursorArrowRaysIcon from "@/svgs/cursor-arrow-rays.svg";
import { DatasetLayersDataItem, MetadataItemComponent } from "@/types/generated/strapi.schemas";

import ChartSentence from "./chart-sentence";
import DateControls from "./date-controls";
import DownloadChartButton from "./download-chart-button";
import DownloadLayerButton from "./download-layer-button";
Expand Down Expand Up @@ -194,6 +195,16 @@ const DatasetCard = ({
return `${name}${!showChartOnInteraction ? ` - ${locationData![0].name}` : ""}.json`;
}, [isChartDownloadDisabled, locationData, name, showChartOnInteraction]);

const isInteractionChartVisible = useMemo(
() => showChartOnInteraction && selectedLayer !== undefined && !!selectedFeature,
[selectedFeature, selectedLayer, showChartOnInteraction],
);

const isYearChartVisible = useMemo(
() => selectedDate !== undefined && selectedLayerId !== undefined,
[selectedDate, selectedLayerId],
);

const onToggleDataset = useCallback(
(active: boolean) => {
if (selectedLayerId === undefined) {
Expand Down Expand Up @@ -361,21 +372,32 @@ const DatasetCard = ({
Select a point on the map for details.
</div>
)}
{showChartOnInteraction && selectedLayer !== undefined && !!selectedFeature && (
{isInteractionChartVisible && (
<div className="mt-3">
<InteractionChart data={interactionChartData} loading={interactionChartIsLoading} />
</div>
)}
{selectedDate !== undefined && selectedLayerId !== undefined && (
{isYearChartVisible && (
<div className="mt-3">
<YearChart
data={yearChartData}
date={selectedDate}
date={selectedDate!}
loading={yearChartIsLoading}
active={isDatasetActive}
/>
</div>
)}
{isDatasetActive &&
(isInteractionChartVisible || isYearChartVisible) &&
selectedLayer !== undefined &&
!!selectedLayer.attributes!.chart_sentence && (
<div className="mt-0.5">
<ChartSentence
sentence={selectedLayer.attributes!.chart_sentence}
feature={selectedFeature}
/>
</div>
)}
{isDatasetActive && selectedLayer !== undefined && selectedDate !== undefined && (
<DateControls layer={selectedLayer} date={selectedDate} onChangeDate={onChangeDate} />
)}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/panels/drought/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useDatasetsBySubTopic from "@/hooks/use-datasets-by-sub-topic";
const DroughtPanel = () => {
const { data, isLoading } = useDatasetsBySubTopic(
"drought",
["name", "params_config", "download_link", "show_chart_on_interaction"],
["name", "params_config", "download_link", "show_chart_on_interaction", "chart_sentence"],
true,
);

Expand Down
2 changes: 1 addition & 1 deletion client/src/components/panels/flood/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useDatasetsBySubTopic from "@/hooks/use-datasets-by-sub-topic";
const FloodPanel = () => {
const { data, isLoading } = useDatasetsBySubTopic(
"flood",
["name", "params_config", "download_link", "show_chart_on_interaction"],
["name", "params_config", "download_link", "show_chart_on_interaction", "chart_sentence"],
true,
);

Expand Down
2 changes: 1 addition & 1 deletion client/src/components/panels/hydrometeorological/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import HydrometeorologicalImage from "../../../../public/assets/images/hydromete
const HydrometeorologicalPanel = () => {
const { data, isLoading } = useDatasetsBySubTopic(
"hydrometeorological",
["name", "params_config", "download_link", "show_chart_on_interaction"],
["name", "params_config", "download_link", "show_chart_on_interaction", "chart_sentence"],
true,
);

Expand Down

0 comments on commit 512cab2

Please sign in to comment.