Skip to content

Commit

Permalink
cosmetics: avoid layer and profile parameter on first load if equal t…
Browse files Browse the repository at this point in the history
…o default
  • Loading branch information
karussell committed May 27, 2024
1 parent 268b79e commit 717ca0e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/NavBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import QueryStore, {
} from '@/stores/QueryStore'
import MapOptionsStore, { MapOptionsStoreState } from './stores/MapOptionsStore'
import { getApi } from '@/api/Api'
import config from 'config'

export default class NavBar {
private readonly queryStore: QueryStore
Expand All @@ -27,21 +28,28 @@ export default class NavBar {

async startSyncingUrlWithAppState() {
// our first history entry shall be the one that we end up with when the app loads for the first time
window.history.replaceState(null, '', this.createUrlFromState())
window.history.replaceState(null, '', this.createUrlFromState(true))
this.queryStore.register(() => this.updateUrlFromState())
this.mapStore.register(() => this.updateUrlFromState())
}

private static createUrl(baseUrl: string, queryStoreState: QueryStoreState, mapState: MapOptionsStoreState) {
private static createUrl(
baseUrl: string,
queryStoreState: QueryStoreState,
mapState: MapOptionsStoreState,
first: boolean
) {
const result = new URL(baseUrl)
if (queryStoreState.queryPoints.filter(point => point.isInitialized).length > 0) {
queryStoreState.queryPoints
.map(point => (!point.isInitialized ? '' : NavBar.pointToParam(point)))
.forEach(pointAsString => result.searchParams.append('point', pointAsString))
}

result.searchParams.append('profile', queryStoreState.routingProfile.name)
result.searchParams.append('layer', mapState.selectedStyle.name)
if (!first || queryStoreState.routingProfile.name != Object.keys(config.profiles ?? {})[0])
result.searchParams.append('profile', queryStoreState.routingProfile.name)
if (!first || mapState.selectedStyle.name != config.defaultTiles)
result.searchParams.append('layer', mapState.selectedStyle.name)
if (queryStoreState.customModelEnabled)
result.searchParams.append('custom_model', queryStoreState.customModelStr.replace(/\s+/g, ''))

Expand Down Expand Up @@ -158,15 +166,16 @@ export default class NavBar {

public updateUrlFromState() {
if (this.ignoreStateUpdates) return
const newHref = this.createUrlFromState()
const newHref = this.createUrlFromState(false)
if (newHref !== window.location.href) window.history.pushState(null, '', newHref)
}

private createUrlFromState() {
private createUrlFromState(first: boolean) {
return NavBar.createUrl(
window.location.origin + window.location.pathname,
this.queryStore.state,
this.mapStore.state
this.mapStore.state,
first
).toString()
}

Expand Down

0 comments on commit 717ca0e

Please sign in to comment.