Skip to content

Commit

Permalink
TagsFilter: Don't use filtered index to generate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jun 17, 2024
1 parent 22a699b commit 09c56df
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
7 changes: 6 additions & 1 deletion code/ui/manager/src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Addon_SidebarBottomType,
Addon_SidebarTopType,
API_LoadedRefData,
StoryIndex,
} from '@storybook/types';
import type { HeadingProps } from './Heading';
import { Heading } from './Heading';
Expand Down Expand Up @@ -126,12 +127,14 @@ export interface SidebarProps extends API_LoadedRefData {
enableShortcuts?: boolean;
onMenuClick?: HeadingProps['onMenuClick'];
showCreateStoryButton?: boolean;
indexJson?: StoryIndex;
}

export const Sidebar = React.memo(function Sidebar({
storyId = null,
refId = DEFAULT_REF_ID,
index,
indexJson,
indexError,
status,
previewInitialized,
Expand Down Expand Up @@ -163,7 +166,9 @@ export const Sidebar = React.memo(function Sidebar({
isLoading={isLoading}
onMenuClick={onMenuClick}
/>
{index && <TagsFilter api={api} index={index} updateQueryParams={updateQueryParams} />}
{index && (
<TagsFilter api={api} indexJson={indexJson} updateQueryParams={updateQueryParams} />
)}
<Search
dataset={dataset}
enableShortcuts={enableShortcuts}
Expand Down
7 changes: 5 additions & 2 deletions code/ui/manager/src/components/sidebar/TagsFilter.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export const Closed: Story = {
api: {
experimental_setFilter: fn(),
} as any,
index: {
story1: { type: 'story', tags: ['A', 'B', 'C', 'dev'] } as any,
indexJson: {
v: 6,
entries: {
'c1-s1': { tags: ['A', 'B', 'C', 'dev'] } as any,
},
},
updateQueryParams: fn(),
},
Expand Down
20 changes: 9 additions & 11 deletions code/ui/manager/src/components/sidebar/TagsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Badge, IconButton, WithTooltip } from '@storybook/components';
import { FilterIcon } from '@storybook/icons';
import type { API } from '@storybook/manager-api';
import { styled } from '@storybook/theming';
import type { Tag, API_IndexHash } from '@storybook/types';
import type { Tag, API_IndexHash, StoryIndex } from '@storybook/types';
import { TagsFilterPanel } from './TagsFilterPanel';

const TAGS_FILTER = 'tags-filter';
Expand Down Expand Up @@ -31,14 +31,14 @@ const Count = styled(Badge)(({ theme }) => ({

export interface TagsFilterProps {
api: API;
index: API_IndexHash;
indexJson: StoryIndex;
updateQueryParams: (params: Record<string, string | null>) => void;
initialSelectedTags?: Tag[];
}

export const TagsFilter = ({
api,
index,
indexJson,
updateQueryParams,
initialSelectedTags = [],
}: TagsFilterProps) => {
Expand All @@ -49,21 +49,19 @@ export const TagsFilter = ({

useEffect(() => {
api.experimental_setFilter(TAGS_FILTER, (item) => {
const tags = item.tags ?? [];
return exclude
? !selectedTags.some((tag) => tags.includes(tag))
: selectedTags.every((tag) => tags.includes(tag));
if (selectedTags.length === 0) return true;

const hasSelectedTags = selectedTags.some((tag) => item.tags?.includes(tag));
return exclude ? !hasSelectedTags : hasSelectedTags;
});

const tagsParam = selectedTags.join(',');
const [includeTags, excludeTags] = exclude ? [null, tagsParam] : [tagsParam, null];
updateQueryParams({ includeTags, excludeTags });
}, [api, selectedTags, exclude, updateQueryParams]);

const allTags = Object.values(index).reduce((acc, entry) => {
if (entry.type === 'story') {
entry.tags.forEach((tag: Tag) => acc.add(tag));
}
const allTags = Object.values(indexJson.entries).reduce((acc, entry) => {
entry.tags?.forEach((tag: Tag) => acc.add(tag));
return acc;
}, new Set<Tag>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const meta = {
toggleTag: fn(),
toggleExclude: fn(),
},
tags: ['hoho'],
} satisfies Meta<typeof TagsFilterPanel>;

export default meta;
Expand Down
5 changes: 5 additions & 0 deletions code/ui/manager/src/container/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const Sidebar = React.memo(function Sideber({ onMenuClick }: SidebarProps) {
storyId,
refId,
layout: { showToolbar },
// FIXME: This is the actual `index.json` index where the `index` below
// is actually the stories hash. We should fix this up and make it consistent.
// eslint-disable-next-line @typescript-eslint/naming-convention
internal_index,
index,
status,
indexError,
Expand Down Expand Up @@ -52,6 +56,7 @@ const Sidebar = React.memo(function Sideber({ onMenuClick }: SidebarProps) {
return {
title: name,
url,
indexJson: internal_index,
index,
indexError,
status,
Expand Down

0 comments on commit 09c56df

Please sign in to comment.