enh
This commit is contained in:
parent
a7456473e9
commit
d8e4b4c41d
3 changed files with 165 additions and 24 deletions
|
|
@ -3,18 +3,45 @@ import { View } from 'react-native'
|
||||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
|
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
|
||||||
import { tw, theme, MyText, MyTouchableOpacity } from 'common-ui'
|
import { tw, theme, MyText, MyTouchableOpacity } from 'common-ui'
|
||||||
import { useRegion } from '@/src/hooks/useRegion'
|
import { useRegion } from '@/src/hooks/useRegion'
|
||||||
|
import { useAddressStore } from '@/src/store/addressStore'
|
||||||
|
import { useAuthOptional } from '@/src/contexts/AuthContext'
|
||||||
|
import { trpc } from '@/src/trpc-client'
|
||||||
import AddressSwitcherSheet from '@/src/components/location/AddressSwitcherSheet'
|
import AddressSwitcherSheet from '@/src/components/location/AddressSwitcherSheet'
|
||||||
|
|
||||||
|
const formatAddress = (address: {
|
||||||
|
addressLine1: string
|
||||||
|
addressLine2?: string | null
|
||||||
|
city: string
|
||||||
|
pincode: string
|
||||||
|
}) =>
|
||||||
|
`${address.addressLine1}${address.addressLine2 ? `, ${address.addressLine2}` : ''}, ${address.city} - ${address.pincode}`
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Location bar shown at the top of every top-level tab screen.
|
* Location bar shown at the top of every top-level tab screen.
|
||||||
* Rendered INSIDE the scroll content (first item / ListHeaderComponent) so it
|
* Rendered INSIDE the scroll content (first item / ListHeaderComponent) so it
|
||||||
* scrolls out with the page instead of sticking. Tapping opens the address
|
* scrolls out with the page instead of sticking. Tapping opens the address
|
||||||
* switcher: pick a saved address (region follows it) or add a new one.
|
* switcher: pick a saved address (region follows it) or add a new one.
|
||||||
|
*
|
||||||
|
* Shows "Deliver to: <region>" (truncated) on the first line and the active
|
||||||
|
* address beneath it.
|
||||||
*/
|
*/
|
||||||
const LocationBar: React.FC = () => {
|
const LocationBar: React.FC = () => {
|
||||||
const [sheetOpen, setSheetOpen] = useState(false)
|
const [sheetOpen, setSheetOpen] = useState(false)
|
||||||
const { region } = useRegion()
|
const { region } = useRegion()
|
||||||
|
|
||||||
|
const isAuthenticated = useAuthOptional()?.isAuthenticated ?? false
|
||||||
|
const selectedAddressId = useAddressStore((state) => state.selectedAddressId)
|
||||||
|
const { data: addressesData } = trpc.user.address.getUserAddresses.useQuery(undefined, {
|
||||||
|
enabled: isAuthenticated,
|
||||||
|
})
|
||||||
|
|
||||||
|
const addresses = addressesData?.data ?? []
|
||||||
|
// Prefer the address the user selected at checkout, else their default.
|
||||||
|
const activeAddress =
|
||||||
|
addresses.find((address) => address.id === selectedAddressId) ??
|
||||||
|
addresses.find((address) => address.isDefault) ??
|
||||||
|
null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={tw`px-4 pt-3 pb-1`}>
|
<View style={tw`px-4 pt-3 pb-1`}>
|
||||||
<MyTouchableOpacity
|
<MyTouchableOpacity
|
||||||
|
|
@ -27,12 +54,21 @@ const LocationBar: React.FC = () => {
|
||||||
<MaterialIcons name="location-on" size={20} color={theme.colors.brand500} />
|
<MaterialIcons name="location-on" size={20} color={theme.colors.brand500} />
|
||||||
</View>
|
</View>
|
||||||
<View style={tw`flex-1`}>
|
<View style={tw`flex-1`}>
|
||||||
<MyText style={tw`text-gray-400 text-[11px] font-semibold uppercase tracking-wider`}>
|
<MyText style={tw`text-sm text-gray-500`} numberOfLines={1} ellipsizeMode="tail">
|
||||||
Deliver to
|
Deliver to:{' '}
|
||||||
</MyText>
|
<MyText weight="bold" style={tw`text-gray-900`}>
|
||||||
<MyText style={tw`text-gray-900 text-base font-bold`} numberOfLines={1}>
|
|
||||||
{region?.name ?? 'Choose your address'}
|
{region?.name ?? 'Choose your address'}
|
||||||
</MyText>
|
</MyText>
|
||||||
|
</MyText>
|
||||||
|
{activeAddress ? (
|
||||||
|
<MyText
|
||||||
|
style={tw`text-xs text-gray-500 mt-0.5`}
|
||||||
|
numberOfLines={2}
|
||||||
|
ellipsizeMode="tail"
|
||||||
|
>
|
||||||
|
{formatAddress(activeAddress)}
|
||||||
|
</MyText>
|
||||||
|
) : null}
|
||||||
</View>
|
</View>
|
||||||
<MaterialIcons name="keyboard-arrow-down" size={24} color="#9CA3AF" />
|
<MaterialIcons name="keyboard-arrow-down" size={24} color="#9CA3AF" />
|
||||||
</MyTouchableOpacity>
|
</MyTouchableOpacity>
|
||||||
|
|
|
||||||
|
|
@ -36,13 +36,27 @@ interface GoogleMarker {
|
||||||
setMap: (map: GoogleMap | null) => void
|
setMap: (map: GoogleMap | null) => void
|
||||||
setPosition: (position: LatLngLiteral) => void
|
setPosition: (position: LatLngLiteral) => void
|
||||||
}
|
}
|
||||||
|
interface GoogleMapConstructor {
|
||||||
|
new (element: HTMLElement, options: Record<string, unknown>): GoogleMap
|
||||||
|
}
|
||||||
|
interface GoogleMarkerConstructor {
|
||||||
|
new (options: Record<string, unknown>): GoogleMarker
|
||||||
|
}
|
||||||
|
// The resolved `maps` + `marker` libraries (both constructors present).
|
||||||
|
interface GoogleMapsApi {
|
||||||
|
Map: GoogleMapConstructor
|
||||||
|
Marker: GoogleMarkerConstructor
|
||||||
|
}
|
||||||
|
type GoogleImportLibrary = (library: string, ...rest: string[]) => Promise<any>
|
||||||
|
// The global `google.maps` namespace, including the dynamic-import entry point.
|
||||||
interface GoogleMapsNamespace {
|
interface GoogleMapsNamespace {
|
||||||
Map: new (element: HTMLElement, options: Record<string, unknown>) => GoogleMap
|
importLibrary?: GoogleImportLibrary
|
||||||
Marker: new (options: Record<string, unknown>) => GoogleMarker
|
// Set by the bootstrap; the API invokes it once the namespace is fully ready.
|
||||||
|
__ib__?: () => void
|
||||||
}
|
}
|
||||||
type ReactNativeWebViewBridge = { postMessage: (message: string) => void }
|
type ReactNativeWebViewBridge = { postMessage: (message: string) => void }
|
||||||
type GoogleWindow = Window & {
|
type GoogleWindow = Window & {
|
||||||
google?: { maps: GoogleMapsNamespace }
|
google?: { maps?: GoogleMapsNamespace }
|
||||||
// Injected by react-native-webview when this page is embedded in user-ui.
|
// Injected by react-native-webview when this page is embedded in user-ui.
|
||||||
ReactNativeWebView?: ReactNativeWebViewBridge
|
ReactNativeWebView?: ReactNativeWebViewBridge
|
||||||
}
|
}
|
||||||
|
|
@ -56,26 +70,82 @@ const DEFAULT_ZOOM = 12
|
||||||
// VITE_GOOGLE_MAPS_API_KEY=your-key
|
// VITE_GOOGLE_MAPS_API_KEY=your-key
|
||||||
const GOOGLE_MAPS_API_KEY = import.meta.env.VITE_GOOGLE_MAPS_API_KEY as string | undefined
|
const GOOGLE_MAPS_API_KEY = import.meta.env.VITE_GOOGLE_MAPS_API_KEY as string | undefined
|
||||||
|
|
||||||
let googleMapsLoader: Promise<GoogleMapsNamespace> | null = null
|
let googleMapsLoader: Promise<GoogleMapsApi> | null = null
|
||||||
|
|
||||||
function loadGoogleMaps(apiKey: string): Promise<GoogleMapsNamespace> {
|
/**
|
||||||
const existing = (window as GoogleWindow).google?.maps
|
* Google's recommended "Dynamic Library Import" bootstrap
|
||||||
if (existing) return Promise.resolve(existing)
|
* (https://goo.gle/js-api-loading).
|
||||||
if (googleMapsLoader) return googleMapsLoader
|
*
|
||||||
|
* It defines `google.maps.importLibrary` synchronously and loads the API with
|
||||||
|
* the `callback` signal, so we never read `google.maps` before it exists. A raw
|
||||||
|
* `<script onload>` check races with `loading=async` (onload fires before the
|
||||||
|
* namespace is populated) and reports the namespace as unavailable.
|
||||||
|
*/
|
||||||
|
function ensureImportLibrary(apiKey: string): GoogleImportLibrary {
|
||||||
|
const w = window as GoogleWindow
|
||||||
|
const g = (w.google ??= {})
|
||||||
|
const maps = (g.maps ??= {})
|
||||||
|
if (maps.importLibrary) return maps.importLibrary
|
||||||
|
|
||||||
|
const requested = new Set<string>()
|
||||||
|
let loading: Promise<void> | null = null
|
||||||
|
|
||||||
|
const load = () => {
|
||||||
|
if (loading) return loading
|
||||||
|
loading = new Promise<void>((resolve, reject) => {
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
params.set('key', apiKey)
|
||||||
|
params.set('v', 'weekly')
|
||||||
|
params.set('libraries', Array.from(requested).join(','))
|
||||||
|
params.set('loading', 'async')
|
||||||
|
params.set('callback', 'google.maps.__ib__')
|
||||||
|
|
||||||
googleMapsLoader = new Promise<GoogleMapsNamespace>((resolve, reject) => {
|
|
||||||
const script = document.createElement('script')
|
const script = document.createElement('script')
|
||||||
script.src = `https://maps.googleapis.com/maps/api/js?key=${encodeURIComponent(apiKey)}`
|
script.src = `https://maps.googleapis.com/maps/api/js?${params.toString()}`
|
||||||
script.async = true
|
script.async = true
|
||||||
script.defer = true
|
const nonce = document.querySelector('script[nonce]')?.getAttribute('nonce')
|
||||||
script.onload = () => {
|
if (nonce) script.setAttribute('nonce', nonce)
|
||||||
const maps = (window as GoogleWindow).google?.maps
|
script.onerror = () => reject(new Error('The Google Maps JavaScript API could not load.'))
|
||||||
if (maps) resolve(maps)
|
// The API calls this once it — and its namespace — is ready.
|
||||||
else reject(new Error('Google Maps loaded but the maps namespace is unavailable.'))
|
maps.__ib__ = () => resolve()
|
||||||
}
|
|
||||||
script.onerror = () => reject(new Error('Failed to load the Google Maps script.'))
|
|
||||||
document.head.appendChild(script)
|
document.head.appendChild(script)
|
||||||
})
|
})
|
||||||
|
return loading
|
||||||
|
}
|
||||||
|
|
||||||
|
const importLibrary: GoogleImportLibrary = (library, ...rest) => {
|
||||||
|
requested.add(library)
|
||||||
|
for (const name of rest) requested.add(name)
|
||||||
|
return load().then(() => {
|
||||||
|
const real = maps.importLibrary
|
||||||
|
if (!real || real === importLibrary) {
|
||||||
|
throw new Error('Google Maps importLibrary did not initialize.')
|
||||||
|
}
|
||||||
|
return real(library, ...rest)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
maps.importLibrary = importLibrary
|
||||||
|
return importLibrary
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadGoogleMaps(apiKey: string): Promise<GoogleMapsApi> {
|
||||||
|
if (googleMapsLoader) return googleMapsLoader
|
||||||
|
|
||||||
|
googleMapsLoader = (async () => {
|
||||||
|
const importLibrary = ensureImportLibrary(apiKey)
|
||||||
|
// Register both libraries so the bootstrap fetches them together.
|
||||||
|
const [mapsLib, markerLib] = await Promise.all([
|
||||||
|
importLibrary('maps'),
|
||||||
|
importLibrary('marker'),
|
||||||
|
])
|
||||||
|
const Map: GoogleMapConstructor | undefined = mapsLib?.Map
|
||||||
|
const Marker: GoogleMarkerConstructor | undefined = markerLib?.Marker
|
||||||
|
if (!Map || !Marker) {
|
||||||
|
throw new Error('Google Maps loaded but the maps/marker libraries are unavailable.')
|
||||||
|
}
|
||||||
|
return { Map, Marker }
|
||||||
|
})()
|
||||||
|
|
||||||
return googleMapsLoader
|
return googleMapsLoader
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3711,3 +3711,38 @@ user-ui
|
||||||
localuserweb.freshyo.in -> http://localhost:4174; DNS CNAME routed; running detached.
|
localuserweb.freshyo.in -> http://localhost:4174; DNS CNAME routed; running detached.
|
||||||
- apps/web-ui/vite.config.ts: add `server.allowedHosts: ['.freshyo.in']` so the Vite
|
- apps/web-ui/vite.config.ts: add `server.allowedHosts: ['.freshyo.in']` so the Vite
|
||||||
dev server does not 403 the tunneled hostname (same fix as superadmin-ui). No tests run.
|
dev server does not 403 the tunneled hostname (same fix as superadmin-ui). No tests run.
|
||||||
|
|
||||||
|
2026-10-13 14:04:49 LocationBar (user-ui): show the active address beneath the region.
|
||||||
|
apps/user-ui/src/components/LocationBar.tsx:
|
||||||
|
+ reads selectedAddressId (useAddressStore) and user.address.getUserAddresses (auth-gated);
|
||||||
|
active address = selected -> default -> none
|
||||||
|
+ line 1 (single line, truncated with ellipsis): "Deliver to: <region name>"
|
||||||
|
+ next line(s): the formatted address (line1[, line2], city - pincode), max 2 lines
|
||||||
|
- removed the separate uppercase "Deliver to" label row
|
||||||
|
No dependency added. No migration. No tests run.
|
||||||
|
|
||||||
|
2026-10-13 14:08:13 web-ui /location-selector: load the Google Maps JS API with the recommended async pattern.
|
||||||
|
The loader added the script without `loading=async`, which triggers the console warning
|
||||||
|
"Google Maps JavaScript API has been loaded directly without loading=async".
|
||||||
|
1/1 apps/web-ui/src/routes/location-selector.tsx:
|
||||||
|
- script.src = `https://maps.googleapis.com/maps/api/js?key=${...}`
|
||||||
|
+ script.src = `https://maps.googleapis.com/maps/api/js?key=${...}&loading=async`
|
||||||
|
+ after onload, resolve Map/Marker via google.maps.importLibrary('maps'|'marker')
|
||||||
|
(falls back to the legacy google.maps.Map/.Marker if importLibrary is absent)
|
||||||
|
+ split the structural types: GoogleMapsApi { Map, Marker } (resolved) vs GoogleMapsNamespace
|
||||||
|
(window.google.maps, optional constructors + importLibrary)
|
||||||
|
No dependency added. No migration. No tests run.
|
||||||
|
|
||||||
|
2026-10-13 14:14:50 web-ui /location-selector: fix the Maps readiness race using Google's recommended dynamic
|
||||||
|
library import. The prior `loading=async` change read window.google.maps in the script `onload`, but the async
|
||||||
|
bootstrap defines the namespace only afterwards, so the loader rejected with
|
||||||
|
"Google Maps loaded but the maps namespace is unavailable."
|
||||||
|
1/1 apps/web-ui/src/routes/location-selector.tsx:
|
||||||
|
- raw <script onload> readiness check (races with loading=async)
|
||||||
|
+ ensureImportLibrary(apiKey): installs Google's official dynamic-import bootstrap. It defines
|
||||||
|
google.maps.importLibrary synchronously and loads
|
||||||
|
https://maps.googleapis.com/maps/api/js?key&v=weekly&libraries=...&loading=async&callback=google.maps.__ib__ ,
|
||||||
|
resolving the load promise from the callback (no early namespace read).
|
||||||
|
+ loadGoogleMaps now resolves Map/Marker via google.maps.importLibrary('maps'|'marker')
|
||||||
|
+ GoogleMapsNamespace type narrowed to { importLibrary?, __ib__? }
|
||||||
|
No dependency added. No migration. No tests run.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue