diff --git a/src/stories/controller.js b/src/stories/controller.js index 604a2f632..a61a850ca 100644 --- a/src/stories/controller.js +++ b/src/stories/controller.js @@ -183,6 +183,22 @@ export function createStoriesController({ delete state.gevStories; history.replaceState(state, '', urlFor({})); } + // Without the story of an entry whose story is gone. + function dropMissingStory(params) { + if (!params.has(PARAM_STORIES)) { + clearStoriesUrl(); + return; + } + const url = new URL(location.href); + url.searchParams.delete(PARAM_STORY); + url.searchParams.delete(PARAM_STORY_TIME); + const state = { ...(history.state ?? {}) }; + if (state.gevStories) { + state.gevStories = { ...state.gevStories }; + delete state.gevStories.story; + } + history.replaceState(state, '', url); + } // Steps back over Stories' entries; `landed` runs once the page Stories // opened from is current (or at once when Stories pushed nothing). A // traversal that never lands (cross-document, blocked) gives up after 2 s. @@ -258,6 +274,10 @@ export function createStoriesController({ } const params = new URLSearchParams(location.search); const id = params.get(PARAM_STORY); + // An entry for a story that is gone (deleted since) opens nothing, in + // any mode; its ?story= must not stay in the address for the next share + // link. The entry's own Explore and the globe hash stay. + if (id && !storyById(id)) dropMissingStory(params); if (id && storyById(id)) controller.openStory(id, { fromHistory: true, @@ -269,9 +289,6 @@ export function createStoriesController({ : controller.backToExplore({ fromHistory: true, returningToExplore }); else if (store.mode !== 'closed') controller.close({ fromHistory: true }); else { - // An entry for a story that is gone (deleted since) opens nothing; - // its ?story= must not stay in the address for the next share link. - if (id) clearStoriesUrl(); if (pendingView) { // Another browser navigation landed while the scene was stopping. // The destination owns the globe now, so discard the earlier restore. diff --git a/src/stories/controller.test.mjs b/src/stories/controller.test.mjs index 75ccc1494..3f369f6e6 100644 --- a/src/stories/controller.test.mjs +++ b/src/stories/controller.test.mjs @@ -966,6 +966,48 @@ test('Back into the entry of a deleted story, with Stories closed, clears ?story controller.destroy(); }); +for (const [from, enter] of [ + ['Explore', (controller) => controller.open()], + ['an open story', (controller) => controller.openStory('gev:flights-radar')], +]) { + test(`Back into the entry of a deleted story, from ${from}, clears ?story=`, () => { + const { controller, store } = setup(); + enter(controller); + assert.notEqual(store.mode, 'closed'); + history.replaceState( + { gevStories: { story: 'gev:gone', depth: 2, explore: 1 } }, + '', + '/?story=gev%3Agone&storyTime=12#lat=1', + ); + dispatchEvent(new PopStateEvent('popstate')); + const url = new URL(location.href); + assert.equal(url.searchParams.has('story'), false); + assert.equal(url.searchParams.has('storyTime'), false); + assert.equal(url.hash, '#lat=1', 'the globe state stays'); + assert.equal(history.state?.gevStories?.story, undefined); + controller.destroy(); + }); +} + +test('Back into an Explore entry naming a deleted story keeps Explore', () => { + const { controller, store } = setup(); + controller.openStory('gev:flights-radar'); + history.replaceState( + { gevStories: { story: 'gev:gone', depth: 1, explore: 1 } }, + '', + '/?stories=explore&story=gev%3Agone#lat=1', + ); + dispatchEvent(new PopStateEvent('popstate')); + const url = new URL(location.href); + assert.equal(url.searchParams.get('stories'), 'explore'); + assert.equal(url.searchParams.has('story'), false); + assert.equal(url.hash, '#lat=1'); + assert.equal(history.state?.gevStories?.depth, 1); + assert.equal(history.state?.gevStories?.story, undefined); + assert.equal(store.mode, 'explore'); + controller.destroy(); +}); + test('unsaved scenes appear as local drafts and disappear when deleted', () => { setActivePinia(createPinia()); history.replaceState(null, '', '/');