-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
833 additions
and
86 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { useAppConfig } from '@/hooks/use-app-config'; | ||
import { ISearchResult } from '@/types/search-result'; | ||
import { LngLatLike } from 'maplibre-gl'; | ||
import dynamic from 'next/dynamic'; | ||
import { memo } from 'react'; | ||
|
||
export default function MapLoader(props) { | ||
interface IMapLoaderProps { | ||
results?: ISearchResult[]; | ||
center?: LngLatLike; | ||
zoom?: number; | ||
animate?: boolean; | ||
boundsPadding?: number; | ||
boundsZoom?: number; | ||
} | ||
|
||
const MapLoader = memo<IMapLoaderProps>(function MemoizedMapLoader(props) { | ||
const appConfig = useAppConfig(); | ||
const mapAdapterName = appConfig.adapters.map; | ||
const MapComponent = dynamic(() => | ||
const MapComponent = dynamic<IMapLoaderProps>(() => | ||
import(`./${mapAdapterName}`).then((mod) => mod.default), | ||
); | ||
|
||
return <MapComponent {...props} />; | ||
} | ||
}); | ||
|
||
export default MapLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { ISearchResult } from '@/types/search-result'; | ||
import { memo } from 'react'; | ||
import Marker from './marker'; | ||
import RenderHtml from '@/components/render-html'; | ||
import { ReferralButton } from '@/components/referral-button'; | ||
import { Globe, Phone } from 'lucide-react'; | ||
import { useTranslation } from 'next-i18next'; | ||
|
||
export const Markers = memo(function MemoizedMarkers({ | ||
results, | ||
zoom, | ||
}: { | ||
results: ISearchResult[]; | ||
zoom?: number; | ||
}) { | ||
const { t } = useTranslation(); | ||
|
||
if (!results || results.length === 0) return null; | ||
|
||
return ( | ||
<> | ||
{results.map((result) => { | ||
if (result?.location?.point?.coordinates == null) return null; | ||
|
||
return ( | ||
<Marker | ||
key={result.id} | ||
longitude={result.location.point.coordinates[0]} | ||
latitude={result.location.point.coordinates[1]} | ||
className="custom-marker" | ||
zoom={zoom} | ||
onClick={(e, marker) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
const element = document.getElementById(result.id); | ||
|
||
document | ||
.querySelectorAll('.outline') | ||
.forEach((elem) => elem.classList.remove('outline')); | ||
|
||
if (element) { | ||
element.classList.add('outline'); | ||
element.scrollIntoView(); | ||
} | ||
}} | ||
popup={ | ||
<div className="flex flex-col gap-2"> | ||
<h3 className="text-lg font-semibold">{result.name}</h3> | ||
|
||
<div className="text-sm"> | ||
<RenderHtml html={result?.description} /> | ||
</div> | ||
|
||
<div className="flex gap-2"> | ||
<ReferralButton | ||
referralType="call_referral" | ||
resourceId={result.id} | ||
resource={result} | ||
disabled={!result.phone} | ||
href={`tel:${result.phone}`} | ||
className="w-full min-w-[130px] gap-1" | ||
> | ||
<Phone className="size-4" /> | ||
{t('call_to_action.call', { | ||
ns: 'common', | ||
})} | ||
</ReferralButton> | ||
|
||
<ReferralButton | ||
referralType="website_referral" | ||
resourceId={result.id} | ||
resource={result} | ||
disabled={!result.website} | ||
href={result?.website ?? ''} | ||
target="_blank" | ||
className="w-full min-w-[130px] gap-1" | ||
> | ||
<Globe className="size-4" /> | ||
{t('call_to_action.view_website', { | ||
ns: 'common', | ||
})} | ||
</ReferralButton> | ||
</div> | ||
</div> | ||
} | ||
/> | ||
); | ||
})} | ||
</> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Radar from 'radar-sdk-js'; | ||
import RadarMap from 'radar-sdk-js/dist/ui/RadarMap'; | ||
import { useEffect, useState } from 'react'; | ||
import { MapContextProvider } from '../mapContext'; | ||
import 'radar-sdk-js/dist/radar.css'; | ||
|
||
export default function Map({ | ||
children, | ||
publishableKey, | ||
zoom, | ||
center, | ||
}: { | ||
children?: React.ReactNode; | ||
publishableKey: string; | ||
zoom?: number; | ||
center?: [number, number]; | ||
}) { | ||
const [map, setMap] = useState<RadarMap | undefined>(); | ||
|
||
useEffect(() => { | ||
Radar.initialize(publishableKey); | ||
const _map = Radar.ui.map({ | ||
container: 'map', | ||
style: 'radar-default-v1', | ||
zoom: zoom || 0, | ||
center, | ||
}); | ||
setMap(_map); | ||
|
||
return () => { | ||
_map.remove(); | ||
setMap(undefined); | ||
}; | ||
}, [publishableKey, zoom, center]); | ||
|
||
return ( | ||
<MapContextProvider value={{ map }}> | ||
<div id="map-container" className="aboslute h-full w-full"> | ||
<div id="map" className="absolute h-full w-full"> | ||
{children} | ||
</div> | ||
</div> | ||
</MapContextProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ReactElement, useContext, useEffect } from 'react'; | ||
import { mapContext } from '../mapContext'; | ||
import Radar from 'radar-sdk-js'; | ||
import { renderToStaticMarkup } from 'react-dom/server'; | ||
import RadarMarker from 'radar-sdk-js/dist/ui/RadarMarker'; | ||
|
||
export default function Marker({ | ||
latitude, | ||
longitude, | ||
popup, | ||
className, | ||
onClick, | ||
zoom, | ||
}: { | ||
latitude?: number; | ||
longitude?: number; | ||
popup?: ReactElement; | ||
className?: string; | ||
onClick?: (e: MouseEvent, marker: RadarMarker) => void; | ||
zoom?: number; | ||
}) { | ||
const { map } = useContext(mapContext); | ||
|
||
useEffect(() => { | ||
if (map == null) return; | ||
if (latitude == null || longitude == null) return; | ||
|
||
const container = popup ? document.createElement('div') : undefined; | ||
if (container != null && popup != null) { | ||
container.innerHTML = renderToStaticMarkup(popup); | ||
} | ||
|
||
const marker = Radar.ui | ||
.marker({ | ||
popup: { | ||
element: container, | ||
maxWidth: '320px', | ||
}, | ||
}) | ||
.setLngLat([longitude, latitude]) | ||
.addTo(map); | ||
|
||
if (className != null) { | ||
marker.getElement().classList.add(className); | ||
} | ||
|
||
const clickListener = (e: any) => { | ||
return onClick?.(e, marker); | ||
}; | ||
|
||
marker.getElement().addEventListener('click', clickListener); | ||
|
||
map?.fitToMarkers({ animate: false }); | ||
|
||
return () => { | ||
marker.remove(); | ||
}; | ||
}, [longitude, latitude, map, popup, className, onClick, zoom]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useMemo } from 'react'; | ||
import { getPublicConfig } from '@/pages/api/config'; | ||
import Map from './components/map'; | ||
import { useAppConfig } from '@/hooks/use-app-config'; | ||
import { Markers } from './components/map-markers'; | ||
|
||
export default function RadarMap(props) { | ||
const appConfig = useAppConfig(); | ||
const RADAR_ACCESS_TOKEN = getPublicConfig('RADAR_ACCESS_TOKEN'); | ||
|
||
const mapProps = useMemo( | ||
() => ({ | ||
publishableKey: RADAR_ACCESS_TOKEN, | ||
center: appConfig?.features?.map?.center, | ||
zoom: 12, | ||
}), | ||
[RADAR_ACCESS_TOKEN, appConfig], | ||
); | ||
|
||
return ( | ||
<Map {...mapProps} {...props}> | ||
<Markers results={props.results} zoom={props.boundsZoom} /> | ||
</Map> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import RadarMap from 'radar-sdk-js/dist/ui/RadarMap'; | ||
import { createContext } from 'react'; | ||
|
||
interface IMapContext { | ||
map?: RadarMap; | ||
} | ||
|
||
export const mapContext = createContext<IMapContext>({}); | ||
export const MapContextProvider = mapContext.Provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.