diff --git a/packages/snap-controller/src/Search/SearchController.ts b/packages/snap-controller/src/Search/SearchController.ts index d62b0cdec..e027a63d7 100644 --- a/packages/snap-controller/src/Search/SearchController.ts +++ b/packages/snap-controller/src/Search/SearchController.ts @@ -134,6 +134,17 @@ export class SearchController extends AbstractController { // restore position if (this.config.settings?.restorePosition?.enabled) { this.eventManager.on('restorePosition', async ({ controller, element }: RestorePositionObj, next: Next) => { + // attempt to grab the element from storage if it is not provided + if (!element?.selector) { + const lastRequest = this.storage.get('lastStringyParams'); + if (lastRequest) { + const storableRequestParams = getStorableRequestParams(JSON.parse(lastRequest)); + const stringyParams = JSON.stringify(storableRequestParams); + const scrollMap: { [key: string]: ElementPositionObj } = this.storage.get('scrollMap') || {}; + element = scrollMap[stringyParams]; + } + } + const scrollToPosition = () => { return new Promise(async (resolve) => { const maxCheckTime = 500; @@ -192,6 +203,13 @@ export class SearchController extends AbstractController { if (element) await scrollToPosition(); await next(); }); + + // fire restorePosition event on 'pageshow' when setting is enabled + if (this.config.settings?.restorePosition?.onPageShow) { + window.addEventListener('pageshow', () => { + this.eventManager.fire('restorePosition', { controller: this, element: {} }); + }); + } } // attach config plugins and event middleware diff --git a/packages/snap-store-mobx/src/types.ts b/packages/snap-store-mobx/src/types.ts index e3c4c74a1..4ed54f770 100644 --- a/packages/snap-store-mobx/src/types.ts +++ b/packages/snap-store-mobx/src/types.ts @@ -24,6 +24,7 @@ export type SearchStoreConfig = StoreConfig & { }; restorePosition?: { enabled: boolean; + onPageShow?: boolean; }; history?: { url?: string;