Skip to content
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

fix: include external Storybook entries for full story name retrieval #29452

Conversation

jsingh0026
Copy link
Contributor

@jsingh0026 jsingh0026 commented Oct 25, 2024

Closes #29268

What I did

This PR resolves the issue of composition failures by including external Storybook entries in the index data. Previously, the current story ID was only searched in the internal Storybook index, which caused mismatches. By incorporating external Storybook IDs into the combined index, full story names are now correctly retrieved and visible on mobile devices without breaking.

Screen.Recording.2024-10-26.at.2.39.59.AM.mov

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. Run a sandbox locally using yarn start.
  2. Open Storybook in your browser.
  3. Access a story from the external Storybook.
  4. Resize the browser window to mobile view (e.g., 375px width) to verify that the external Storybook is composed correctly and that the full story names are displayed as expected.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

Greptile Summary

Enhanced mobile navigation to properly handle external Storybook composition by merging root and external indexes for accurate story name retrieval.

  • Added combineIndexes function in /code/core/src/manager/components/mobile/navigation/MobileNavigation.tsx to merge root and external ref indexes
  • Fixed type safety by removing @ts-expect-error comments and adding proper type checks
  • Modified useFullStoryName hook to handle undefined nodes gracefully
  • Added null checks for refs object to prevent runtime errors during composition
  • Improved error handling when accessing parent nodes in story hierarchy

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 22 to 34
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;
}
Copy link
Contributor

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 refs are updated while combining indexes - consider using useMemo to cache the combined index

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: add null check for combinedIndex[currentStory.id] to prevent runtime errors

Comment on lines 53 to 59
while (
node &&
'parent' in node &&
node.parent &&
combinedIndex[node.parent] &&
fullStoryName.length < 24
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider moving length check to beginning of loop to prevent unnecessary iterations

@jsingh0026 jsingh0026 closed this Oct 25, 2024
@jsingh0026 jsingh0026 force-pushed the fix/external-storybook-composition branch from 7c5f8f1 to 6098c01 Compare October 25, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Storybook Composition Fails When Composing a Valid External Storybook
1 participant