Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions code/core/src/manager-api/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import {
} from '../lib/stories.ts';
import type { ModuleFn } from '../lib/types.tsx';
import { buildNavigationUrl } from '../lib/url.ts';
import { hasActiveFilters } from '../../shared/utils/story-index-filters.ts';
import type { ComposedRef } from '../root.tsx';
import { fullStatusStore } from '../stores/status.ts';
import { BUILT_IN_FILTERS } from '../../shared/constants/tags.ts';
Expand Down Expand Up @@ -599,14 +598,9 @@ export const init: ModuleFn<SubAPI, SubState> = ({
},
selectFirstStory: () => {
const state = store.getState();
const hasAnyActiveFilters = hasActiveFilters(state);

if (hasAnyActiveFilters) {
const { filteredIndex } = state;
if (!filteredIndex) {
return;
}
const { filteredIndex } = state;

if (filteredIndex) {
const firstStory = Object.keys(filteredIndex).find(
(id) => filteredIndex[id].type === 'story'
);
Expand Down Expand Up @@ -1075,11 +1069,10 @@ export const init: ModuleFn<SubAPI, SubState> = ({
* - If the user started storybook with a specific page-URL like "/settings/about"
*/
if (isCanvasRoute) {
const hasAnyActiveFilters = hasActiveFilters(state);
const { filteredIndex } = state;

if (hasAnyActiveFilters && !stateHasSelection) {
const { filteredIndex } = state;
const storyPassesFilter = filteredIndex && filteredIndex[storyId]?.type === 'story';
if (filteredIndex && !stateHasSelection) {
const storyPassesFilter = filteredIndex[storyId]?.type === 'story';

if (!storyPassesFilter) {
const firstFiltered = filteredIndex
Expand Down
32 changes: 32 additions & 0 deletions code/core/src/manager-api/tests/stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,38 @@ describe('stories API', () => {
expect(navigate).toHaveBeenCalledWith('/story/a--2', undefined);
});

/**
* Whilst the two of the built-in filters (status and tag) have easy ways to determine
* whether or not they are active, no other filters do - in particular, user-provided filters
* from experimental_setFilter.
*
* As such, the filtered index is now used if it is present, regardless of the heuristics that
* could be used to determine if the status/tag filters are active.
*/
it('uses filteredIndex when status filters are not active', () => {
const moduleArgs = createMockModuleArgs({
initialState: {
path: '/',
index: {
'a--1': { type: 'story', id: 'a--1', depth: 0 } as any,
'a--2': { type: 'story', id: 'a--2', depth: 0 } as any,
},
filteredIndex: {
'a--2': { type: 'story', id: 'a--2', depth: 0 } as any,
},
includedStatusFilters: [],
excludedStatusFilters: [],
includedTagFilters: [],
excludedTagFilters: [],
} as any,
});
const { api } = initStories(moduleArgs as unknown as ModuleArgs);
const { navigate } = moduleArgs;

api.selectFirstStory();
expect(navigate).toHaveBeenCalledWith('/story/a--2', undefined);
});

it('suppresses navigation when status filters active but filteredIndex is empty', () => {
const moduleArgs = createMockModuleArgs({
initialState: {
Expand Down
3 changes: 0 additions & 3 deletions code/core/src/shared/utils/story-index-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export const getActiveFilterCount = ({
(includedStatusFilters?.length ?? 0) +
(excludedStatusFilters?.length ?? 0);

export const hasActiveFilters = (filters: ActiveFiltersInput): boolean =>
getActiveFilterCount(filters) > 0;

/** Was this docs entry generated by a `.mdx` file (as opposed to an autodocs page)? */
export function isMdxEntry({ tags }: { tags?: Tag[] }) {
return tags?.includes(Tag.UNATTACHED_MDX) || tags?.includes(Tag.ATTACHED_MDX);
Expand Down
Loading