11import { useRouter } from "@tanstack/react-router" ;
22import { useEffect } from "react" ;
3- import { type NativeTab , tabForPath } from "./bridge" ;
3+ import { type NativeTab , shellListens , tabForPath } from "./bridge" ;
4+ import { tellShell } from "./route-reports" ;
45import { nativeShell } from "./shell" ;
56import {
67 canGoBack ,
@@ -44,19 +45,35 @@ export const BACK_REQUEST = "openbot:back";
4445
4546/** The tabs' stacks for this page load, once the shell's navigation has started. */
4647let stacks : TabStacks | null = null ;
47- /** Shows a step (set while the navigation is mounted). */
48- let apply : ( ( next : TabStep ) => void ) | null = null ;
48+ /** Shows a step, announced to the app as `direction` when given (set while mounted). */
49+ let apply : ( ( next : TabStep , direction ?: Direction ) => void ) | null = null ;
50+
51+ type Direction = "push" | "back" | "replace" ;
4952
5053/** For tests: forget the stacks, as a new page load does. */
5154export function forgetTabStacksForTests ( ) : void {
5255 stacks = null ;
5356 apply = null ;
5457 scrolls . clear ( ) ;
58+ restoring = null ;
5559}
5660
57- /** The tab the page is in and whether Back can go anywhere, inside the shell; null outside it. */
58- export function shellTabState ( ) : { tab : NativeTab ; canGoBack : boolean } | null {
59- return stacks ? { tab : stacks . current , canGoBack : canGoBack ( stacks ) } : null ;
61+ /**
62+ * The tab the page is in, whether Back can go anywhere, and how deep its stack is (1 at its root),
63+ * inside the shell; null outside it.
64+ */
65+ export function shellTabState ( ) : {
66+ tab : NativeTab ;
67+ canGoBack : boolean ;
68+ depth : number ;
69+ } | null {
70+ return stacks
71+ ? {
72+ tab : stacks . current ,
73+ canGoBack : canGoBack ( stacks ) ,
74+ depth : stacks . stacks [ stacks . current ] . length ,
75+ }
76+ : null ;
6077}
6178
6279/** The root of the tab the page is in, inside the shell: where a back control falls back to. */
@@ -70,7 +87,7 @@ export function currentTabRoot(): string | null {
7087 */
7188export function goBackInShell ( ) : boolean {
7289 if ( ! stacks || ! apply || ! canGoBack ( stacks ) ) return false ;
73- apply ( goBack ( stacks ) ) ;
90+ apply ( goBack ( stacks ) , "back" ) ;
7491 return true ;
7592}
7693
@@ -101,22 +118,88 @@ function saveScroll(location: string) {
101118 if ( list ) scrolls . set ( location , list . scrollTop ) ;
102119}
103120
121+ /**
122+ * A list being put back where it was, while it is: the `route` for its page waits for it
123+ * (`whenScrollRestored`), so the app drops its snapshot only once nothing will jump.
124+ */
125+ let restoring : { done : Promise < void > } | null = null ;
126+
104127/** Puts `location`'s list back where it was, once it has drawn enough to scroll that far. */
105128function restoreScroll ( location : string ) {
106129 const saved = scrolls . get ( location ) ;
107130 if ( ! saved ) return ;
108131 let frames = 0 ;
132+ let settle = ( ) => { } ;
133+ const done = new Promise < void > ( ( resolve ) => {
134+ settle = resolve ;
135+ } ) ;
136+ const current = { done } ;
137+ restoring = current ;
138+ const finish = ( ) => {
139+ if ( restoring === current ) restoring = null ;
140+ settle ( ) ;
141+ } ;
109142 const tick = ( ) => {
110143 const list = document . querySelector ( LIST ) ;
111144 if ( list ) {
112145 list . scrollTop = saved ;
113- if ( Math . abs ( list . scrollTop - saved ) < 2 ) return ;
146+ if ( Math . abs ( list . scrollTop - saved ) < 2 ) return finish ( ) ;
114147 }
148+ // About a second at most: a list that can no longer scroll that far is shown where it can.
115149 if ( ++ frames < 60 ) requestAnimationFrame ( tick ) ;
150+ else finish ( ) ;
116151 } ;
117152 requestAnimationFrame ( tick ) ;
118153}
119154
155+ /** Runs `then` once no list is being put back (at once, mostly). */
156+ export function whenScrollRestored ( then : ( ) => void ) : ( ) => void {
157+ let live = true ;
158+ const pending = restoring ;
159+ if ( ! pending ) then ( ) ;
160+ else void pending . done . then ( ( ) => live && then ( ) ) ;
161+ return ( ) => {
162+ live = false ;
163+ } ;
164+ }
165+
166+ /** The app's answer to `will-navigate`: it holds its snapshot, so the page may change. */
167+ export const NAVIGATE_OK = "openbot:navigate-ok" ;
168+ /** How long the page waits for that answer before it goes ahead anyway. */
169+ export const NAVIGATE_OK_MS = 120 ;
170+
171+ /**
172+ * Tells the app a navigation the page started is coming (`will-navigate`), then runs `go`: at once,
173+ * or on iOS, where the app takes a snapshot of the page for its swipe back, once the app answers
174+ * `openbot:navigate-ok` or after `NAVIGATE_OK_MS`, whichever is first. Never waits where nothing
175+ * received the message, nor on Android, which does not answer.
176+ */
177+ function announce ( direction : Direction , next : TabStacks , go : ( ) => void ) : void {
178+ const message = {
179+ type : "will-navigate" ,
180+ tab : next . current ,
181+ direction,
182+ depth : next . stacks [ next . current ] . length ,
183+ } as const ;
184+ if ( nativeShell ( ) ?. platform !== "ios" || ! shellListens ( ) ) {
185+ tellShell ( message ) ;
186+ go ( ) ;
187+ return ;
188+ }
189+ // Listening before it is said, so an answer given at once is not missed.
190+ let gone = false ;
191+ const once = ( ) => {
192+ if ( gone ) return ;
193+ gone = true ;
194+ clearTimeout ( timer ) ;
195+ window . removeEventListener ( NAVIGATE_OK , once ) ;
196+ go ( ) ;
197+ } ;
198+ const timer = setTimeout ( once , NAVIGATE_OK_MS ) ;
199+ window . addEventListener ( NAVIGATE_OK , once ) ;
200+ tellShell ( message ) ;
201+ }
202+
120203function scrollListToTop ( ) {
121204 document . querySelector ( LIST ) ?. scrollTo ( { top : 0 , behavior : "smooth" } ) ;
122205}
@@ -135,31 +218,47 @@ export function useTabNavigation(): void {
135218 const push = history . push ;
136219 const replace = history . replace ;
137220
221+ /** Whether going from `from` to `to` changes the page, as opposed to its search or fragment. */
222+ const changesPage = ( from : string , to : string ) =>
223+ pathOf ( from ) !== pathOf ( to ) ;
224+
138225 history . push = ( path , state , options ) => {
139- saveScroll ( here ( ) ) ;
226+ const from = here ( ) ;
227+ saveScroll ( from ) ;
140228 stacks = signedOut ( path )
141229 ? cleared ( )
142- : pushLocation ( stacks ?? stacksFor ( here ( ) ) , path ) . state ;
143- replace ( path , state , options ) ;
230+ : pushLocation ( stacks ?? stacksFor ( from ) , path ) . state ;
231+ if ( signedOut ( path ) || ! changesPage ( from , path ) )
232+ replace ( path , state , options ) ;
233+ else announce ( "push" , stacks , ( ) => replace ( path , state , options ) ) ;
144234 } ;
145235 history . replace = ( path , state , options ) => {
236+ const from = here ( ) ;
146237 stacks = signedOut ( path )
147238 ? cleared ( )
148- : replaceLocation ( stacks ?? stacksFor ( here ( ) ) , path ) . state ;
149- replace ( path , state , options ) ;
239+ : replaceLocation ( stacks ?? stacksFor ( from ) , path ) . state ;
240+ if ( signedOut ( path ) || ! changesPage ( from , path ) )
241+ replace ( path , state , options ) ;
242+ else announce ( "replace" , stacks , ( ) => replace ( path , state , options ) ) ;
150243 } ;
151- apply = ( next ) => {
244+ apply = ( next , direction ) => {
152245 const from = here ( ) ;
153246 if ( next . show !== from ) saveScroll ( from ) ;
154247 stacks = next . state ;
155- if ( next . show !== from ) {
248+ if ( next . scrollToTop ) scrollListToTop ( ) ;
249+ if ( next . show === from ) return ;
250+ const go = ( ) => {
156251 replace ( next . show ) ;
157252 restoreScroll ( next . show ) ;
158- }
159- if ( next . scrollToTop ) scrollListToTop ( ) ;
253+ } ;
254+ if ( direction && changesPage ( from , next . show ) )
255+ announce ( direction , next . state , go ) ;
256+ else go ( ) ;
160257 } ;
258+ // Back the app asked for: popped as the page's own Back is, but not announced, since the app
259+ // started it and already knows.
161260 const onBack = ( ) => {
162- goBackInShell ( ) ;
261+ if ( stacks && apply && canGoBack ( stacks ) ) apply ( goBack ( stacks ) ) ;
163262 } ;
164263 window . addEventListener ( BACK_REQUEST , onBack ) ;
165264 return ( ) => {
0 commit comments