-
-
Notifications
You must be signed in to change notification settings - Fork 78
feat: Compact Route View #1131
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
Merged
Merged
feat: Compact Route View #1131
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7948832
feat: route new view first draft
Mygod 1b4a2b2
fix: various stuff
Mygod a13db4b
fix: badge text centered
Mygod af7b399
fix: align switch
Mygod 9b37e2e
fix: suggestions
Mygod 6d41fcd
fix: various improvements
Mygod 94e0626
fix: further refinements
Mygod 920cbed
fix: irreversible routes
Mygod b9813bc
fix: use lib
Mygod 01cdbcb
fix: refine code style
Mygod 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
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 |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| // @ts-check | ||
| import * as React from 'react' | ||
| import { Marker, useMapEvents } from 'react-leaflet' | ||
| import { divIcon } from 'leaflet' | ||
|
|
||
| import { useStorage } from '@store/useStorage' | ||
|
|
||
| import { RouteTile } from './RouteTile' | ||
| import { routeMarker } from './routeMarker' | ||
| import { | ||
| useRouteStore, | ||
| ROUTE_COORD_EPSILON, | ||
| getRouteCoordKey, | ||
| } from './useRouteStore' | ||
|
|
||
| const ACTIVE_Z_INDEX = 1800 | ||
| const INACTIVE_Z_INDEX = 900 | ||
|
|
||
| const RouteAnchor = React.memo( | ||
| ({ | ||
| entry, | ||
| selected, | ||
| onSelect, | ||
| routeCount, | ||
| variant = 'start', | ||
| icon = null, | ||
| }) => { | ||
| const baseIcon = React.useMemo( | ||
| () => icon || routeMarker(variant === 'destination' ? 'end' : 'start'), | ||
| [icon, variant], | ||
| ) | ||
| const badgeIcon = React.useMemo(() => { | ||
| if (routeCount <= 1) return null | ||
| return divIcon({ | ||
| className: 'route-count-wrapper', | ||
| html: `<span class="route-count-badge route-count-badge--${variant}">${routeCount}</span>`, | ||
| iconSize: [0, 0], | ||
| iconAnchor: [0, 0], | ||
| }) | ||
| }, [routeCount, variant]) | ||
|
|
||
| return ( | ||
| <> | ||
| {variant !== 'destination' && !selected && ( | ||
| <Marker | ||
| position={[entry.lat, entry.lon]} | ||
| icon={baseIcon} | ||
| zIndexOffset={INACTIVE_Z_INDEX} | ||
| riseOnHover | ||
| eventHandlers={{ | ||
| click: () => onSelect(entry.key), | ||
| }} | ||
| title={routeCount > 1 ? `${routeCount} routes` : ''} | ||
| /> | ||
| )} | ||
| {badgeIcon && ( | ||
| <Marker | ||
| position={[entry.lat, entry.lon]} | ||
| icon={badgeIcon} | ||
| interactive={false} | ||
| keyboard={false} | ||
| pane="tooltipPane" | ||
| zIndexOffset={ | ||
| selected ? ACTIVE_Z_INDEX + 200 : INACTIVE_Z_INDEX + 200 | ||
| } | ||
| /> | ||
| )} | ||
| </> | ||
| ) | ||
| }, | ||
| ) | ||
|
|
||
| const ActiveRoute = React.memo(({ selection }) => { | ||
| const route = useRouteStore( | ||
| React.useCallback( | ||
| (state) => state.routeCache[selection.routeId], | ||
| [selection.routeId], | ||
| ), | ||
| ) | ||
|
|
||
| if (!route) return null | ||
| return <RouteTile route={route} orientation={selection.orientation} /> | ||
| }) | ||
|
|
||
| export function RouteLayer({ routes }) { | ||
| const enabled = useStorage((s) => !!s.filters?.routes?.enabled) | ||
| const compactView = useStorage( | ||
| (s) => s.userSettings.routes?.compactView ?? true, | ||
| ) | ||
| const syncRoutes = useRouteStore((s) => s.syncRoutes) | ||
| const poiIndex = useRouteStore((s) => s.poiIndex) | ||
| const routeCache = useRouteStore((s) => s.routeCache) | ||
| const activeRoutes = useRouteStore((s) => s.activeRoutes) | ||
| const activePoiId = useRouteStore((s) => s.activePoiId) | ||
| const selectPoi = useRouteStore((s) => s.selectPoi) | ||
| const clearSelection = useRouteStore((s) => s.clearSelection) | ||
|
|
||
| React.useEffect(() => { | ||
| syncRoutes(routes || []) | ||
| }, [routes, syncRoutes]) | ||
|
|
||
| React.useEffect(() => { | ||
| if (!enabled || !compactView) { | ||
| clearSelection() | ||
| } | ||
| }, [enabled, compactView, clearSelection]) | ||
|
|
||
| useMapEvents({ | ||
| click: ({ originalEvent }) => { | ||
| if (!originalEvent.defaultPrevented) { | ||
| clearSelection() | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| const destinationSummary = React.useMemo(() => { | ||
| if (!compactView) | ||
| return { keys: new Set(), icons: new Map(), counts: new Map() } | ||
| const keys = new Set() | ||
| const icons = new Map() | ||
| const counts = new Map() | ||
| activeRoutes.forEach((selection) => { | ||
| const route = routeCache[selection.routeId] | ||
| if (!route) return | ||
| const isForward = selection.orientation === 'forward' | ||
| const lat = isForward ? route.end_lat : route.start_lat | ||
| const lon = isForward ? route.end_lon : route.start_lon | ||
| const coordKey = getRouteCoordKey(lat, lon) | ||
| keys.add(coordKey) | ||
| counts.set(coordKey, (counts.get(coordKey) || 0) + 1) | ||
| if (!icons.has(coordKey)) { | ||
| icons.set(coordKey, routeMarker('end')) | ||
| } | ||
| }) | ||
| return { keys, icons, counts } | ||
| }, [activeRoutes, routeCache, compactView]) | ||
|
|
||
| const anchors = React.useMemo(() => { | ||
| if (!compactView) return [] | ||
| const values = Object.values(poiIndex) | ||
| return values.map((entry) => { | ||
| const uniqueRoutes = new Set() | ||
| values.forEach((candidate) => { | ||
| if ( | ||
| Math.abs(candidate.lat - entry.lat) <= ROUTE_COORD_EPSILON && | ||
| Math.abs(candidate.lon - entry.lon) <= ROUTE_COORD_EPSILON | ||
| ) { | ||
| candidate.routes.forEach((ref) => { | ||
| if (routeCache[ref.routeId]) { | ||
| uniqueRoutes.add(ref.routeId) | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| return { | ||
| entry, | ||
| routeCount: | ||
| uniqueRoutes.size || new Set(entry.routes.map((r) => r.routeId)).size, | ||
| } | ||
| }) | ||
| }, [compactView, poiIndex, routeCache]) | ||
|
|
||
| if (!enabled) { | ||
| return null | ||
| } | ||
|
|
||
| if (!compactView) { | ||
| return ( | ||
| <> | ||
| {routes.map((route) => ( | ||
| <RouteTile key={route.id} route={route} /> | ||
| ))} | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {anchors.map(({ entry, routeCount }) => { | ||
| const entryCoordKey = getRouteCoordKey(entry.lat, entry.lon) | ||
| const iconOverride = destinationSummary.icons.get(entryCoordKey) | ||
| const destinationCount = destinationSummary.counts.get(entryCoordKey) | ||
| if (destinationCount && destinationCount > 1) { | ||
| return ( | ||
| <RouteAnchor | ||
| key={`${entry.key}-destination`} | ||
| entry={entry} | ||
| selected={entry.key === activePoiId} | ||
| onSelect={selectPoi} | ||
| routeCount={destinationCount} | ||
| variant="destination" | ||
| icon={iconOverride || undefined} | ||
| /> | ||
| ) | ||
| } | ||
| if ( | ||
| destinationSummary.keys.has(entryCoordKey) && | ||
| entry.key !== activePoiId | ||
| ) { | ||
| return null | ||
| } | ||
| return ( | ||
| <RouteAnchor | ||
| key={entry.key} | ||
| entry={entry} | ||
| selected={entry.key === activePoiId} | ||
| onSelect={selectPoi} | ||
| routeCount={destinationCount || routeCount} | ||
| variant="start" | ||
| icon={iconOverride || undefined} | ||
| /> | ||
| ) | ||
| })} | ||
| {activeRoutes.map((selection) => ( | ||
| <ActiveRoute | ||
| key={`${selection.routeId}-${selection.orientation}`} | ||
| selection={selection} | ||
| /> | ||
| ))} | ||
| </> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.