-
-
Notifications
You must be signed in to change notification settings - Fork 303
Add precise location sharing option for chapter map #2644
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
27b4a44
Added precise location sharing option
anurag2787 7d4efc6
update
anurag2787 47e56a2
update warning
anurag2787 4d2aa62
Merge branch 'main' into location-sharing
anurag2787 cc99765
Merge branch 'main' of github.com:anurag2787/Nest into location-sharing
anurag2787 cfc8424
Fixed review
anurag2787 927f614
Merge branch 'main' into location-sharing
anurag2787 bd0111b
Merge branch 'location-sharing' of github.com:anurag2787/Nest into lo…
anurag2787 81d3bd7
sonarqube warning fixed
anurag2787 b613afe
Update code
arkid15r a5038f3
Update code
arkid15r 180d1ed
Update code
arkid15r 02ebf66
Update code
arkid15r a520752
Merge branch 'main' into pr/anurag2787/2644
arkid15r 01dae6d
Merge branch 'main' into location-sharing
arkid15r d27230b
fixed sonarqube warning:
anurag2787 2137935
Merge branch 'location-sharing' of github.com:anurag2787/Nest into lo…
anurag2787 1ea9a97
Merge branch 'main' into location-sharing
anurag2787 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,15 +1,105 @@ | ||
| import { faLocationDot } from '@fortawesome/free-solid-svg-icons' | ||
| import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
| import { Button } from '@heroui/button' | ||
| import dynamic from 'next/dynamic' | ||
| import React from 'react' | ||
| import React, { useState } from 'react' | ||
| import type { Chapter } from 'types/chapter' | ||
| import { | ||
| getUserLocationFromBrowser, | ||
| sortChaptersByDistance, | ||
| type UserLocation, | ||
| } from 'utils/geolocationUtils' | ||
|
|
||
| const ChapterMap = dynamic(() => import('./ChapterMap'), { ssr: false }) | ||
|
|
||
| const ChapterMapWrapper = (props: { | ||
| interface ChapterMapWrapperProps { | ||
| geoLocData: Chapter[] | ||
| showLocal: boolean | ||
| style: React.CSSProperties | ||
| }) => { | ||
| return <ChapterMap {...props} /> | ||
| showLocationSharing?: boolean | ||
| } | ||
|
|
||
| const ChapterMapWrapper: React.FC<ChapterMapWrapperProps> = (props) => { | ||
| const [userLocation, setUserLocation] = useState<UserLocation | null>(null) | ||
| const [isLoadingLocation, setIsLoadingLocation] = useState(false) | ||
| const [sortedData, setSortedData] = useState<Chapter[] | null>(null) | ||
|
|
||
| const enableLocationSharing = props.showLocationSharing === true | ||
|
|
||
| if (!enableLocationSharing) { | ||
| return <ChapterMap {...props} /> | ||
| } | ||
|
|
||
| const handleShareLocation = async () => { | ||
| if (userLocation) { | ||
| setUserLocation(null) | ||
| setSortedData(null) | ||
| return | ||
| } | ||
|
|
||
| setIsLoadingLocation(true) | ||
|
|
||
| try { | ||
| const location = await getUserLocationFromBrowser() | ||
|
|
||
| if (location) { | ||
| setUserLocation(location) | ||
| const sorted = sortChaptersByDistance(props.geoLocData, location) | ||
| setSortedData(sorted.map(({ _distance, ...chapter }) => chapter as unknown as Chapter)) | ||
anurag2787 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error detecting location:', error) | ||
| } finally { | ||
| setIsLoadingLocation(false) | ||
| } | ||
| } | ||
|
|
||
| const mapData = sortedData ?? props.geoLocData | ||
|
|
||
| return ( | ||
| <div className="space-y-4"> | ||
| <div className="mb-4 flex items-center gap-3 rounded-lg bg-gray-100 p-4 shadow-md dark:bg-gray-800"> | ||
| <Button | ||
| isIconOnly | ||
| className="bg-blue-500 text-white hover:bg-blue-600" | ||
| onClick={handleShareLocation} | ||
| isLoading={isLoadingLocation} | ||
| disabled={isLoadingLocation} | ||
| aria-label={ | ||
| userLocation ? 'Reset location filter' : 'Share location to find nearby chapters' | ||
| } | ||
| title={userLocation ? 'Reset location filter' : 'Share location to find nearby chapters'} | ||
| > | ||
| <FontAwesomeIcon icon={faLocationDot} size="lg" /> | ||
| </Button> | ||
|
|
||
| <div className="text-sm text-gray-700 dark:text-gray-300"> | ||
| {userLocation ? ( | ||
| <> | ||
| <div className="font-semibold text-blue-600 dark:text-blue-400"> | ||
| 📍 Showing chapters near you | ||
| </div> | ||
| <div className="text-xs text-gray-600 dark:text-gray-400"> | ||
| Location: {userLocation.latitude.toFixed(2)}, {userLocation.longitude.toFixed(2)} | ||
| </div> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <div className="font-semibold text-gray-800 dark:text-gray-200"> | ||
| Find chapters near you | ||
| </div> | ||
| <div className="text-xs text-gray-600 dark:text-gray-400"> | ||
| Click the blue button to use your current location | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
anurag2787 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </div> | ||
|
|
||
| <ChapterMap {...props} geoLocData={mapData} userLocation={userLocation} /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default ChapterMapWrapper | ||
This file contains hidden or 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,104 @@ | ||
| export interface UserLocation { | ||
| latitude: number | ||
| longitude: number | ||
| city?: string | ||
| country?: string | ||
| } | ||
anurag2787 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface ChapterCoordinates { | ||
| lat: number | null | ||
| lng: number | null | ||
| } | ||
|
|
||
| export const calculateDistance = ( | ||
| lat1: number, | ||
| lon1: number, | ||
| lat2: number, | ||
| lon2: number | ||
| ): number => { | ||
| const R = 6371 | ||
| const dLat = ((lat2 - lat1) * Math.PI) / 180 | ||
| const dLon = ((lon2 - lon1) * Math.PI) / 180 | ||
|
|
||
| const a = | ||
| Math.sin(dLat / 2) ** 2 + | ||
| Math.cos((lat1 * Math.PI) / 180) * Math.cos((lat2 * Math.PI) / 180) * Math.sin(dLon / 2) ** 2 | ||
|
|
||
| const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) | ||
| return R * c | ||
| } | ||
|
|
||
| export const getUserLocationFromBrowser = (): Promise<UserLocation | null> => { | ||
| return new Promise((resolve) => { | ||
| if (!navigator.geolocation) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('Geolocation API not supported') | ||
| resolve(null) | ||
| return | ||
| } | ||
|
|
||
| // # NOSONAR Geolocation permission is necessary for "Find chapters near you" feature. | ||
| navigator.geolocation.getCurrentPosition( | ||
| (position) => { | ||
| resolve({ | ||
| latitude: position.coords.latitude, | ||
| longitude: position.coords.longitude, | ||
| }) | ||
| }, | ||
| (error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('Browser geolocation error:', error.message) | ||
| resolve(null) | ||
| }, | ||
| { | ||
| enableHighAccuracy: false, | ||
| timeout: 10000, | ||
| maximumAge: 0, | ||
| } | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| const extractChapterCoordinates = (chapter: Record<string, unknown>): ChapterCoordinates => { | ||
| const lat = | ||
| (chapter._geoloc as Record<string, unknown>)?.lat ?? | ||
| (chapter.geoLocation as Record<string, unknown>)?.lat ?? | ||
| (chapter.geoLocation as Record<string, unknown>)?.latitude ?? | ||
| (chapter.location as Record<string, unknown>)?.lat ?? | ||
| null | ||
|
|
||
| const lng = | ||
| (chapter._geoloc as Record<string, unknown>)?.lng ?? | ||
| (chapter._geoloc as Record<string, unknown>)?.lon ?? | ||
| (chapter.geoLocation as Record<string, unknown>)?.lng ?? | ||
| (chapter.geoLocation as Record<string, unknown>)?.lon ?? | ||
| (chapter.geoLocation as Record<string, unknown>)?.longitude ?? | ||
| (chapter.location as Record<string, unknown>)?.lng ?? | ||
| (chapter.location as Record<string, unknown>)?.lon ?? | ||
| null | ||
|
|
||
| return { lat: lat as number | null, lng: lng as number | null } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Sort chapters by distance from user | ||
| */ | ||
| export const sortChaptersByDistance = ( | ||
| chapters: Record<string, unknown>[], | ||
| userLocation: UserLocation | ||
| ): Array<Record<string, unknown> & { distance: number }> => { | ||
| return chapters | ||
| .map((chapter) => { | ||
| const { lat, lng } = extractChapterCoordinates(chapter) | ||
|
|
||
| if (typeof lat !== 'number' || typeof lng !== 'number') return null | ||
|
|
||
| const distance = calculateDistance(userLocation.latitude, userLocation.longitude, lat, lng) | ||
|
|
||
| return { ...chapter, distance } | ||
| }) | ||
| .filter((item) => item !== null) // remove invalid ones | ||
| .sort((a, b) => a!.distance - b!.distance) as Array< | ||
| Record<string, unknown> & { distance: number } | ||
| > | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.