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

prepareMeta function similar to prepareStory #20592

Merged
merged 6 commits into from
Jan 13, 2023
Merged

prepareMeta function similar to prepareStory #20592

merged 6 commits into from
Jan 13, 2023

Conversation

JReinhold
Copy link
Contributor

@JReinhold JReinhold commented Jan 12, 2023

This PR implements a prepareMeta function, that similar to prepareStory, combines project annotations with component annotations, but without story annotations.

This is exposed in the useOf hook in blocks, that is then used by the Description block to render descriptions.

This is just a refactor, no stories should change because of this.

@JReinhold JReinhold self-assigned this Jan 12, 2023
@JReinhold JReinhold added maintenance User-facing maintenance tasks block: description labels Jan 12, 2023
Comment on lines 26 to 40
switch (resolved.type) {
case 'component': {
return { ...resolved, projectAnnotations: context.projectAnnotations };
}
case 'meta': {
return {
...resolved,
preparedMeta: prepareMeta(resolved.csfFile.meta, context.projectAnnotations),
};
}
case 'story':
default: {
return resolved;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

one could argue that this functionality should be directly in DocsContext.resolveModuleExport and not in useOf.

I'm indifferent.

Copy link
Member

Choose a reason for hiding this comment

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

We can always change it later if we need it higher up.

@JReinhold JReinhold marked this pull request as ready for review January 12, 2023 14:59
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.

I think we should split this a little more so it looks like:

function prepareStory(..) {
  const partial = prepareAnnotations(...);

  const unboundStoryFn = () => {....}
  // ...

  return {
     ...partial,
     id: ..,
     name: ...,
     unboundStoryFn,
     // ...
  };
}

That way we aren't doing all the story function preparation for no reason with a mocked function.

...preparedStory,
// properties that are actually different between prepareMeta and prepareStory
moduleExport: undefined,
id: 'id--__meta',
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't set an id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about that. componentAnnotations have ids, and all of the tests for prepareStory pass in an id as the componentAnnotations id.

I agree that we shouldn't set a fake one, we should just use the one from componentAnnotations, I've changed that.

Comment on lines 26 to 40
switch (resolved.type) {
case 'component': {
return { ...resolved, projectAnnotations: context.projectAnnotations };
}
case 'meta': {
return {
...resolved,
preparedMeta: prepareMeta(resolved.csfFile.meta, context.projectAnnotations),
};
}
case 'story':
default: {
return resolved;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We can always change it later if we need it higher up.

code/lib/preview-api/src/modules/store/csf/prepareStory.ts Outdated Show resolved Hide resolved
@JReinhold
Copy link
Contributor Author

@tmeasday I refactored it so the story-specific stuff only happens in prepareStory as well as created actual types for PreparedMeta.
I couldn't completely get rid of "fake" story identifiers (id, name, story) because we need to pass them to enhancers to build argTypes, etc.

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.

Excellent, thanks @JReinhold

@JReinhold JReinhold merged commit 858aa9d into next Jan 13, 2023
@JReinhold JReinhold deleted the prepareMeta branch January 13, 2023 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
block: description maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants