-
-
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
maintenance: fixing minor code smells #29706
base: next
Are you sure you want to change the base?
Conversation
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.
7 file(s) reviewed, 18 comment(s)
Edit PR Review Bot Settings | Greptile
// @ts-expect-error (Converted from ts-ignore) | ||
const addonsInstance = addons; | ||
// @ts-expect-error (Converted from ts-ignore) | ||
const channel = { | ||
on: () => {}, | ||
once: () => {}, | ||
off: () => {}, | ||
emit: () => {}, | ||
addListener: () => {}, | ||
removeListener: () => {}, | ||
}; |
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: Consider using a proper mock object or testing utility instead of ts-expect-error and empty function stubs
handleAPI(api) { | ||
addons.loadAddons(api); | ||
} | ||
const handleAPI = (api: any) => addonsInstance.loadAddons(api); |
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: api parameter should be properly typed instead of any
); | ||
} | ||
} | ||
export const PrettyFakeProvider = (props: any) => { |
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: props should be properly typed instead of any
const refresh = useCallback(() => { | ||
globalWindow.document.location.reload(); | ||
setAuthAttempted(false); | ||
}, []); |
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.
logic: Setting isAuthAttempted to false won't trigger a page refresh. Need to actually reload the page to fetch new data.
@@ -278,6 +278,8 @@ const SourceCodeMessage: FC<{ | |||
|
|||
const LoginRequiredMessage: FC<RefType> = ({ loginUrl, id }) => { | |||
const theme = useTheme(); | |||
const [isLoggedIn, setIsLoggedIn] = useState(false); |
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.
syntax: useState import is missing from React imports at the top of the file
const OcticonHeader = OcticonHeaders[as]; | ||
const hash = `#${id}`; | ||
const elementRef = useRef<HTMLAnchorElement | null>(null); |
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.
logic: elementRef is declared but never used
@@ -177,10 +165,16 @@ const HeaderWithOcticonAnchor: FC<PropsWithChildren<HeaderWithOcticonAnchorProps | |||
...rest | |||
}) => { | |||
const context = useContext(DocsContext); | |||
|
|||
// @ts-expect-error (Converted from ts-ignore) | |||
const OcticonHeader = OcticonHeaders[as]; |
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.
logic: potential runtime error if 'as' is not a valid header type
// We replace the lifecycle method with a try-catch block | ||
const errorHandler = (error: Error) => { | ||
componentDidCatch(error); | ||
}; |
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.
logic: errorHandler function is defined but never used, and doesn't properly replicate error boundary functionality
const componentDidCatch = (err: Error) => { | ||
showException(err); | ||
} | ||
setHasError(true); | ||
}; |
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.
logic: componentDidCatch is defined but not hooked into React's error boundary system - functional components cannot be error boundaries
if (forceRemount) { | ||
unmountElement(canvasElement); | ||
} |
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: Consider adding back the important comment explaining why forceRemount is necessary for React instance recreation
View your CI Pipeline Execution ↗ for commit 43a045c.
☁️ Nx Cloud last updated this comment at |
What I did
This PR addresses and resolves 8 code smells, categorized and sourced as follows:
JSX outside the render method
storybook/code/core/src/manager/FakeProvider.tsx
storybook/code/core/src/manager/settings/shortcuts.tsx
Direct DOM manipulations
storybook/code/lib/blocks/src/blocks/DocsContainer.tsx
storybook/code/lib/blocks/src/blocks/mdx.tsx
Inheritance instead of Composition
storybook/code/core/src/manager/FakeProvider.tsx
storybook/code/renderers/react/src/renderToCanvas.tsx
Force Update
forceUpdate
was used, replacing them with more appropriate state management solutions to ensure proper re-rendering.storybook/code/core/src/manager/components/sidebar/RefBlocks.tsx
storybook/code/core/src/manager/components/sidebar/RefIndicator.tsx
These changes improve the overall code quality, readability, and maintainability.
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/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
This pull request focuses on code quality improvements across multiple Storybook components, addressing React anti-patterns and modernizing component implementations.
code/core/src/manager/FakeProvider.tsx
andcode/renderers/react/src/renderToCanvas.tsx
, favoring composition over inheritancecode/lib/blocks/src/blocks/DocsContainer.tsx
andcode/lib/blocks/src/blocks/mdx.tsx
code/core/src/manager/components/sidebar/RefBlocks.tsx
andRefIndicator.tsx
code/core/src/manager/settings/shortcuts.tsx
for better type safety and event handling consistency