diff --git a/package.json b/package.json index d7d2633..357f554 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@purduehackers/time", - "version": "0.6.5", + "version": "0.6.6", "description": "convert between traditional time and lightning time ⚡️", "main": "dist/index.js", "module": "dist/esm/index.js", diff --git a/src/react/index.tsx b/src/react/index.tsx index 640a1bf..4d0bca7 100644 --- a/src/react/index.tsx +++ b/src/react/index.tsx @@ -1,16 +1,32 @@ -import { useState, useEffect } from 'react' +import { useState, useEffect, useRef, useLayoutEffect } from 'react' import { format as formatTime } from 'date-fns' import { LightningTime, MILLIS_PER_CHARGE } from '../time' import { Colors } from '../types' export function useLightningTimeClock() { - const [lightningTimeClock, setLightningTime] = useState('0~0~0') - const [normalTimeClock, setNormalTime] = useState('12:00 AM') + const [lightningTimeClock, setLightningTime] = useState('') + const [normalTimeClock, setNormalTime] = useState('') const [timeColors, setTimeColors] = useState({ - boltColor: '#00000000', - zapColor: '#00000000', - sparkColor: '#00000000' + boltColor: '', + zapColor: '', + sparkColor: '' }) + const isFirstRender = useRef(true) + + useLayoutEffect(() => { + if (isFirstRender.current) { + const now = new Date() + const { lightningString, colors } = + new LightningTime().convertToLightning(now) + const formattedTime = formatTime(now, 'h:mm a') + + setLightningTime(lightningString) + setNormalTime(formattedTime) + setTimeColors(colors) + + isFirstRender.current = false + } + }, []) useEffect(() => { const update = () => { @@ -24,12 +40,13 @@ export function useLightningTimeClock() { const lt = new LightningTime() const convertedTime = lt.convertToLightning(now).lightningString + const formattedTime = formatTime(now, 'h:mm a') const colors = lt.getColors(convertedTime) + setLightningTime(convertedTime) setTimeColors(colors) - - const formattedTime = formatTime(now, 'h:mm a') setNormalTime(formattedTime) + setTimeout(update, remainingMillis) } update()