mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
feat(client): open TREK on the active trip when asked to
Settings -> General -> Startup: start on the dashboard (unchanged default) or straight in the active trip, on a tab of your choice. Three clicks and three loads less for someone entering expenses while actually travelling, which is where the request came from. The redirect sits on '/' because that is what a shortcut, a bookmark and the installed PWA all launch (manifest start_url). /dashboard keeps working as itself - redirecting there too would leave no way back to it. The preference is mirrored into localStorage, the way the language and the theme snapshot already are, so the first paint can decide without waiting on GET /api/settings. That keeps the default launch exactly as fast as before: only someone who asked for the trip pays for the lookup, and if it fails or they have no trip they get the dashboard. Right after login the store already holds the real settings, so those win there. Login stops hardcoding /dashboard as its fallback and hands the decision to the same place, so there is one answer to "where does TREK open".
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { isTripTabId } from '../constants/tripTabs'
|
||||
import type { Settings } from '../types'
|
||||
|
||||
/**
|
||||
* Where opening TREK lands. 'dashboard' is what TREK has always done; with
|
||||
* 'active_trip' the app skips straight into the trip the dashboard would
|
||||
* feature, on the tab picked below — three clicks and three loads less for
|
||||
* someone entering expenses while they're actually travelling.
|
||||
*/
|
||||
export type StartPage = 'dashboard' | 'active_trip'
|
||||
|
||||
export const DEFAULT_START_PAGE: StartPage = 'dashboard'
|
||||
export const DEFAULT_START_TRIP_TAB = 'plan'
|
||||
|
||||
/**
|
||||
* The route that resolves the destination (RootRedirect in App.tsx). Anywhere
|
||||
* that used to navigate to '/dashboard' for lack of a better idea sends people
|
||||
* here instead, so the preference is applied in exactly one place.
|
||||
*/
|
||||
export const START_DESTINATION_ROUTE = '/'
|
||||
|
||||
export interface StartDestination {
|
||||
page: StartPage
|
||||
tab: string
|
||||
}
|
||||
|
||||
// Mirrored in localStorage, not read from the settings store, because the
|
||||
// redirect has to be decided on the first paint — waiting for GET /api/settings
|
||||
// would put a spinner in front of every launch, including the default one that
|
||||
// isn't redirecting anywhere. Same trick as 'app_language' and the theme boot
|
||||
// snapshot: the server's answer wins on the next load, this is only what we
|
||||
// knew last time.
|
||||
const PAGE_KEY = 'trek_start_page'
|
||||
const TAB_KEY = 'trek_start_trip_tab'
|
||||
|
||||
const isStartPage = (value: unknown): value is StartPage =>
|
||||
value === 'dashboard' || value === 'active_trip'
|
||||
|
||||
export function readStartDestination(): StartDestination {
|
||||
try {
|
||||
const page = localStorage.getItem(PAGE_KEY)
|
||||
const tab = localStorage.getItem(TAB_KEY)
|
||||
return {
|
||||
page: isStartPage(page) ? page : DEFAULT_START_PAGE,
|
||||
tab: isTripTabId(tab) ? tab : DEFAULT_START_TRIP_TAB,
|
||||
}
|
||||
} catch {
|
||||
// Private-mode Safari and friends throw on access, not just on write.
|
||||
return { page: DEFAULT_START_PAGE, tab: DEFAULT_START_TRIP_TAB }
|
||||
}
|
||||
}
|
||||
|
||||
/** Called whenever the settings store learns a value — load, single or bulk save. */
|
||||
export function rememberStartDestination(settings: Partial<Settings>): void {
|
||||
try {
|
||||
if ('start_page' in settings) {
|
||||
const page = settings.start_page
|
||||
if (isStartPage(page)) localStorage.setItem(PAGE_KEY, page)
|
||||
else localStorage.removeItem(PAGE_KEY)
|
||||
}
|
||||
if ('start_trip_tab' in settings) {
|
||||
const tab = settings.start_trip_tab
|
||||
if (isTripTabId(tab)) localStorage.setItem(TAB_KEY, tab)
|
||||
else localStorage.removeItem(TAB_KEY)
|
||||
}
|
||||
} catch {
|
||||
// A browser that won't persist this just falls back to the dashboard.
|
||||
}
|
||||
}
|
||||
|
||||
/** On logout, so the next account on this browser starts on its own preference. */
|
||||
export function forgetStartDestination(): void {
|
||||
try {
|
||||
localStorage.removeItem(PAGE_KEY)
|
||||
localStorage.removeItem(TAB_KEY)
|
||||
} catch {
|
||||
// Nothing to clean up if storage is unavailable.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The planner URL for a startup redirect. The tab always goes on the query
|
||||
* string, including 'plan' — leaving it off would hand the decision back to
|
||||
* whichever tab the last session happened to end on.
|
||||
*/
|
||||
export function tripStartPath(tripId: number, tab: string): string {
|
||||
const safeTab = isTripTabId(tab) ? tab : DEFAULT_START_TRIP_TAB
|
||||
return `/trips/${tripId}?tab=${safeTab}`
|
||||
}
|
||||
Reference in New Issue
Block a user