-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathwaypoint-item.tsx
More file actions
132 lines (124 loc) · 3.79 KB
/
waypoint-item.tsx
File metadata and controls
132 lines (124 loc) · 3.79 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useCallback } from 'react';
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import type { ActiveWaypoint } from '@/components/types';
import { Button } from '@/components/ui/button';
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip';
import { WaypointSearch } from '@/components/ui/waypoint-search';
import { GripVertical, Trash } from 'lucide-react';
import { useDirectionsStore } from '@/stores/directions-store';
import { useDirectionsQuery } from '@/hooks/use-directions-queries';
interface WaypointProps {
id: string;
index: number;
}
export const Waypoint = ({ id, index }: WaypointProps) => {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
} = useSortable({ id });
const waypoints = useDirectionsStore((state) => state.waypoints);
const receiveGeocodeResults = useDirectionsStore(
(state) => state.receiveGeocodeResults
);
const updateTextInput = useDirectionsStore((state) => state.updateTextInput);
const { refetch: refetchDirections } = useDirectionsQuery();
const doRemoveWaypoint = useDirectionsStore(
(state) => state.doRemoveWaypoint
);
const waypoint = waypoints[index];
const { userInput, geocodeResults } = waypoint!;
const handleGeocodeResults = useCallback(
(addresses: ActiveWaypoint[]) => {
receiveGeocodeResults({ addresses, index });
},
[receiveGeocodeResults, index]
);
const handleResultSelect = useCallback(
(result: ActiveWaypoint) => {
updateTextInput({
inputValue: result.title,
index: index,
addressindex: result.addressindex,
});
refetchDirections();
},
[updateTextInput, index, refetchDirections]
);
const style = {
transform: CSS.Transform.toString(transform),
transition,
opacity: isDragging ? 0.5 : 1,
};
return (
<div
ref={setNodeRef}
style={style}
{...attributes}
role="listitem"
aria-label={`Waypoint ${index + 1}`}
className="cursor-auto"
>
<WaypointSearch
index={index}
userInput={userInput}
geocodeResults={geocodeResults}
onGeocodeResults={handleGeocodeResults}
onResultSelect={handleResultSelect}
placeholder="Select a waypoint..."
testId={`waypoint-input-${index}`}
leftContent={
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
className="cursor-grab active:cursor-grabbing"
size="sm"
{...listeners}
aria-label={`Drag handle for waypoint ${index + 1}. Press space bar to pick up, use arrow keys to move, space bar to drop.`}
>
{index + 1}
<GripVertical className="size-4" aria-hidden="true" />
</Button>
</TooltipTrigger>
<TooltipContent>
<p>Re-shuffle this waypoint</p>
</TooltipContent>
</Tooltip>
}
rightContent={
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon-sm"
onClick={() => {
doRemoveWaypoint({ index });
refetchDirections();
}}
data-testid="remove-waypoint-button"
disabled={
waypoints.every((wp) => !wp.userInput) &&
waypoints.length <= 2
}
>
<Trash className="size-3" />
</Button>
</TooltipTrigger>
<TooltipContent>
<p>Remove this waypoint</p>
</TooltipContent>
</Tooltip>
}
/>
</div>
);
};