On init, we check the hash fragment for these parameters and load the URL. We always show a loading state in that case rather than the landing screen. When determining the title, an explicitly-specified title takes precedence, otherwise we use the filename. I also added an error state, currently only used for my new code, but possibly there could be a more robust or widespread error handling approach in the future.
29 lines
733 B
TypeScript
29 lines
733 B
TypeScript
export interface HashParams {
|
|
profileURL?: string
|
|
title?: string
|
|
}
|
|
|
|
export function getHashParams(): HashParams {
|
|
try {
|
|
const hashContents = window.location.hash
|
|
if (!hashContents.startsWith('#')) {
|
|
return {}
|
|
}
|
|
const components = hashContents.substr(1).split('&')
|
|
const result: HashParams = {}
|
|
for (const component of components) {
|
|
const [key, value] = component.split('=')
|
|
if (key === 'profileURL') {
|
|
result.profileURL = decodeURIComponent(value)
|
|
} else if (key === 'title') {
|
|
result.title = decodeURIComponent(value)
|
|
}
|
|
}
|
|
return result
|
|
} catch (e) {
|
|
console.error(`Error when loading hash fragment.`)
|
|
console.error(e)
|
|
return {}
|
|
}
|
|
}
|