From 28422c5de967099dedb9131470aa0477872f91d5 Mon Sep 17 00:00:00 2001 From: Rashid Shamloo Date: Sat, 9 Sep 2023 02:07:30 +0330 Subject: [PATCH] fix: added code to update store.settings.lastTrackedStoryId in api.selectStory() to enable keeping the current story active when navigating between sidebar and canvas on mobile --- code/lib/manager-api/src/modules/stories.ts | 4 ++++ code/lib/manager-api/src/tests/stories.test.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/code/lib/manager-api/src/modules/stories.ts b/code/lib/manager-api/src/modules/stories.ts index aff85aabcea..8283d35da6e 100644 --- a/code/lib/manager-api/src/modules/stories.ts +++ b/code/lib/manager-api/src/modules/stories.ts @@ -412,6 +412,10 @@ export const init: ModuleFn = ({ if (!entry) throw new Error(`Unknown id or title: '${titleOrId}'`); + store.setState({ + settings: { ...store.getState().settings, lastTrackedStoryId: entry.id }, + }); + // We want to navigate to the first ancestor entry that is a leaf const leafEntry = api.findLeafEntry(hash, entry.id); const fullId = leafEntry.refId ? `${leafEntry.refId}_${leafEntry.id}` : leafEntry.id; diff --git a/code/lib/manager-api/src/tests/stories.test.ts b/code/lib/manager-api/src/tests/stories.test.ts index a93cd1df9a9..eac7861b53b 100644 --- a/code/lib/manager-api/src/tests/stories.test.ts +++ b/code/lib/manager-api/src/tests/stories.test.ts @@ -895,6 +895,16 @@ describe('stories API', () => { api.selectStory('intro'); expect(navigate).toHaveBeenCalledWith('/docs/intro--docs'); }); + it('updates lastTrackedStoryId', () => { + const initialState = { path: '/story/a--1', storyId: 'a--1', viewMode: 'story' }; + const moduleArgs = createMockModuleArgs({ initialState }); + const { api } = initStories(moduleArgs as unknown as ModuleArgs); + const { store } = moduleArgs; + + api.setIndex({ v: 4, entries: navigationEntries }); + api.selectStory('a--1'); + expect(store.getState().settings.lastTrackedStoryId).toBe('a--1'); + }); describe('deprecated api', () => { it('allows navigating to a combination of title + name', () => { const initialState = { path: '/story/a--1', storyId: 'a--1', viewMode: 'story' };