im-amrith/crisp
0
1/**2 * Location utility functions for handling geolocation permissions and errors3 */4 5export interface LocationError {6 code: number;7 message: string;8 userGuidance: string;9}10 11export interface LocationData {12 latitude: number;13 longitude: number;14 accuracy?: number;15 timestamp?: number;16}17 18/**19 * Request location permission with user-friendly error handling20 */21export const requestLocationPermission = (): Promise<LocationData> => {22 return new Promise((resolve, reject) => {23 if (!navigator.geolocation) {24 const error: LocationError = {25 code: -1,26 message: 'Geolocation is not supported by this browser',27 userGuidance: 'Please use a modern browser with geolocation support like Chrome, Firefox, Safari, or Edge.'28 };29 reject(error);30 return;31 }32 33 const options = {34 enableHighAccuracy: true,35 timeout: 10000,36 maximumAge: 300000 // 5 minutes37 };38 39 navigator.geolocation.getCurrentPosition(40 (position) => {41 resolve({42 latitude: position.coords.latitude,43 longitude: position.coords.longitude,44 accuracy: position.coords.accuracy,45 timestamp: position.timestamp46 });47 },48 (error) => {49 let errorMessage = 'Failed to get location';50 let userGuidance = '';51 52 switch (error.code) {53 case error.PERMISSION_DENIED:54 errorMessage = 'Location access denied';55 userGuidance = 'To enable location access:\n\n' +56 '1. Click the location icon in your browser\'s address bar\n' +57 '2. Select "Allow" or "Always allow"\n' +58 '3. Refresh the page and try again\n\n' +59 'Alternatively, you can manually enter your location coordinates.';60 break;61 case error.POSITION_UNAVAILABLE:62 errorMessage = 'Location information unavailable';63 userGuidance = 'Your device may not be able to determine your location. Please try:\n\n' +64 '1. Moving to an area with better GPS signal\n' +65 '2. Checking if your device\'s location services are enabled\n' +66 '3. Using manual location entry instead';67 break;68 case error.TIMEOUT:69 errorMessage = 'Location request timed out';70 userGuidance = 'The location request took too long. Please try:\n\n' +71 '1. Moving to an area with better GPS signal\n' +72 '2. Checking your internet connection\n' +73 '3. Using manual location entry instead';74 break;75 default:76 errorMessage = 'An unknown error occurred while getting location';77 userGuidance = 'Please try using manual location entry or contact support if the problem persists.';78 }79 80 const locationError: LocationError = {81 code: error.code,82 message: errorMessage,83 userGuidance84 };85 86 reject(locationError);87 },88 options89 );90 });91};92 93/**94 * Check if geolocation is supported by the browser95 */96export const isGeolocationSupported = (): boolean => {97 return 'geolocation' in navigator;98};99 100/**101 * Check if location permission is granted (non-blocking)102 */103export const checkLocationPermission = (): Promise<boolean> => {104 return new Promise((resolve) => {105 if (!isGeolocationSupported()) {106 resolve(false);107 return;108 }109 110 // Try to get current position with a very short timeout111 navigator.geolocation.getCurrentPosition(112 () => resolve(true),113 () => resolve(false),114 { timeout: 1000, maximumAge: 0 }115 );116 });117};118 119/**120 * Format location error for display121 */122export const formatLocationError = (error: LocationError): string => {123 return `${error.message}\n\n${error.userGuidance}`;124};125 126/**127 * Show user-friendly location error alert128 */129export const showLocationError = (error: LocationError): void => {130 const message = formatLocationError(error);131 alert(`Location Error\n\n${message}\n\nYou can still use the application with manual location settings.`);132};133 134/**135 * Get browser-specific location permission instructions136 */137export const getLocationPermissionInstructions = (): string => {138 const userAgent = navigator.userAgent.toLowerCase();139 140 if (userAgent.includes('chrome')) {141 return 'Chrome: Click the location icon in the address bar and select "Allow"';142 } else if (userAgent.includes('firefox')) {143 return 'Firefox: Click the location icon in the address bar and select "Allow"';144 } else if (userAgent.includes('safari')) {145 return 'Safari: Go to Safari > Preferences > Websites > Location and allow access';146 } else if (userAgent.includes('edge')) {147 return 'Edge: Click the location icon in the address bar and select "Allow"';148 } else {149 return 'Look for a location icon in your browser\'s address bar and click "Allow"';150 }151};152 153/**154 * Validate coordinates155 */156export const validateCoordinates = (lat: number, lng: number): boolean => {157 return lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180;158};159 160/**161 * Format coordinates for display162 */163export const formatCoordinates = (lat: number, lng: number): string => {164 return `${lat.toFixed(4)}, ${lng.toFixed(4)}`;165}; 