Skip to content

Commit

Permalink
Move logic to core hook
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewStanciu committed Dec 13, 2023
1 parent 5358af8 commit 37dc5c7
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/use-lightning-time-clock.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { format as formatTime } from 'date-fns'
import { LightningTime, MILLIS_PER_CHARGE } from './time'
import type { Colors, GenericUseEffect, GenericUseState } from './types'
import type { LightningTimeClock, GenericUseEffect, GenericUseState } from './types'

function calculateInitialData() {
function calculateInitialTime(): LightningTimeClock {
const now = new Date()
const { lightningString, colors } = new LightningTime().convertToLightning(
now
)
const lightningTime = new LightningTime().convertToLightning(now)
const formattedNormalTime = formatTime(now, 'h:mm a')

return {
lightningString,
formattedNormalTime,
colors
...lightningTime,
formattedNormalTime
}
}

Expand All @@ -23,12 +20,9 @@ export function useLightningTimeClockCore({
useState: GenericUseState
useEffect: GenericUseEffect
}) {
const { lightningString, formattedNormalTime, colors } =
calculateInitialData()
const [lightningTimeClock, setLightningTime] =
useState<string>(lightningString)
const [normalTimeClock, setNormalTime] = useState<string>(formattedNormalTime)
const [timeColors, setTimeColors] = useState<Colors>(colors)
const initialTime = calculateInitialTime()
const [lightningTimeClock, setLightningTimeClock] =
useState<LightningTimeClock>(initialTime)

useEffect(() => {
const update = () => {
Expand All @@ -41,18 +35,18 @@ export function useLightningTimeClockCore({
let remainingMillis = MILLIS_PER_CHARGE - (millis % MILLIS_PER_CHARGE)

const lt = new LightningTime()
const convertedTime = lt.convertToLightning(now).lightningString
const formattedTime = formatTime(now, 'h:mm a')
const colors = lt.getColors(convertedTime)
const convertedTime = lt.convertToLightning(now)
const formattedNormalTime = formatTime(now, 'h:mm a')

setLightningTime(convertedTime)
setTimeColors(colors)
setNormalTime(formattedTime)
setLightningTimeClock({
...convertedTime,
formattedNormalTime
})

setTimeout(update, remainingMillis)
}
update()
}, [])

return { lightningTimeClock, timeColors, normalTimeClock }
}
return lightningTimeClock
}

0 comments on commit 37dc5c7

Please sign in to comment.