@towsila/expo-ui-library 4.1.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/hooks/useHeading.ts +27 -0
- package/src/hooks/useRelativeTime.ts +24 -0
- package/src/hooks/useTrimmedRoute.ts +52 -0
- package/src/index.ts +9 -1
- package/src/types/route.ts +5 -0
- package/src/utils/formatPrice.ts +2 -0
- package/src/utils/geo.ts +21 -0
- package/src/utils/shortanName.ts +30 -0
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { bearing, distanceM } from "../utils/geo";
|
|
3
|
+
|
|
4
|
+
export function useHeading(position: [number, number] | null, target: [number, number] | null) {
|
|
5
|
+
const last = useRef<[number, number] | null>(null);
|
|
6
|
+
const [heading, setHeading] = useState(0);
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (!position) return;
|
|
10
|
+
if (!last.current) {
|
|
11
|
+
last.current = position;
|
|
12
|
+
if (target) setHeading(bearing(position, target));
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (distanceM(last.current, position) >= 8) {
|
|
16
|
+
setHeading(bearing(last.current, position));
|
|
17
|
+
last.current = position;
|
|
18
|
+
}
|
|
19
|
+
}, [position?.[0], position?.[1]]);
|
|
20
|
+
|
|
21
|
+
// when the target changes (client -> destination), face it again
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (position && target) setHeading(bearing(position, target));
|
|
24
|
+
}, [target?.[0], target?.[1]]);
|
|
25
|
+
|
|
26
|
+
return heading;
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export function useRelativeTime(t: (key: string, opts?: any) => string) {
|
|
4
|
+
const [, forceTick] = useState(0);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const id = setInterval(() => forceTick((n) => n + 1), 60_000);
|
|
8
|
+
return () => clearInterval(id);
|
|
9
|
+
}, []);
|
|
10
|
+
|
|
11
|
+
return (iso: string): string => {
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
const then = new Date(iso).getTime();
|
|
14
|
+
const diffSec = Math.max(0, Math.floor((now - then) / 1000));
|
|
15
|
+
|
|
16
|
+
if (diffSec < 60) return t("request.time.second", { count: diffSec });
|
|
17
|
+
if (diffSec < 3600) return t("request.time.minute", { count: Math.floor(diffSec / 60) });
|
|
18
|
+
if (diffSec < 86400) return t("request.time.hour", { count: Math.floor(diffSec / 3600) });
|
|
19
|
+
if (diffSec < 604800) return t("request.time.day", { count: Math.floor(diffSec / 86400) });
|
|
20
|
+
if (diffSec < 2592000) return t("request.time.week", { count: Math.floor(diffSec / 604800) });
|
|
21
|
+
if (diffSec < 31536000) return t("request.time.month", { count: Math.floor(diffSec / 2592000) });
|
|
22
|
+
return t("request.time.year", { count: Math.floor(diffSec / 31536000) });
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
import { distanceM } from "../utils/geo";
|
|
3
|
+
import { RouteInfo } from "../types/route";
|
|
4
|
+
|
|
5
|
+
type LngLat = [number, number];
|
|
6
|
+
|
|
7
|
+
// closest point to p on segment a-b (local flat approximation, fine for short distances)
|
|
8
|
+
function project(p: LngLat, a: LngLat, b: LngLat) {
|
|
9
|
+
const k = Math.cos((p[1] * Math.PI) / 180);
|
|
10
|
+
const ax = a[0] * k, ay = a[1];
|
|
11
|
+
const dx = b[0] * k - ax, dy = b[1] - ay;
|
|
12
|
+
const len = dx * dx + dy * dy;
|
|
13
|
+
let t = len === 0 ? 0 : ((p[0] * k - ax) * dx + (p[1] - ay) * dy) / len;
|
|
14
|
+
t = Math.max(0, Math.min(1, t));
|
|
15
|
+
const pt: LngLat = [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
16
|
+
return { pt, dist: distanceM(p, pt) };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useTrimmedRoute(route: RouteInfo | null, position: LngLat | null) {
|
|
20
|
+
const lastIdx = useRef(0);
|
|
21
|
+
const lastRoute = useRef<RouteInfo | null>(null);
|
|
22
|
+
|
|
23
|
+
return useMemo(() => {
|
|
24
|
+
if (!route || !position) return null;
|
|
25
|
+
const coords = route.geometry.coordinates as LngLat[];
|
|
26
|
+
if (coords.length < 2) return null;
|
|
27
|
+
|
|
28
|
+
// new route: start again from the beginning
|
|
29
|
+
if (lastRoute.current !== route) {
|
|
30
|
+
lastRoute.current = route;
|
|
31
|
+
lastIdx.current = 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// only look ahead of where we already are, so the line never grows back
|
|
35
|
+
let bestI = lastIdx.current;
|
|
36
|
+
let best = project(position, coords[bestI], coords[Math.min(bestI + 1, coords.length - 1)]);
|
|
37
|
+
const end = Math.min(lastIdx.current + 60, coords.length - 2);
|
|
38
|
+
|
|
39
|
+
for (let i = lastIdx.current + 1; i <= end; i++) {
|
|
40
|
+
const c = project(position, coords[i], coords[i + 1]);
|
|
41
|
+
if (c.dist < best.dist) {
|
|
42
|
+
best = c;
|
|
43
|
+
bestI = i;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
lastIdx.current = bestI;
|
|
47
|
+
|
|
48
|
+
const rest = [best.pt, ...coords.slice(bestI + 1)];
|
|
49
|
+
if (rest.length < 2) return null;
|
|
50
|
+
return { type: "LineString" as const, coordinates: rest };
|
|
51
|
+
}, [route, position?.[0], position?.[1]]);
|
|
52
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -68,4 +68,12 @@ export {default as Toggle} from "./components/ui/toggle";
|
|
|
68
68
|
export {default as BackIcon} from "./components/ui/backIcon";
|
|
69
69
|
export {default as SuccessScreen} from "./components/screens/sucessScreen";
|
|
70
70
|
export {default as AnimatedSuccessIcon} from "./components/ui/AnimatedSuccessIcon";
|
|
71
|
-
export {ScrollWithIndicator} from "./components/ui/scrollIndicator"
|
|
71
|
+
export {ScrollWithIndicator} from "./components/ui/scrollIndicator"
|
|
72
|
+
export {default as NavigationRow} from "./components/ui/navigationCard";
|
|
73
|
+
export {useHeading} from "./hooks/useHeading";
|
|
74
|
+
export {useTrimmedRoute} from "./hooks/useTrimmedRoute";
|
|
75
|
+
export {useRelativeTime} from "./hooks/useRelativeTime";
|
|
76
|
+
export { bearing, distanceM } from "./utils/geo";
|
|
77
|
+
export { formatPrice } from "./utils/formatPrice";
|
|
78
|
+
export { shortAddress, getWilaya } from "./utils/shortanName";
|
|
79
|
+
export {RouteInfo} from "./types/route";
|
package/src/utils/geo.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const toRad = (d: number) => (d * Math.PI) / 180;
|
|
2
|
+
|
|
3
|
+
// points are [lng, lat]
|
|
4
|
+
export function bearing(from: [number, number], to: [number, number]) {
|
|
5
|
+
const dLng = toRad(to[0] - from[0]);
|
|
6
|
+
const y = Math.sin(dLng) * Math.cos(toRad(to[1]));
|
|
7
|
+
const x =
|
|
8
|
+
Math.cos(toRad(from[1])) * Math.sin(toRad(to[1])) -
|
|
9
|
+
Math.sin(toRad(from[1])) * Math.cos(toRad(to[1])) * Math.cos(dLng);
|
|
10
|
+
return ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function distanceM(a: [number, number], b: [number, number]) {
|
|
14
|
+
const R = 6371000;
|
|
15
|
+
const dLat = toRad(b[1] - a[1]);
|
|
16
|
+
const dLng = toRad(b[0] - a[0]);
|
|
17
|
+
const h =
|
|
18
|
+
Math.sin(dLat / 2) ** 2 +
|
|
19
|
+
Math.cos(toRad(a[1])) * Math.cos(toRad(b[1])) * Math.sin(dLng / 2) ** 2;
|
|
20
|
+
return 2 * R * Math.asin(Math.sqrt(h));
|
|
21
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const shortAddress = (full?: string, parts = 2) => {
|
|
2
|
+
if (!full) return "";
|
|
3
|
+
// raw coordinates (failure fallback): don't show them as a name
|
|
4
|
+
if (/^-?\d+\.\d+,\s*-?\d+\.\d+$/.test(full)) return "";
|
|
5
|
+
return full
|
|
6
|
+
.split(",")
|
|
7
|
+
.map((p) => p.trim())
|
|
8
|
+
.filter((p) => p && !/^\d+$/.test(p))
|
|
9
|
+
.slice(0, parts)
|
|
10
|
+
.join(", ");
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// utils/shortAddress.ts
|
|
14
|
+
|
|
15
|
+
export const getWilaya = (full?: string) => {
|
|
16
|
+
if (!full) return "";
|
|
17
|
+
// raw coordinates (failure fallback): no name available
|
|
18
|
+
if (/^-?\d+\.\d+,\s*-?\d+\.\d+$/.test(full)) return "";
|
|
19
|
+
|
|
20
|
+
const parts = full
|
|
21
|
+
.split(",")
|
|
22
|
+
.map((p) => p.trim())
|
|
23
|
+
.filter(Boolean)
|
|
24
|
+
.filter((p) => !/^\d{4,5}$/.test(p)) // postal codes
|
|
25
|
+
|
|
26
|
+
const last = parts[parts.length - 1] ?? "";
|
|
27
|
+
|
|
28
|
+
// some results say "Wilaya d'Alger" or "ولاية الجزائر"
|
|
29
|
+
return last.replace(/^(wilaya\s+(d'|de\s+)?|ولاية\s+)/i, "").trim();
|
|
30
|
+
};
|