-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTideHeight.tsx
More file actions
56 lines (50 loc) · 1.08 KB
/
TideHeight.tsx
File metadata and controls
56 lines (50 loc) · 1.08 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
import { imperial } from "../utils/units";
const FEET_PER_METER = 3.28084;
const MILES_PER_METER = 0.000621371;
interface DistanceProps {
meters: number;
precision?: number;
}
export function Distance({ meters, precision }: DistanceProps) {
let value: number;
let unit: string;
if (imperial) {
const miles = meters * MILES_PER_METER;
if (miles >= 0.1) {
value = miles;
unit = "mi";
} else {
value = meters * FEET_PER_METER;
unit = "ft";
}
} else {
if (meters >= 1000) {
value = meters / 1000;
unit = "km";
} else {
value = meters;
unit = "m";
}
}
return (
<span className="tabular-nums">
{value.toFixed(precision ?? (value >= 5 ? 0 : 1))}
<span className="text-[0.8em]"> {unit}</span>
</span>
);
}
export function TideHeight({
value,
precision = 1,
}: {
value: number;
precision?: number;
}) {
const unit = imperial ? "ft" : "m";
return (
<span className="tabular-nums">
{value.toFixed(precision)}
<span className="text-[0.8em]"> {unit}</span>
</span>
);
}