mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 11:20:17 -04:00
33 lines
728 B
TypeScript
33 lines
728 B
TypeScript
import { useMemo } from 'react';
|
|
import estimateOptimalPath, { Location } from '../lib/estimateoptimalpath';
|
|
import furthestPoint from '../lib/furthestpoint';
|
|
|
|
export default function useOptimalPath(
|
|
memberLocations: Location[],
|
|
destination: Location
|
|
) {
|
|
return useMemo(() => {
|
|
if (memberLocations.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
// O(n)
|
|
const { maxLocation: driverLocation } = furthestPoint(
|
|
memberLocations,
|
|
destination
|
|
);
|
|
|
|
// O(n)
|
|
const passengerLocations = memberLocations.filter(
|
|
(location) => location !== driverLocation
|
|
);
|
|
|
|
// O(n)
|
|
return estimateOptimalPath({
|
|
from: driverLocation!,
|
|
waypoints: passengerLocations,
|
|
to: destination,
|
|
});
|
|
}, [destination, memberLocations]);
|
|
}
|