Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/directions/waypoints/waypoint-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ export const Waypoint = ({ id, index }: WaypointProps) => {
const doRemoveWaypoint = useDirectionsStore(
(state) => state.doRemoveWaypoint
);
const waypoint = waypoints[index];
const { userInput, geocodeResults } = waypoint!;

const handleGeocodeResults = useCallback(
(addresses: ActiveWaypoint[]) => {
Expand All @@ -60,6 +58,11 @@ export const Waypoint = ({ id, index }: WaypointProps) => {
[updateTextInput, index, refetchDirections]
);

const waypoint = waypoints.find((wp) => wp.id === id);
if (!waypoint) return null;

const { userInput, geocodeResults } = waypoint!;

const style = {
transform: CSS.Transform.toString(transform),
transition,
Expand Down
9 changes: 8 additions & 1 deletion src/components/route-planner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useQuery } from '@tanstack/react-query';
import { format } from 'date-fns';
import { DirectionsControl } from './directions/directions';
import { IsochronesControl } from './isochrones/isochrones';
import { useDirectionsStore } from '@/stores/directions-store';

const TilesControl = lazy(() =>
import('./tiles/tiles').then((module) => ({ default: module.TilesControl }))
Expand Down Expand Up @@ -52,6 +53,9 @@ export const RoutePlanner = () => {
const loading = useCommonStore((state) => state.loading);
const toggleDirections = useCommonStore((state) => state.toggleDirections);

const clearPlaceholderWaypoints = useDirectionsStore(
(state) => state.clearPlaceholderWaypoints
);
const tabConfig = TAB_CONFIG[activeTab as keyof typeof TAB_CONFIG];

const {
Expand Down Expand Up @@ -124,7 +128,10 @@ export const RoutePlanner = () => {
<Button
variant="ghost"
size="icon"
onClick={toggleDirections}
onClick={() => {
clearPlaceholderWaypoints();
toggleDirections();
}}
data-testid="close-directions-button"
>
<X className="size-4" />
Expand Down
20 changes: 20 additions & 0 deletions src/stores/directions-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ interface DirectionsActions {
) => void;
setIsOptimized: (isOptimized: boolean) => void;
setActiveRouteIndex: (index: number) => void;
clearPlaceholderWaypoints: () => void;
}

type DirectionsStore = DirectionsState & DirectionsActions;
Expand Down Expand Up @@ -353,6 +354,25 @@ export const useDirectionsStore = create<DirectionsStore>()(
undefined,
'setActiveRouteIndex'
),

clearPlaceholderWaypoints: () =>
set(
(state) => {
state.waypoints.forEach((wp, i) => {
const hasOnlyPlaceholders = wp.geocodeResults.every(
(r) => !r.title || r.title === ''
);
if (hasOnlyPlaceholders) {
if (state.waypoints[i]) {
state.waypoints[i].geocodeResults = [];
state.waypoints[i].userInput = '';
}
}
});
},
undefined,
'clearPlaceholderWaypoints'
),
})),
{ name: 'directions-store' }
)
Expand Down