From de8056b362b467732c82b5654987ff40a2bb30b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Prod=27homme?= Date: Wed, 13 Nov 2024 13:53:49 +0100 Subject: [PATCH] feat(client): Allow playing layer animation --- client/src/components/dataset-card/index.tsx | 137 +++++++++++++------ client/src/svgs/pause.svg | 4 + client/src/svgs/play.svg | 4 + client/tailwind.config.ts | 1 + 4 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 client/src/svgs/pause.svg create mode 100644 client/src/svgs/play.svg diff --git a/client/src/components/dataset-card/index.tsx b/client/src/components/dataset-card/index.tsx index 5eb525e..fe47819 100644 --- a/client/src/components/dataset-card/index.tsx +++ b/client/src/components/dataset-card/index.tsx @@ -1,8 +1,9 @@ "use client"; +import { getMonth } from "date-fns"; import { format } from "date-fns/format"; import Link from "next/link"; -import { useCallback, useMemo, useState } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import * as React from "react"; import { Button } from "@/components/ui/button"; @@ -22,6 +23,8 @@ import { cn } from "@/lib/utils"; import CalendarDaysIcon from "@/svgs/calendar-days.svg"; import ChevronDownIcon from "@/svgs/chevron-down.svg"; import DownloadIcon from "@/svgs/download.svg"; +import PauseIcon from "@/svgs/pause.svg"; +import PlayIcon from "@/svgs/play.svg"; import { DatasetLayersDataItem } from "@/types/generated/strapi.schemas"; import { LayerParamsConfig } from "@/types/layer"; @@ -60,6 +63,10 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) => const [selectedLayerId, setSelectedLayerId] = useState(defaultSelectedLayerId); const [selectedReturnPeriod, setSelectedReturnPeriod] = useState(defaultSelectedReturnPeriod); const [selectedDate, setSelectedDate] = useState(defaultSelectedDate); + const [isAnimated, setIsAnimated] = useState(false); + const animationIntervalRef = useRef(null); + // Date that was selected before the animation is played + const dateBeforeAnimationRef = useRef(null); const selectedLayer = useMemo( () => layers.find(({ id }) => id === selectedLayerId), @@ -168,17 +175,44 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) => addLayer(selectedLayerId, { ["return-period"]: returnPeriod, date }); } }, - [ - selectedLayerId, - setSelectedReturnPeriod, - isDatasetActive, - addLayer, - updateLayer, - layers, - layersConfiguration, - ], + [selectedLayerId, isDatasetActive, addLayer, updateLayer, layers, layersConfiguration], ); + const onToggleAnimation = useCallback(() => { + const newIsAnimated = !isAnimated; + + if (newIsAnimated) { + dateBeforeAnimationRef.current = selectedDate !== undefined ? selectedDate : null; + } else { + dateBeforeAnimationRef.current = null; + } + + setIsAnimated(newIsAnimated); + }, [selectedDate, isAnimated, setIsAnimated]); + + // When the layer is animated, show each month of the year in a loop + useEffect(() => { + if (isAnimated && selectedDate !== undefined && selectedLayerId !== undefined) { + animationIntervalRef.current = setInterval(() => { + const date = format( + new Date(selectedDate).setMonth((getMonth(selectedDate) + 1) % 12), + "yyyy-MM-dd", + ); + + setSelectedDate(date); + updateLayer(selectedLayerId, { date }); + }, 500); + } else if (animationIntervalRef.current !== null) { + clearInterval(animationIntervalRef.current); + } + + return () => { + if (animationIntervalRef.current !== null) { + clearInterval(animationIntervalRef.current); + } + }; + }, [selectedLayerId, selectedDate, isAnimated, setSelectedDate, updateLayer]); + return (
@@ -245,39 +279,58 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) => )} {selectedDate !== undefined && dateRange !== undefined && isDatasetActive && (
- - - - - - + Play layer animation + {!isAnimated && } + {isAnimated && } + +
+ + + + + + + + + +
)}
diff --git a/client/src/svgs/pause.svg b/client/src/svgs/pause.svg new file mode 100644 index 0000000..fec67e2 --- /dev/null +++ b/client/src/svgs/pause.svg @@ -0,0 +1,4 @@ + + + diff --git a/client/src/svgs/play.svg b/client/src/svgs/play.svg new file mode 100644 index 0000000..b089e58 --- /dev/null +++ b/client/src/svgs/play.svg @@ -0,0 +1,4 @@ + + + diff --git a/client/tailwind.config.ts b/client/tailwind.config.ts index cee6d33..829ca64 100644 --- a/client/tailwind.config.ts +++ b/client/tailwind.config.ts @@ -31,6 +31,7 @@ const config: Config = { "50": "#f3f5fb", "400": "#86a0d4", "500": "#6982c8", + "800": "#424B8B", "900": "#38406e", "950": "#262a45", },