Newer
Older
navi-1 / webclient / src / utils / hash.js
// Settings live at `#settings`, and a single section can be linked directly:
// `#settings/app`. Every reader of the hash goes through here, so the three
// places in App.vue that route on it cannot drift apart — an unparsed
// `#settings/app` would be taken for a session id and loaded as one.
const SETTINGS_HASH = /^#settings(?:\/([A-Za-z0-9_-]+))?$/

export function isSettingsHash(hash) {
  return SETTINGS_HASH.test(hash || '')
}

/** The tab id in the hash, or null when the hash names no section. */
export function settingsTabFromHash(hash) {
  return SETTINGS_HASH.exec(hash || '')?.[1] ?? null
}

export function settingsHash(tab) {
  return tab ? `#settings/${tab}` : '#settings'
}

// A shared chat is a capability link: `#share=<token>`. The hash is the right
// carrier — it never reaches the server in a request line or referrer, so the
// token is not logged by the web server on every page load.
const SHARE_HASH = /^#share=([A-Za-z0-9_-]{16,})$/

/** The token in the hash, or null when this is not a share link. */
export function shareTokenFromHash(hash) {
  return SHARE_HASH.exec(hash || '')?.[1] ?? null
}

export function shareHash(token) {
  return `#share=${token}`
}