Files
waseel/components/google-text-input.tsx
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

184 lines
5.4 KiB
TypeScript

import { useEffect, useRef, useState } from "react";
import {
Image,
Keyboard,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { icons } from "@/constants";
import { useT } from "@/lib/i18n";
import { useTheme } from "@/lib/theme";
import type { GoogleInputProps } from "@/types/type";
const googleApiKey = process.env.EXPO_PUBLIC_GOOGLE_API_KEY!;
interface Suggestion {
placeId: string;
text: string;
}
// Places API (New) — the legacy Places web service is unavailable to
// newer Google Cloud projects. Results are restricted to Lebanon.
const fetchSuggestions = async (input: string): Promise<Suggestion[]> => {
const res = await fetch(
"https://places.googleapis.com/v1/places:autocomplete",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Goog-Api-Key": googleApiKey,
},
body: JSON.stringify({
input,
languageCode: "en",
includedRegionCodes: ["lb"],
}),
},
);
const data = await res.json();
return (data.suggestions ?? [])
.map((s: any) => ({
placeId: s.placePrediction?.placeId as string,
text: s.placePrediction?.text?.text as string,
}))
.filter((s: Suggestion) => s.placeId && s.text);
};
const fetchPlaceDetails = async (placeId: string) => {
const res = await fetch(`https://places.googleapis.com/v1/places/${placeId}`, {
headers: {
"X-Goog-Api-Key": googleApiKey,
"X-Goog-FieldMask": "location,formattedAddress",
},
});
const data = await res.json();
return {
latitude: data.location?.latitude as number,
longitude: data.location?.longitude as number,
address: data.formattedAddress as string,
};
};
export const GoogleTextInput = ({
icon,
initialLocation,
containerStyles,
textInputBackgroundColor,
handlePress,
}: GoogleInputProps) => {
const t = useT();
const { isDark } = useTheme();
const [query, setQuery] = useState("");
const [suggestions, setSuggestions] = useState<Suggestion[]>([]);
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const inputBg = textInputBackgroundColor || (isDark ? "#1a1a1a" : "white");
const inputShadow = isDark ? "#000000" : "#d4d4d4";
useEffect(() => {
if (debounceRef.current) clearTimeout(debounceRef.current);
if (query.trim().length < 3) {
setSuggestions([]);
return;
}
debounceRef.current = setTimeout(async () => {
try {
setSuggestions(await fetchSuggestions(query));
} catch (err) {
console.log("[PLACES_AUTOCOMPLETE]: ", err);
}
}, 200);
return () => {
if (debounceRef.current) clearTimeout(debounceRef.current);
};
}, [query]);
const onSelect = async (suggestion: Suggestion) => {
setQuery(suggestion.text);
setSuggestions([]);
// The search is over the moment a place is picked. Left open, the keyboard
// covers whatever the next tap was meant to be — and inside a bottom sheet
// it holds the sheet in its extended state on top of it.
Keyboard.dismiss();
try {
const details = await fetchPlaceDetails(suggestion.placeId);
handlePress({
latitude: details.latitude,
longitude: details.longitude,
address: suggestion.text,
});
} catch (err) {
console.log("[PLACE_DETAILS]: ", err);
}
};
return (
<View
className={`flex flex-row items-center justify-center relative z-50 rounded-xl ${containerStyles}`}
>
<View className="flex-1 mx-5">
<View
className="flex flex-row items-center rounded-full px-4 mt-1"
style={{
backgroundColor: inputBg,
shadowColor: inputShadow,
}}
>
<View className="justify-center items-center w-6 h-6">
<Image
source={icon ? icon : icons.search}
alt={t("components.googleTextInput.searchAlt")}
className="w-6 h-6"
resizeMode="contain"
/>
</View>
<TextInput
value={query}
onChangeText={setQuery}
placeholder={initialLocation ?? t("components.googleTextInput.placeholder")}
placeholderTextColor="#a3a3a3"
className="flex-1 p-3 text-base font-JakartaSemiBold text-black dark:text-white"
/>
</View>
{/* Rendered as plain rows, not a FlatList. Places never returns more
than a handful of predictions, so there is nothing to virtualise —
and a list that scrolls inside the home feed (or inside the ride
sheet) fights its parent for the gesture and swallows taps meant
for a suggestion. */}
{suggestions.length > 0 && (
<View
className="rounded-xl mt-1"
style={{
backgroundColor: inputBg,
shadowColor: inputShadow,
}}
>
{suggestions.map((item) => (
<TouchableOpacity
key={item.placeId}
onPress={() => onSelect(item)}
className="p-3 border-b border-general-700 dark:border-neutral-700"
>
<Text className="text-base font-JakartaRegular text-black dark:text-white">
{item.text}
</Text>
</TouchableOpacity>
))}
</View>
)}
</View>
</View>
);
};