Files
SolidScribe/client/src/LaunchPage.js
T

82 lines
2.6 KiB
JavaScript

//
// Launch page preference.
//
// The app used to inject its manifest at runtime with
// `start_url: window.location.href`, so installing from /metrictrack made the
// installed app always open there. That mattered: not everyone wants the notes
// page as their home.
//
// A static manifest can't carry a per-install start_url, and we need a static
// same-origin manifest for share_target to register at all (a data: URI
// manifest has an opaque origin, so scope resolution fails). So the preference
// moves in-app: the manifest's start_url is "/", and we redirect from there.
//
// This is also strictly better than the old behaviour -- the old one froze the
// home page at install time and could only be changed by reinstalling. This one
// is editable in Settings.
//
const LAUNCH_KEY = 'launchPage'
const DEFAULT_LAUNCH = '/notes'
// Pages that make sense as a home. Anything else (a specific note, the share
// target, login) would be a bad place to land every time.
export const LAUNCH_OPTIONS = [
{ path: '/notes', label: 'Notes' },
{ path: '/overview', label: 'Overview' },
{ path: '/quick', label: 'Scratch Pad' },
{ path: '/metrictrack', label: 'Metric Tracking' },
{ path: '/attachments', label: 'Files & Links' },
]
export function getLaunchPage(){
const stored = localStorage.getItem(LAUNCH_KEY)
const valid = LAUNCH_OPTIONS.some(option => option.path === stored)
return valid ? stored : DEFAULT_LAUNCH
}
export function setLaunchPage(path){
if(!LAUNCH_OPTIONS.some(option => option.path === path)){ return }
localStorage.setItem(LAUNCH_KEY, path)
}
export function isStandalone(){
return window.matchMedia('(display-mode: standalone)').matches
|| window.navigator.standalone === true
}
//
// Capture the page the user installed from, matching the old behaviour: if they
// install while on /metrictrack, that becomes their launch page. Only set as a
// default -- an explicit choice in Settings is never overwritten.
//
export function watchForInstall(){
window.addEventListener('appinstalled', () => {
if(localStorage.getItem(LAUNCH_KEY)){ return }
const path = window.location.pathname
if(LAUNCH_OPTIONS.some(option => option.path === path)){
localStorage.setItem(LAUNCH_KEY, path)
}
})
}
//
// "/" is a shim for installed launches. Redirect only when running standalone,
// so the marketing home page still works normally in a browser tab.
//
export function installLaunchRedirect(router){
watchForInstall()
router.beforeEach((to, from, next) => {
// Only the very first navigation of a standalone launch
if(to.path === '/' && from.matched.length === 0 && isStandalone()){
return next(getLaunchPage())
}
return next()
})
}