Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves home page chapter map #1009

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions frontend/src/components/ChapterMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ChapterMap = ({
style: React.CSSProperties
}) => {
const mapRef = useRef<L.Map | null>(null)
const markerClusterRef = useRef<L.MarkerClusterGroup | null>(null)

const normalizedData = useMemo(() => {
return geoLocData.map((chapter) => ({
Expand All @@ -24,48 +25,60 @@ const ChapterMap = ({
}))
}, [geoLocData])

//for reference: https://leafletjs.com/reference.html#map-example
useEffect(() => {
if (!mapRef.current) {
mapRef.current = L.map('chapter-map', {
worldCopyJump: false, // Prevents the map from wrapping around the world
worldCopyJump: false,
maxBounds: [
[-90, -180], // Southwest corner of the map bounds (latitude, longitude)
[90, 180], // Northeast corner of the map bounds (latitude, longitude)
[-90, -180],
[90, 180],
],
maxBoundsViscosity: 1.0,
}).setView([20, 0], 2)

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors',
className: 'map-tiles',
}).addTo(mapRef.current)
}

const map = mapRef.current

// Remove previous markers
map.eachLayer((layer) => {
if (layer instanceof L.Marker || layer instanceof L.LayerGroup) {
map.removeLayer(layer)
}
})

// Create a new marker cluster group
const markerClusterGroup = L.markerClusterGroup()
const bounds: [number, number][] = []
normalizedData.forEach((chapter) => {
markerClusterRef.current = markerClusterGroup

// Validate and filter out invalid coordinates
const validChapters = normalizedData.filter(
(chapter) =>
chapter.lat !== null &&
chapter.lng !== null &&
!isNaN(chapter.lat) &&
!isNaN(chapter.lng) &&
chapter.lat >= -90 &&
chapter.lat <= 90 &&
chapter.lng >= -180 &&
chapter.lng <= 180
)

// Create markers for all chapters
validChapters.forEach((chapter) => {
const markerIcon = new L.Icon({
iconAnchor: [12, 41], // Anchor point
iconAnchor: [12, 41],
iconRetinaUrl: '/img/marker-icon-2x.png',
iconSize: [25, 41], // Default size for Leaflet markers
iconSize: [25, 41],
iconUrl: '/img/marker-icon.png',
popupAnchor: [1, -34], // Popup position relative to marker
shadowSize: [41, 41], // Shadow size
popupAnchor: [1, -34],
shadowSize: [41, 41],
shadowUrl: '/img/marker-shadow.png',
})
const marker = L.marker([chapter.lat, chapter.lng], {
icon: markerIcon,
})
const marker = L.marker([chapter.lat, chapter.lng], { icon: markerIcon })
const popup = L.popup()
const popupContent = document.createElement('div')
popupContent.className = 'popup-content'
Expand All @@ -81,12 +94,34 @@ const ChapterMap = ({

map.addLayer(markerClusterGroup)

if (bounds.length > 0) {
map.fitBounds(bounds as L.LatLngBoundsExpression, { maxZoom: 10 })
// Determine map view based on 6th index (index 5)
try {
if (validChapters.length >= 6) {
// Specifically target the 6th chapter (index 5)
const sixthChapter = validChapters[5]

// Take the first 6 chapters for bounds
const localChapters = validChapters.slice(0, 6)
const temp = localChapters.map((ch) => [ch.lat, ch.lng])
const localBounds = L.latLngBounds(temp)

map.setView([sixthChapter.lat, sixthChapter.lng], 6)
map.fitBounds(localBounds, { maxZoom: 10 })
} else if (validChapters.length > 0) {
// Fallback if fewer than 6 chapters
const firstChapter = validChapters[0]
map.setView([firstChapter.lat, firstChapter.lng], 6)
} else if (bounds.length > 0) {
map.fitBounds(bounds)
} else {
map.setView([20, 0], 2)
}
} catch {
map.setView([20, 0], 2)
}
}, [normalizedData])

return <div id="chapter-map" className="rounded-lg dark:bg-[#212529]" style={style} />
return <div id="chapter-map" style={style} />
}

export default ChapterMap
4 changes: 4 additions & 0 deletions frontend/src/pages/Chapters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SearchPageLayout from 'components/SearchPageLayout'

const ChaptersPage = () => {
const [geoLocData, setGeoLocData] = useState<ChapterTypeAlgolia[]>([])

const {
items: chapters,
isLoaded,
Expand All @@ -35,14 +36,17 @@ const ChaptersPage = () => {
currentPage: 1,
hitsPerPage: 1000,
}

const data: AlgoliaResponseType<ChapterTypeAlgolia> = await fetchAlgoliaData(
searchParams.indexName,
searchParams.query,
searchParams.currentPage,
searchParams.hitsPerPage
)

setGeoLocData(data.hits)
}

fetchData()
}, [])

Expand Down