@thibault.sh/hooks
Version:
A comprehensive collection of React hooks for browser storage, UI interactions, and more
1 lines • 3.04 kB
Source Map (JSON)
{"version":3,"sources":["../src/hooks/useCountdown.ts"],"names":["useCountdown","countDownDate","refreshRate","now","firstVal","countDown","setCountDown","useState","intervalRef","useRef","useEffect","updateCountdown","getReturnValues","days","hours","minutes","seconds","useCountdown_default"],"mappings":"wCAEA,IAAMA,CAAe,CAAA,CAACC,EAAuBC,CAAyB,GAAA,CACpE,IAAMC,CAAAA,CAAM,IAAI,IAAK,EAAA,CAAE,OAAQ,EAAA,CACzBC,EAAWH,CAAgBE,CAAAA,CAAAA,CAC3B,CAACE,CAAAA,CAAWC,CAAY,CAAIC,CAAAA,cAAAA,CAASH,CAAQ,CAAA,CAC7CI,EAAcC,YAAmB,CAAA,IAAI,CAE3C,CAAA,OAAAC,gBAAU,IAAM,CACd,IAAMC,CAAAA,CAAkB,IAAM,CAC5BL,CAAAA,CAAaL,CAAgB,CAAA,IAAI,MAAO,CAAA,OAAA,EAAS,EACnD,EAEA,OAAAU,CAAAA,EACAH,CAAAA,CAAAA,CAAY,QAAU,WAAYG,CAAAA,CAAAA,CAAiBT,CAAW,CAAA,CAEvD,IAAM,CACPM,CAAAA,CAAY,OACd,EAAA,aAAA,CAAcA,EAAY,OAAO,EAErC,CACF,CAAA,CAAG,CAACP,CAAeC,CAAAA,CAAW,CAAC,CAExBU,CAAAA,CAAAA,CAAgBP,CAAS,CAClC,CAAA,CAEMO,CAAmBP,CAAAA,CAAAA,EAAsB,CAE7C,IAAMQ,CAAAA,CAAO,IAAK,CAAA,KAAA,CAAMR,EAAa,KAAoB,CAAA,CACnDS,CAAQ,CAAA,IAAA,CAAK,MAAOT,CAAa,EAAA,GAAA,CAAO,EAAK,CAAA,EAAA,CAAK,KAAQ,GAAO,CAAA,EAAA,CAAK,EAAG,CAAA,CAAA,CACzEU,EAAU,IAAK,CAAA,KAAA,CAAOV,CAAa,EAAA,GAAA,CAAO,GAAK,EAAQ,CAAA,EAAA,GAAA,CAAO,EAAG,CAAA,CAAA,CACjEW,EAAU,IAAK,CAAA,KAAA,CAAOX,GAAa,GAAO,CAAA,EAAA,CAAA,CAAO,GAAI,CAG3D,CAAA,OAAIQ,CAAO,CAAA,CAAA,CAAU,CAAC,CAAG,CAAA,CAAA,CAAG,CAAG,CAAA,CAAC,EAC5BC,CAAQ,CAAA,CAAA,CAAU,CAAC,CAAA,CAAG,EAAG,CAAG,CAAA,CAAC,CAC7BC,CAAAA,CAAAA,CAAU,EAAU,CAAC,CAAA,CAAG,CAAG,CAAA,CAAA,CAAG,CAAC,CAC/BC,CAAAA,CAAAA,CAAU,CAAU,CAAA,CAAC,EAAG,CAAG,CAAA,CAAA,CAAG,CAAC,CAAA,CAE5B,CAACH,CAAMC,CAAAA,CAAAA,CAAOC,EAASC,CAAO,CACvC,EAEOC,CAAQjB,CAAAA","file":"useCountdown.cjs","sourcesContent":["import { useEffect, useRef, useState } from \"react\";\n\nconst useCountdown = (countDownDate: number, refreshRate?: number) => {\n const now = new Date().getTime();\n const firstVal = countDownDate - now;\n const [countDown, setCountDown] = useState(firstVal);\n const intervalRef = useRef<any | null>(null);\n\n useEffect(() => {\n const updateCountdown = () => {\n setCountDown(countDownDate - new Date().getTime());\n };\n\n updateCountdown();\n intervalRef.current = setInterval(updateCountdown, refreshRate);\n\n return () => {\n if (intervalRef.current) {\n clearInterval(intervalRef.current);\n }\n };\n }, [countDownDate, refreshRate]);\n\n return getReturnValues(countDown);\n};\n\nconst getReturnValues = (countDown: number) => {\n // calculate time left\n const days = Math.floor(countDown / (1000 * 60 * 60 * 24));\n const hours = Math.floor((countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));\n const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));\n const seconds = Math.floor((countDown % (1000 * 60)) / 1000);\n\n // values can't be negative\n if (days < 0) return [0, 0, 0, 0];\n if (hours < 0) return [0, 0, 0, 0];\n if (minutes < 0) return [0, 0, 0, 0];\n if (seconds < 0) return [0, 0, 0, 0];\n\n return [days, hours, minutes, seconds] as const;\n};\n\nexport default useCountdown;\n"]}