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

Controls: Added server channel to create a new story #26769

Merged
merged 12 commits into from
Apr 15, 2024

Conversation

valentinpalkovic
Copy link
Contributor

@valentinpalkovic valentinpalkovic commented Apr 8, 2024

Closes #26658

What I did

This PR registers a server channel backend side to listen to 'create-new-storyfile' events for creating new story files. The payload for the creation looks like this:

interface Payload {
  // The filepath of the component for which the Story should be generated for (relative to the project root)
  filepath: string;
  // The name of the exported component
  componentExportName: string;
  // is default export
  default: boolean;
}

As soon as the new story file is created the result event 'create-new-storyfile-result' is emitted with the following payload:

interface Result {
  success: true | false;
  result: null | {
    storyId: string;
  };
  error: null | string;
}

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

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

  1. Run a sandbox for template, e.g. yarn task --task sandbox --start-from auto --template react-vite/default-ts
  2. Change src/stories/Button.stories.ts and adjust the meta section like this:
import type { Meta, StoryObj } from '@storybook/react';
import { Button } from './Button';
import { addons } from '@storybook/preview-api';

const channel = addons.getChannel();

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
  title: 'Example/Button',
  component: Button,
  parameters: {
    // Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
    layout: 'centered',
  },
  // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
  tags: ['autodocs'],
  // More on argTypes: https://storybook.js.org/docs/api/argtypes
  argTypes: {
    backgroundColor: { control: 'color' },
  },
  // Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
  args: { onClick: () => {
    channel.emit('create-new-storyfile', {
      filepath: 'sandbox/ButtonExample.ts',
      // The name of the exported component
      componentExportName: 'ButtonExample',
      // is default export
      default: false
    })
  } },
} satisfies Meta<typeof Button>;
  1. Click the button and see whether it triggers an event, the result event is sent to the browser and whether sandbox/ButtonExample gets created.

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>

@valentinpalkovic valentinpalkovic changed the title Valentin/create story file Controls: Added server channel to create a new story Apr 8, 2024
Copy link

nx-cloud bot commented Apr 8, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 154a302. 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 1 target

Sent with 💌 from NxCloud.

@valentinpalkovic valentinpalkovic changed the base branch from next to valentin/add-file-search-api April 9, 2024 08:22
@valentinpalkovic valentinpalkovic force-pushed the valentin/create-story-file branch 4 times, most recently from ba0a512 to 83f2b9f Compare April 9, 2024 11:50
@valentinpalkovic valentinpalkovic marked this pull request as ready for review April 9, 2024 13:48
Base automatically changed from valentin/add-file-search-api to next April 9, 2024 17:36
code/lib/core-server/src/utils/get-story-id.ts Outdated Show resolved Hide resolved
code/addons/controls/package.json Outdated Show resolved Hide resolved
code/lib/core-events/src/index.ts Show resolved Hide resolved

type Story = StoryObj<typeof meta>;

export const Default: Story = {}"
Copy link
Member

Choose a reason for hiding this comment

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

Missing a newline at end-of-file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This also falls into the (opinionated) formatting preferences of the user. As elaborated earlier, we don't have a proper solution in place to format the file by using the user's formatter and formatting rules.

code/lib/core-server/src/utils/get-story-id.ts Outdated Show resolved Hide resolved
Copy link
Member

@ghengeveld ghengeveld left a comment

Choose a reason for hiding this comment

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

I still don't like the generic Component name for default imports, but otherwise this looks good.


expect(exportedStoryName).toBe('Default');
expect(storyFileContent).toMatchInlineSnapshot(`
"import Component from './Page';
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I thought componentExportName would be the file's base name if there is no named export. That's what the test above suggested. Either way, can we use the file's base name (converted to PascalCase) rather than Component?

@valentinpalkovic valentinpalkovic merged commit ea87f40 into next Apr 15, 2024
35 checks passed
@valentinpalkovic valentinpalkovic deleted the valentin/create-story-file branch April 15, 2024 09:18
@github-actions github-actions bot mentioned this pull request Apr 15, 2024
44 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Project SfC]: Create a server channel API to create new story files triggered by the manager UI
3 participants