-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3f543f
commit eb54352
Showing
7 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from './use-context-fallback.hook'; | ||
export * from './use-geolocation.hook'; | ||
export * from './use-mounted.hook'; | ||
export * from './use-timeout.hook'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Context, useContext } from 'react'; | ||
|
||
export const useContextFallback = <T>(value: Context<T | undefined>): T => { | ||
const context = useContext<T | undefined>(value); | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
`Components that require this context must be children of ${ | ||
value.displayName ?? 'the appropriate provider' | ||
}.` | ||
); | ||
} | ||
|
||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
import { Coordinates } from '../models'; | ||
|
||
export const useGeoLocation = (): Coordinates | null => { | ||
const [geoLocation, setGeoLocation] = useState<Coordinates | null>(null); | ||
|
||
useEffect(() => { | ||
navigator.geolocation.getCurrentPosition( | ||
({ coords }) => setGeoLocation(coords), | ||
() => setGeoLocation(null), | ||
{ | ||
enableHighAccuracy: false, | ||
timeout: 60000, | ||
} | ||
); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const value = useMemo(() => geoLocation, [geoLocation]); | ||
|
||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
export const useMounted = () => { | ||
const mountedRef = useRef(false); | ||
const isMounted = useCallback(() => mountedRef.current, []); | ||
|
||
useEffect(() => { | ||
mountedRef.current = true; | ||
|
||
return () => { | ||
mountedRef.current = false; | ||
}; | ||
}, []); | ||
|
||
return isMounted; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Coordinates } from '../models'; | ||
|
||
/** | ||
* Distance calculation based on the Haversine formula | ||
* Credit: https://stackoverflow.com/a/21623206/12130423 | ||
* @param coordsA - The coords of the users location (geoCoords or defaultCoords) | ||
* @param coordsB - The coords of the city | ||
* | ||
* @returns Distance in km (number) to two decimal places | ||
*/ | ||
export const getDistanceBetweenCoords = ( | ||
coordsA: Coordinates, | ||
coordsB: Coordinates | ||
): number => { | ||
const R = 6371.071; // Radius of the Earth in miles | ||
const coordsALatRad = coordsA.latitude * (Math.PI / 180); // Convert degrees to radians | ||
const coordsBLatRad = coordsB.latitude * (Math.PI / 180); // Convert degrees to radians | ||
const latDiff = coordsBLatRad - coordsALatRad; // Radian difference (latitudes) | ||
const lonDiff = (coordsB.longitude - coordsA.longitude) * (Math.PI / 180); // Radian difference (longitudes) | ||
|
||
const distance = | ||
2 * | ||
R * | ||
Math.asin( | ||
Math.sqrt( | ||
Math.sin(latDiff / 2) * Math.sin(latDiff / 2) + | ||
Math.cos(coordsALatRad) * | ||
Math.cos(coordsBLatRad) * | ||
Math.sin(lonDiff / 2) * | ||
Math.sin(lonDiff / 2) | ||
) | ||
); | ||
|
||
return Number(parseFloat(String(distance)).toFixed(2)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './distance.util'; |