-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Composition: Fix composed story search #29453
Merged
yannbf
merged 3 commits into
storybookjs:next
from
jsingh0026:fix/external-storybook-composition
Nov 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import React from 'react'; | |
import { IconButton } from '@storybook/core/components'; | ||
import { styled } from '@storybook/core/theming'; | ||
import { BottomBarToggleIcon, MenuIcon } from '@storybook/icons'; | ||
import type { API_IndexHash, API_Refs } from '@storybook/types'; | ||
|
||
import { useStorybookApi, useStorybookState } from '@storybook/core/manager-api'; | ||
|
||
|
@@ -17,27 +18,46 @@ interface MobileNavigationProps { | |
showPanel: boolean; | ||
} | ||
|
||
// Function to combine all indexes | ||
function combineIndexes(rootIndex: API_IndexHash | undefined, refs: API_Refs) { | ||
// Create a copy of the root index to avoid mutation | ||
const combinedIndex = { ...(rootIndex || {}) }; // Use an empty object as fallback | ||
|
||
// Traverse refs and merge each nested index with the root index | ||
Object.values(refs).forEach((ref) => { | ||
if (ref.index) { | ||
Object.assign(combinedIndex, ref.index); | ||
} | ||
}); | ||
|
||
return combinedIndex; | ||
} | ||
|
||
/** | ||
* Walks the tree from the current story to combine story+component+folder names into a single | ||
* string | ||
*/ | ||
const useFullStoryName = () => { | ||
const { index } = useStorybookState(); | ||
const { index, refs } = useStorybookState(); | ||
const api = useStorybookApi(); | ||
const currentStory = api.getCurrentStoryData(); | ||
|
||
if (!currentStory) { | ||
return ''; | ||
} | ||
|
||
const combinedIndex = combineIndexes(index, refs || {}); | ||
let fullStoryName = currentStory.renderLabel?.(currentStory, api) || currentStory.name; | ||
// @ts-expect-error (non strict) | ||
let node = index[currentStory.id]; | ||
|
||
// @ts-expect-error (non strict) | ||
while ('parent' in node && node.parent && index[node.parent] && fullStoryName.length < 24) { | ||
// @ts-expect-error (non strict) | ||
node = index[node.parent]; | ||
let node = combinedIndex[currentStory.id]; | ||
|
||
while ( | ||
node && | ||
'parent' in node && | ||
node.parent && | ||
combinedIndex[node.parent] && | ||
fullStoryName.length < 24 | ||
) { | ||
Comment on lines
+53
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: check node.parent type explicitly since it could be undefined from external sources |
||
node = combinedIndex[node.parent]; | ||
const parentName = node.renderLabel?.(node, api) || node.name; | ||
fullStoryName = `${parentName}/${fullStoryName}`; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: potential race condition if ref.index is modified during iteration - consider using Object.entries and destructuring for safer iteration