Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewStanciu committed Nov 14, 2023
1 parent 1cb41a0 commit 90c2813
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
node_modules
purduehackers-time-0.6.0.tgz
40 changes: 20 additions & 20 deletions src/react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { useState, useEffect } 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('')
const [normalTimeClock, setNormalTime] = useState('')
const [lightningTimeClock, setLightningTime] = useState<string>('')
const [normalTimeClock, setNormalTime] = useState<string>('')
const [timeColors, setTimeColors] = useState<Colors>({
boltColor: '#000000',
zapColor: '#000000',
sparkColor: '#000000'
})

useEffect(() => {
const now = new Date()
const millis =
1000 * 60 * 60 * now.getHours() +
1000 * 60 * now.getMinutes() +
1000 * now.getSeconds() +
now.getMilliseconds()

const remainingMillis = MILLIS_PER_CHARGE - (millis % MILLIS_PER_CHARGE)
let nextExpectedUpdateTime = Date.now() + remainingMillis

const update = () => {
nextExpectedUpdateTime += MILLIS_PER_CHARGE

const now = new Date()
const millis =
1000 * 60 * 60 * now.getHours() +
1000 * 60 * now.getMinutes() +
1000 * now.getSeconds() +
now.getMilliseconds()
let remainingMillis = MILLIS_PER_CHARGE - (millis % MILLIS_PER_CHARGE)

const lt = new LightningTime()
const convertedTime = lt.convertToLightning(now).lightningString
const colors = lt.getColors(convertedTime)
setLightningTime(convertedTime)
setTimeColors(colors)

const formattedTime = formatTime(now, 'h:mm a')
setNormalTime(formattedTime)

const drift = Date.now() - nextExpectedUpdateTime
setTimeout(update, MILLIS_PER_CHARGE - drift)
setTimeout(update, remainingMillis)
}

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

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

0 comments on commit 90c2813

Please sign in to comment.