okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1/** 2* After a crash of the page's renderer (#5079). The desktop shell loads the page again at `/?recovery=<n>`; the page then starts with the 3* sidebar previews closed, once, so the file that may have caused the crash is not opened again by itself. The chats are the host's and 4* are all still there; only what the sidebar showed is deferred, and only until the person opens a preview themselves. 5*/ 6 7/** 1..9 from a `recovery=<n>` query, 0 when there is none or it is not a number. */ 8export function parseRecovery ( search :string ) :number { 9const value = new URLSearchParams ( search ). get ( 'recovery' ); 10if ( value === null || ! /^[1-9]$ / . test ( value )) 11return 0 ; 12return Number ( value ); 13} 14 15/** The query without the recovery parameter (and with its `?` only when something is left). */ 16export function withoutRecovery ( search :string ) :string { 17const params = new URLSearchParams ( search ); 18params . delete ( 'recovery' ); 19const rest = params . toString (); 20return rest ?`? ${ rest } ` :'' ; 21} 22 23let deferred = false ; 24let deferredSession :string | null = null ; 25 26/** 27* Read the shell's recovery mark from the address, remove it (a later reload of the page is an ordinary start), and defer the previews. 28* Called once, before the first render. Returns the recovery number, 0 for an ordinary start. 29*/ 30export function startRecovery () :number { 31let level = 0 ; 32try { 33level = parseRecovery ( window . location . search ); 34if ( level ) { 35const { pathname, search, hash} = window . location ; 36window . history . replaceState ( window . history . state , '' , pathname + withoutRecovery ( search ) + hash ); 37} 38} 39catch { /* no address to read: an ordinary start */ } 40deferred = level > 0 ; 41deferredSession = null ; 42return level ; 43} 44 45/** 46* True while the sidebar previews stay closed after a recovery: for the chat that was open when the renderer died, until the person 47* opens a preview themselves or goes to another chat. The first chat asked about is that chat. 48*/ 49export function previewsDeferred ( sessionId ?:string ) :boolean { 50if ( ! deferred ) 51return false ; 52if ( sessionId === undefined ) 53return true ; 54deferredSession ??= sessionId ; 55if ( deferredSession !== sessionId ) { 56deferred = false ; 57return false ; 58} 59return true ; 60} 61 62/** The person opened a preview themselves: from now on previews open as usual. */ 63export function endPreviewDeferral () :void { 64deferred = false ; 65}