aboutsummaryrefslogtreecommitdiff
path: root/lib/hooks
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-12-24 13:03:54 +0700
committerFactiven <[email protected]>2023-12-24 13:03:54 +0700
commit50a0f0240d7fef133eb5acc1bea2b1168b08e9db (patch)
tree307e09e505580415a58d64b5fc3580e9235869f1 /lib/hooks
parentUpdate README.md (#104) (diff)
downloadmoopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.tar.xz
moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.zip
migrate to typescript
Diffstat (limited to 'lib/hooks')
-rw-r--r--lib/hooks/useCountdownSeconds.ts54
-rw-r--r--lib/hooks/useWatchStorage.tsx28
2 files changed, 82 insertions, 0 deletions
diff --git a/lib/hooks/useCountdownSeconds.ts b/lib/hooks/useCountdownSeconds.ts
new file mode 100644
index 0000000..3d17ede
--- /dev/null
+++ b/lib/hooks/useCountdownSeconds.ts
@@ -0,0 +1,54 @@
+import { useEffect, useState } from "react";
+
+interface CountdownValues {
+ days: number;
+ hours: number;
+ minutes: number;
+ seconds: number;
+}
+
+interface Props {
+ targetDate: number;
+ update: Function;
+ countdown: CountdownValues;
+}
+
+const useCountdown = (targetDate: number, update: Function): Props => {
+ const countDownDate = new Date(targetDate).getTime();
+
+ const [countDown, setCountDown] = useState(
+ countDownDate - new Date().getTime()
+ );
+
+ useEffect(() => {
+ const interval = setInterval(() => {
+ const newCountDown = countDownDate - new Date().getTime();
+ setCountDown(newCountDown);
+ if (newCountDown <= 0 && newCountDown > -1000) {
+ update();
+ }
+ }, 1000);
+
+ return () => clearInterval(interval);
+ }, [countDownDate, update]);
+
+ return {
+ targetDate,
+ update,
+ countdown: getReturnValues(countDown),
+ };
+};
+
+const getReturnValues = (countDown: number): CountdownValues => {
+ // calculate time left
+ const days = Math.floor(countDown / (1000 * 60 * 60 * 24));
+ const hours = Math.floor(
+ (countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
+ );
+ const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));
+ const seconds = Math.floor((countDown % (1000 * 60)) / 1000);
+
+ return { days, hours, minutes, seconds };
+};
+
+export { useCountdown };
diff --git a/lib/hooks/useWatchStorage.tsx b/lib/hooks/useWatchStorage.tsx
new file mode 100644
index 0000000..ee24a39
--- /dev/null
+++ b/lib/hooks/useWatchStorage.tsx
@@ -0,0 +1,28 @@
+import { UserData } from "@/components/watch/new-player/player";
+import { useState } from "react";
+
+function useWatchStorage() {
+ // Get initial value from local storage or empty object
+ const [settings, setSettings] = useState(() => {
+ const storedSettings = localStorage?.getItem("artplayer_settings");
+ return storedSettings ? JSON.parse(storedSettings) : {};
+ });
+
+ const getSettings = (id: string): UserData | undefined => {
+ return settings[id];
+ };
+
+ // Function to update settings
+ const updateSettings = (id: string, data?: any) => {
+ // Update state
+ const updatedSettings = { ...settings, [id]: data };
+ setSettings(updatedSettings);
+
+ // Update local storage
+ localStorage.setItem("artplayer_settings", JSON.stringify(updatedSettings));
+ };
+
+ return [getSettings, updateSettings];
+}
+
+export default useWatchStorage;