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

API: Add API access to sidebar renderLabel #27099

Merged
merged 3 commits into from May 13, 2024
Merged

Conversation

shilman
Copy link
Member

@shilman shilman commented May 12, 2024

Closes N/A

What I did

Added a way to access parent/child node story metadata from the renderLabel function.

Here's an example of how the new API can be used to propagate story tags up to their wrapping components (the use case I had in mind for this feature):

// manager.ts
import { addons } from '@storybook/manager-api';
import type { API, LeafEntry, HashEntry } from '@storybook/manager-api';

const SYSTEM_TAGS = ['dev', 'autodocs', 'test'];

const findComponentTags = (stories: LeafEntry[]) => {
  const allTags = stories.flatMap((story) => story.tags);
  const tagToCount = allTags.reduce(
    (acc, tag) => {
      acc[tag] = (acc[tag] || 0) + 1;
      return acc;
    },
    {} as Record<string, number>
  );
  return Object.entries(tagToCount)
    .filter(([tag, count]) => count === stories.length && !SYSTEM_TAGS.includes(tag))
    .map(([tag]) => tag);
};

addons.setConfig({
  sidebar: {
    renderLabel: (item: HashEntry, api: API) => {
      if (item.type !== 'component') return item.name;
      const tags = findComponentTags(item.children.map((childId) => api.getData(childId)));
      const tagsLabel = tags.length > 0 ? ` [${tags.join(', ')}]` : '';
      return `${item.name}${tagsLabel}`;
    },
  },
});

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

Documentation

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

🦋 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>

Copy link

nx-cloud bot commented May 12, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 5a85ede. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@shilman shilman requested a review from tmeasday May 12, 2024 14:00
@shilman shilman force-pushed the shilman/extend-render-label branch from 89b75d6 to dbad7c5 Compare May 12, 2024 14:13
Copy link
Member

@tmeasday tmeasday left a comment

Choose a reason for hiding this comment

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

LGTM

@shilman shilman marked this pull request as ready for review May 13, 2024 07:33
@shilman shilman self-assigned this May 13, 2024
@shilman shilman changed the title API: Extend sidebar renderLabel with API access API: Extend sidebar renderLabel with API argument May 13, 2024
@shilman shilman changed the title API: Extend sidebar renderLabel with API argument API: Add API access to sidebar renderLabel May 13, 2024
@shilman shilman merged commit 08b89fb into next May 13, 2024
67 of 68 checks passed
@shilman shilman deleted the shilman/extend-render-label branch May 13, 2024 08:46
@github-actions github-actions bot mentioned this pull request May 13, 2024
8 tasks
@shilman shilman added maintenance User-facing maintenance tasks and removed feature request labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: addons ci:normal maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants