Files
waseel/lib/use-service-availability.ts
T
KrikoriosandClaude Opus 5 8807ff41c5 Waseel: driver capture, chat/calls, dispatch, and session fixes
Driver onboarding now photographs the licence, ID card and vehicle
registration and reads the credential fields off them, plus a camera-only
profile selfie riders check the arriving driver against. Adds in-app chat
and WebRTC calls, push-backed ride offers, ratings, cancellation and
payment sheets, settlement, and the owner dashboard endpoints behind them.

Camera permission on Android:
  - Declare CAMERA and READ_MEDIA_IMAGES in the manifest. expo-image-picker's
    own plugin never declares CAMERA, and Android denies a request for an
    undeclared permission instantly and silently — no dialog is ever shown,
    which is indistinguishable from the app not asking at all.
  - Handle canAskAgain: once Android stops showing the dialog, repeating why
    we need it is a dead end, so offer Open Settings instead (lib/capture-
    permission.ts), matching what the location flow already did.

Session: a 401 on a request that carried a token now ends the session
instead of being reinterpreted per-screen — driver-home had been reading it
as "this user has no driver profile" and showing an onboarding form to an
already-onboarded driver. Requests without a token are exempt so a failed
sign-in doesn't sign you out, and the notification is latched per token so
concurrent polls tear the session down once. (root) gains the auth guard
that turns that into the sign-in screen; app/index.tsx only guarded the way
in, leaving a session that ended mid-screen with nowhere to go.

Also ignore .uploads/ — it holds driver licence, ID and vehicle scans plus
profile photos, which are personal data and must not be committed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 02:17:55 +03:00

108 lines
3.6 KiB
TypeScript

// Which services actually have a driver near the rider.
//
// The map only ever shows the selected service, so an empty map means both
// "nobody is driving tonight" and "nobody is on a moto, though three cars are
// a street away" — and the rider has no way to tell which. That ambiguity is
// what leaves someone staring at a blank map instead of switching service and
// getting a ride.
//
// This answers it once for every service, so the picker can show availability
// and the request screen can point at a service that would actually work.
import { useEffect, useRef, useState } from "react";
import { SERVICES, type ServiceId } from "@/constants/services";
import { fetchAPI } from "@/lib/fetch";
import { SEARCH_MAX_RADIUS_M } from "@/lib/use-nearby-drivers";
// Slower than the map's own poll: this drives a hint, not the pins, and it
// scans every service rather than one.
const POLL_MS = 15000;
// ~110 m, so GPS jitter doesn't restart the request loop on every fix.
const QUANTIZE = 1e3;
const quantize = (v: number): number => Math.round(v * QUANTIZE) / QUANTIZE;
export type ServiceAvailability = {
/** Driver count per service, every service present (zeros included). */
counts: Record<ServiceId, number>;
/** Services with at least one driver in range. */
available: ServiceId[];
/** Radius the counts were measured over, in metres. */
radius: number;
loading: boolean;
};
const emptyCounts = (): Record<ServiceId, number> => {
const counts = {} as Record<ServiceId, number>;
for (const service of SERVICES) counts[service.id] = 0;
return counts;
};
export const useServiceAvailability = (
latitude: number | null,
longitude: number | null,
): ServiceAvailability => {
const [counts, setCounts] = useState<Record<ServiceId, number>>(emptyCounts);
const [radius, setRadius] = useState(SEARCH_MAX_RADIUS_M);
const [loading, setLoading] = useState(true);
const latKey = latitude === null ? null : quantize(latitude);
const lngKey = longitude === null ? null : quantize(longitude);
const coords = useRef({ latitude, longitude });
coords.current = { latitude, longitude };
useEffect(() => {
if (latKey === null || lngKey === null) return;
let cancelled = false;
let timer: ReturnType<typeof setTimeout>;
const run = async () => {
const { latitude: lat, longitude: lng } = coords.current;
if (lat === null || lng === null) return;
try {
const res = await fetchAPI(
`/(api)/driver/availability?lat=${lat}&lng=${lng}&radius=${SEARCH_MAX_RADIUS_M}`,
);
if (cancelled) return;
const data = res.data as {
radius: number;
counts: Record<string, number>;
};
// Merge onto a full set of zeros so a service the server didn't
// mention still renders as "none nearby" rather than blank.
const next = emptyCounts();
for (const service of SERVICES) {
next[service.id] = data.counts?.[service.id] ?? 0;
}
setCounts(next);
setRadius(data.radius ?? SEARCH_MAX_RADIUS_M);
} catch {
// Leave the last known counts in place — a dropped request shouldn't
// flash "no drivers anywhere" at the rider.
} finally {
if (!cancelled) {
setLoading(false);
timer = setTimeout(run, POLL_MS);
}
}
};
void run();
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [latKey, lngKey]);
const available = SERVICES.map((s) => s.id).filter((id) => counts[id] > 0);
return { counts, available, radius, loading };
};