|
| 1 | +import { useMutation } from '@tanstack/react-query'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { toast } from 'sonner'; |
| 4 | +import { useDirectionsStore } from '@/stores/directions-store'; |
| 5 | +import { |
| 6 | + VALHALLA_OSM_URL, |
| 7 | + buildOptimizedRouteRequest, |
| 8 | + parseDirectionsGeometry, |
| 9 | +} from '@/utils/valhalla'; |
| 10 | +import { filterProfileSettings } from '@/utils/filter-profile-settings'; |
| 11 | +import { useCommonStore } from '@/stores/common-store'; |
| 12 | +import { router } from '@/routes'; |
| 13 | +import type { ValhallaOptimizedRouteResponse } from '@/components/types'; |
| 14 | +import type { Waypoint } from '@/stores/directions-store'; |
| 15 | +import { getDirectionsLanguage } from '@/utils/directions-language'; |
| 16 | + |
| 17 | +export function useOptimizedRouteQuery() { |
| 18 | + const waypoints = useDirectionsStore((state) => state.waypoints); |
| 19 | + const setWaypoint = useDirectionsStore((state) => state.setWaypoint); |
| 20 | + const receiveRouteResults = useDirectionsStore( |
| 21 | + (state) => state.receiveRouteResults |
| 22 | + ); |
| 23 | + const setIsOptimized = useDirectionsStore((state) => state.setIsOptimized); |
| 24 | + const zoomTo = useCommonStore((state) => state.zoomTo); |
| 25 | + const { settings: rawSettings } = useCommonStore.getState(); |
| 26 | + |
| 27 | + const mutation = useMutation({ |
| 28 | + mutationFn: async () => { |
| 29 | + const relevantWaypoints: Waypoint[] = []; |
| 30 | + |
| 31 | + const activeWaypoints = waypoints.flatMap((wp) => { |
| 32 | + const selected = wp.geocodeResults.filter((r) => r.selected); |
| 33 | + if (selected.length > 0) { |
| 34 | + relevantWaypoints.push(wp); |
| 35 | + } |
| 36 | + return selected; |
| 37 | + }); |
| 38 | + |
| 39 | + if (activeWaypoints.length < 4) { |
| 40 | + throw new Error('Not enough waypoints to optimize'); |
| 41 | + } |
| 42 | + |
| 43 | + const profile = router.state.location.search.profile || 'bicycle'; |
| 44 | + const settings = filterProfileSettings(profile, rawSettings); |
| 45 | + const language = getDirectionsLanguage(); |
| 46 | + const request = buildOptimizedRouteRequest({ |
| 47 | + profile, |
| 48 | + activeWaypoints, |
| 49 | + // @ts-expect-error todo: initial settings and filtered settings types mismatch |
| 50 | + settings, |
| 51 | + language, |
| 52 | + }); |
| 53 | + const { data } = await axios.get<ValhallaOptimizedRouteResponse>( |
| 54 | + `${VALHALLA_OSM_URL}/optimized_route`, |
| 55 | + { params: { json: JSON.stringify(request.json) } } |
| 56 | + ); |
| 57 | + |
| 58 | + const processedData = { |
| 59 | + ...data, |
| 60 | + id: data.id ?? 'valhalla_optimized_route', |
| 61 | + decodedGeometry: parseDirectionsGeometry(data), |
| 62 | + }; |
| 63 | + |
| 64 | + return { data: processedData, relevantWaypoints }; |
| 65 | + }, |
| 66 | + onSuccess: ({ data, relevantWaypoints }) => { |
| 67 | + const newWaypoints: Waypoint[] = []; |
| 68 | + const locations = data.trip.locations; |
| 69 | + locations.forEach((loc) => { |
| 70 | + if (typeof loc.original_index === 'number') { |
| 71 | + const original = relevantWaypoints[loc.original_index]; |
| 72 | + if (original) { |
| 73 | + newWaypoints.push(original); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + setWaypoint(newWaypoints); |
| 78 | + setIsOptimized(true); |
| 79 | + receiveRouteResults({ data }); |
| 80 | + zoomTo(data.decodedGeometry); |
| 81 | + toast.success('Route optimized successfully'); |
| 82 | + }, |
| 83 | + onError: (error) => { |
| 84 | + console.error('Optimization error:', error); |
| 85 | + toast.error('Failed to optimize route'); |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + return { |
| 90 | + optimizeRoute: mutation.mutate, |
| 91 | + isPending: mutation.isPending, |
| 92 | + }; |
| 93 | +} |
0 commit comments