-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathget-direction-icon.ts
More file actions
127 lines (107 loc) · 3.02 KB
/
get-direction-icon.ts
File metadata and controls
127 lines (107 loc) · 3.02 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
import {
ArrowUp,
ArrowRight,
ArrowLeft,
ArrowUpRight,
ArrowUpLeft,
CornerDownRight,
CornerDownLeft,
RotateCcw,
GitMerge,
CircleDot,
} from 'lucide-react';
import type { LucideIcon } from 'lucide-react';
export interface ValhallaStep {
type?: number;
instruction?: string;
bearing_before?: number;
bearing_after?: number;
}
function getDirectionFromBearing(
before: number,
after: number
): 'straight' | 'right' | 'left' | 'slight-right' | 'slight-left' {
const diff = (after - before + 360) % 360;
if (diff <= 10 || diff >= 350) return 'straight';
if (diff <= 90) return 'slight-right';
if (diff <= 180) return 'right';
if (diff <= 270) return 'left';
return 'slight-left';
}
export function getTurnIcon(step: ValhallaStep): LucideIcon {
const text = step.instruction?.toLowerCase() || '';
if (text.includes('u-turn')) return RotateCcw;
if (text.includes('merge')) return GitMerge;
if (text.includes('enter') && text.includes('exit')) {
if (
typeof step.bearing_before === 'number' &&
typeof step.bearing_after === 'number'
) {
const dir = getDirectionFromBearing(
step.bearing_before,
step.bearing_after
);
if (dir === 'right') return ArrowRight;
if (dir === 'left') return ArrowLeft;
if (dir === 'slight-right') return ArrowUpRight;
if (dir === 'slight-left') return ArrowUpLeft;
}
return ArrowRight;
}
if (
text.includes('exit') &&
(text.includes('roundabout') || step.type === 15)
) {
if (
typeof step.bearing_before === 'number' &&
typeof step.bearing_after === 'number'
) {
const dir = getDirectionFromBearing(
step.bearing_before,
step.bearing_after
);
if (dir === 'right') return ArrowRight;
if (dir === 'left') return ArrowLeft;
if (dir === 'slight-right') return ArrowUpRight;
if (dir === 'slight-left') return ArrowUpLeft;
}
return ArrowRight;
}
if (
text.includes('exit') &&
typeof step.bearing_before === 'number' &&
typeof step.bearing_after === 'number'
) {
const dir = getDirectionFromBearing(
step.bearing_before,
step.bearing_after
);
if (dir === 'right') return ArrowRight;
if (dir === 'left') return ArrowLeft;
if (dir === 'slight-right') return ArrowUpRight;
if (dir === 'slight-left') return ArrowUpLeft;
}
if (
text.includes('enter') &&
(text.includes('roundabout') || step.type === 15)
) {
return CircleDot;
}
if (text.includes('sharp right')) return CornerDownRight;
if (text.includes('sharp left')) return CornerDownLeft;
if (
text.includes('slight right') ||
text.includes('bear right') ||
text.includes('keep right')
)
return ArrowUpRight;
if (
text.includes('slight left') ||
text.includes('bear left') ||
text.includes('keep left')
)
return ArrowUpLeft;
if (text.includes('right')) return ArrowRight;
if (text.includes('straight') || text.includes('continue')) return ArrowUp;
return ArrowUp;
}