feat: handle basic routes (#1784)
This commit is contained in:
50
packages/ui-services/src/Route/RouteService.ts
Normal file
50
packages/ui-services/src/Route/RouteService.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
AbstractService,
|
||||
ApplicationEvent,
|
||||
ApplicationInterface,
|
||||
InternalEventBusInterface,
|
||||
} from '@standardnotes/services'
|
||||
import { RouteParser } from './RouteParser'
|
||||
import { RouteServiceEvent } from './RouteServiceEvent'
|
||||
|
||||
export class RouteService extends AbstractService<RouteServiceEvent, RouteParser> {
|
||||
private unsubApp!: () => void
|
||||
|
||||
constructor(
|
||||
private application: ApplicationInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
this.addAppEventObserver()
|
||||
}
|
||||
|
||||
override deinit() {
|
||||
super.deinit()
|
||||
;(this.application as unknown) = undefined
|
||||
this.unsubApp()
|
||||
}
|
||||
|
||||
public getRoute(): RouteParser {
|
||||
return new RouteParser(window.location.href)
|
||||
}
|
||||
|
||||
public removeSettingsFromURLQueryParameters() {
|
||||
const urlSearchParams = new URLSearchParams(window.location.search)
|
||||
urlSearchParams.delete('settings')
|
||||
|
||||
const newUrl = `${window.location.origin}${window.location.pathname}${urlSearchParams.toString()}`
|
||||
window.history.replaceState(null, document.title, newUrl)
|
||||
}
|
||||
|
||||
private addAppEventObserver() {
|
||||
this.unsubApp = this.application.addEventObserver(async (event: ApplicationEvent) => {
|
||||
if (event === ApplicationEvent.LocalDataLoaded) {
|
||||
void this.notifyRouteChange()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private notifyRouteChange() {
|
||||
void this.notifyEvent(RouteServiceEvent.RouteChanged, this.getRoute())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user