@@ -14,97 +14,6 @@ declare global {
14
14
}
15
15
}
16
16
17
- // Global state to track script loading
18
- let isScriptLoaded = false ;
19
- let isScriptLoading = false ;
20
- let scriptLoadPromise : Promise < void > | null = null ;
21
-
22
- const loadGoogleMapsScript = ( ) : Promise < void > => {
23
- // If already loaded, return resolved promise
24
- if ( isScriptLoaded && window . google ?. maps ?. places ) {
25
- return Promise . resolve ( ) ;
26
- }
27
-
28
- // If already loading, return existing promise
29
- if ( isScriptLoading && scriptLoadPromise ) {
30
- return scriptLoadPromise ;
31
- }
32
-
33
- // Check if script is already in the document
34
- const existingScript = document . querySelector ( 'script[src*="maps.googleapis.com/maps/api/js"]' ) ;
35
- if ( existingScript && window . google ?. maps ?. places ) {
36
- isScriptLoaded = true ;
37
- return Promise . resolve ( ) ;
38
- }
39
-
40
- isScriptLoading = true ;
41
- scriptLoadPromise = new Promise < void > ( ( resolve , reject ) => {
42
- // If API is already available, resolve immediately
43
- if ( window . google ?. maps ?. places ) {
44
- isScriptLoaded = true ;
45
- isScriptLoading = false ;
46
- resolve ( ) ;
47
- return ;
48
- }
49
-
50
- // Define the callback function
51
- window . initGoogleMapsCallback = ( ) => {
52
- isScriptLoaded = true ;
53
- isScriptLoading = false ;
54
- resolve ( ) ;
55
- } ;
56
-
57
- // Create script element only if it doesn't exist
58
- if ( ! existingScript ) {
59
- const script = document . createElement ( 'script' ) ;
60
- const apiKey = import . meta. env . VITE_GOOGLE_MAPS_API_KEY ;
61
-
62
- if ( ! apiKey ) {
63
- console . error ( 'Google Maps API key is not defined. Please set VITE_GOOGLE_MAPS_API_KEY in your environment variables.' ) ;
64
- isScriptLoading = false ;
65
- reject ( new Error ( 'Google Maps API key is not defined' ) ) ;
66
- return ;
67
- }
68
-
69
- script . src = `https://maps.googleapis.com/maps/api/js?key=${ apiKey } &libraries=places&loading=async&callback=initGoogleMapsCallback` ;
70
- script . async = true ;
71
- script . defer = true ;
72
-
73
- // Handle errors
74
- script . onerror = ( error ) => {
75
- console . error ( 'Error loading Google Maps script:' , error ) ;
76
- isScriptLoading = false ;
77
- scriptLoadPromise = null ;
78
- reject ( error ) ;
79
- } ;
80
-
81
- // Append to document
82
- document . head . appendChild ( script ) ;
83
- } else {
84
- // Script exists but API might not be ready yet
85
- const checkInterval = setInterval ( ( ) => {
86
- if ( window . google ?. maps ?. places ) {
87
- isScriptLoaded = true ;
88
- isScriptLoading = false ;
89
- clearInterval ( checkInterval ) ;
90
- resolve ( ) ;
91
- }
92
- } , 100 ) ;
93
-
94
- // Clear interval after 10 seconds to prevent infinite checking
95
- setTimeout ( ( ) => {
96
- clearInterval ( checkInterval ) ;
97
- if ( ! isScriptLoaded ) {
98
- isScriptLoading = false ;
99
- reject ( new Error ( 'Google Maps API failed to load within timeout' ) ) ;
100
- }
101
- } , 10000 ) ;
102
- }
103
- } ) ;
104
-
105
- return scriptLoadPromise ;
106
- } ;
107
-
108
17
const LocationInput : React . FC < LocationInputProps > = ( { value, onChange, className } ) => {
109
18
const inputRef = useRef < HTMLInputElement > ( null ) ;
110
19
const autocompleteElementRef = useRef < any > ( null ) ;
@@ -125,6 +34,10 @@ const LocationInput: React.FC<LocationInputProps> = ({ value, onChange, classNam
125
34
setIsApiLoaded ( true ) ;
126
35
} catch ( error ) {
127
36
console . error ( 'Failed to load Google Maps API:' , error ) ;
37
+ // Ensure input is visible when Google Maps fails to load
38
+ if ( inputRef . current ) {
39
+ inputRef . current . style . display = '' ;
40
+ }
128
41
}
129
42
} ;
130
43
0 commit comments