Skip to content

Commit

Permalink
Merge pull request #18 from purduehackers/hook-expose-entire-time-string
Browse files Browse the repository at this point in the history
Surface entire Lightning Time object in clock hook
  • Loading branch information
MatthewStanciu authored Dec 13, 2023
2 parents 4860c7e + 37dc5c7 commit 59fcfda
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export interface StaticColors {
sparkColors: number[]
}

export type LightningTimeClock = LightningString & {
formattedNormalTime: string
}
export type GenericUseState = <T>(
initialState: T | (() => T)
) => [T, (newState: T | ((prevState: T) => T)) => void]
Expand Down
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 59fcfda

Please sign in to comment.