import Constants from 'expo-constants';
import { Platform } from 'react-native';
import { DEFAULT_LOCATION } from '../location';
import type { Coord } from '../types';
import type { DeviceProfile, Role } from './protocol';
const DISPATCH_PORT = 8787;
function randomId(prefix: string): string {
return `${prefix}-${Math.random().toString(36).slice(2, 8)}`;
}
const params =
Platform.OS === 'web' && typeof window !== 'undefined' ? new URLSearchParams(window.location.search) : null;
function profileFromUrl(): DeviceProfile | null {
if (!params) return null;
const id = params.get('device');
if (!id) return null;
const role: Role = params.get('role') === 'driver' ? 'driver' : 'rider';
const rating = Number(params.get('rating'));
return {
id,
role,
name: params.get('name') ?? id,
car: params.get('car') ?? undefined,
plate: params.get('plate') ?? undefined,
rating: rating > 0 ? rating : undefined,
};
}
const fromUrl = profileFromUrl();
export const appVariant: Role = Constants.expoConfig?.extra?.appVariant === 'driver' ? 'driver' : 'rider';
export const device: DeviceProfile = fromUrl ?? {
id: randomId(appVariant),
role: appVariant,
name: appVariant === 'driver' ? 'Driver' : 'You',
};
export const startsDriving = fromUrl?.role === 'driver' && params?.get('driving') === '1';
* Simulated phones never use the real location API: there's nobody to answer a permission
* prompt, and the test machine's position would leak into scenarios. They sit at the
* position in their URL (`lat`, `lng`), or downtown San Francisco. Null outside the simulator.
*/
export const simulatedLocation: Coord | null = fromUrl ? locationFromUrl() : null;
function locationFromUrl(): Coord {
const latitude = Number(params?.get('lat'));
const longitude = Number(params?.get('lng'));
const valid = params?.has('lat') && params.has('lng') && Number.isFinite(latitude) && Number.isFinite(longitude);
return valid ? { latitude, longitude } : DEFAULT_LOCATION;
}
export const storagePrefix = fromUrl ? `${fromUrl.id}:` : '';
function dispatchUrl(): string {
if (process.env.EXPO_PUBLIC_DISPATCH_URL) return process.env.EXPO_PUBLIC_DISPATCH_URL;
const host =
Platform.OS === 'web' && typeof window !== 'undefined'
? window.location.hostname
: (Constants.expoConfig?.hostUri?.split(':')[0] ?? 'localhost');
return `ws://${host}:${DISPATCH_PORT}`;
}
export const DISPATCH_URL = dispatchUrl();