blob: 2ca279bacc4df46bb167bbedacf35822c5a6a211 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import { useState } from 'react'
import { useGeolocation as globalUseGeolocation } from './use-geolocation'
import { useInterval } from './use-interval'
import suncalc from 'suncalc'
export const computeLightLevel = (
{ loading, latitude, longitude },
date = new Date()
) => {
if (loading || !latitude || !longitude) return 0
let { altitude } = suncalc.getPosition(date, latitude, longitude)
if (altitude < 0) {
altitude += 2 * Math.PI
}
// on or below the horizon is considered "no light"
if (altitude >= Math.PI) return 0
// otherwise, return a relative altitude
// where n is an int in [0,10]
// and 10 represents the zenith (straight over your head)
return Math.round(10 * Math.sin(altitude))
}
// make a location hook injectable such that we can test this
export const useSunlight = (useGeolocation = globalUseGeolocation) => {
const geoLocation = useGeolocation()
const [lightLevel, setLightLevel] = useState(computeLightLevel(geoLocation))
useInterval(() => {
setLightLevel(computeLightLevel(geoLocation))
}, 1000)
return [lightLevel]
}
|