From 627fdf2a7d04121294e75ac97006df16246a1efd Mon Sep 17 00:00:00 2001 From: malmen237 Date: Thu, 11 Apr 2024 10:04:13 +0200 Subject: [PATCH] fix: refresh-animation as a separate hook --- .../landing-page/use-refresh-animation.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/components/landing-page/use-refresh-animation.ts diff --git a/src/components/landing-page/use-refresh-animation.ts b/src/components/landing-page/use-refresh-animation.ts new file mode 100644 index 00000000..0d61ee2c --- /dev/null +++ b/src/components/landing-page/use-refresh-animation.ts @@ -0,0 +1,35 @@ +import { useEffect, useState } from "react"; + +type TUseRefreshAnimationOptions = { + reloadProductionList: boolean; +}; + +export const useRefreshAnimation = ({ + reloadProductionList, +}: TUseRefreshAnimationOptions) => { + const [showRefreshing, setShowRefreshing] = useState(true); + + useEffect(() => { + let timeout: number | null = null; + + if (showRefreshing) { + timeout = window.setTimeout(() => { + setShowRefreshing(false); + }, 1500); + } + + return () => { + if (timeout !== null) { + window.clearTimeout(timeout); + } + }; + }, [showRefreshing]); + + useEffect(() => { + if (reloadProductionList) { + setShowRefreshing(true); + } + }, [reloadProductionList]); + + return showRefreshing; +};