- New src/lib/router.js: parseHash/ viewToHash for the 5 tab views, with a default fallback for empty/unknown hashes. - App.svelte: initialize viewMode from location.hash, setView() updates both state and the URL hash, and a guarded hashchange listener syncs back/forward and manual URL edits. All tab onclicks route through setView(). - Tests for parse/viewToHash round-trips and fallback. 76 total pass.
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
/**
|
|
* Lightweight hash router for the app's tab views.
|
|
*
|
|
* Tabs are routable via the URL hash so a user can share/copy a URL for a
|
|
* specific tab (e.g. #/radar) and use browser back/forward between tabs.
|
|
* There is no nested route structure — just a single top-level view segment.
|
|
*/
|
|
|
|
export const VALID_VIEWS = ['current', 'hourly', 'daily', 'detail', 'radar']
|
|
export const DEFAULT_VIEW = 'current'
|
|
|
|
/**
|
|
* Parse a location hash into a view id, falling back to the default view.
|
|
* Accepts '#/hourly', 'hourly', '#hourly', '', '#/unknown', etc.
|
|
*
|
|
* @param {string} [hash] - window.location.hash
|
|
* @returns {string} one of VALID_VIEWS
|
|
*/
|
|
export function parseHash(hash = '') {
|
|
const clean = String(hash).replace(/^#\/?/, '')
|
|
const seg = clean.split('/')[0]
|
|
return VALID_VIEWS.includes(seg) ? seg : DEFAULT_VIEW
|
|
}
|
|
|
|
/**
|
|
* Convert a view id into its URL hash representation.
|
|
* @param {string} view - one of VALID_VIEWS
|
|
* @returns {string} e.g. '#/radar'
|
|
*/
|
|
export function viewToHash(view) {
|
|
return `#/${view}`
|
|
}
|