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

UI: Fix sidebar top and bottom addon slots #25426

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions code/ui/manager/src/container/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useMemo } from 'react';

import type { Combo, StoriesHash } from '@storybook/manager-api';
import { types, Consumer } from '@storybook/manager-api';
import { Consumer } from '@storybook/manager-api';
import { Addon_TypesEnum } from '@storybook/types';

import type { SidebarProps as SidebarComponentProps } from '../components/sidebar/Sidebar';
import { Sidebar as SidebarComponent } from '../components/sidebar/Sidebar';
Expand Down Expand Up @@ -41,9 +42,12 @@ const Sidebar = React.memo(function Sideber({ onMenuClick }: SidebarProps) {
const whatsNewNotificationsEnabled =
state.whatsNewData?.status === 'SUCCESS' && !state.disableWhatsNewNotifications;

const items = api.getElements(types.experimental_SIDEBAR_BOTTOM);
const bottom = useMemo(() => Object.values(items), [items]);
const top = useMemo(() => Object.values(api.getElements(types.experimental_SIDEBAR_TOP)), []);
const bottomItems = api.getElements(Addon_TypesEnum.experimental_SIDEBAR_BOTTOM);
const topItems = api.getElements(Addon_TypesEnum.experimental_SIDEBAR_TOP);
// eslint-disable-next-line react-hooks/exhaustive-deps
const bottom = useMemo(() => Object.values(bottomItems), [...Object.values(bottomItems)]);
Copy link
Member

Choose a reason for hiding this comment

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

What is the point of this kind of memoization???

Copy link
Member Author

Choose a reason for hiding this comment

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

You get the same array every time, unless bottomItems has new properties/values.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, so it's about preserving instance equality of the array so that the consuming UI does not re-render unnecessarily?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, exactly

// eslint-disable-next-line react-hooks/exhaustive-deps
const top = useMemo(() => Object.values(topItems), [...Object.values(topItems)]);

return {
title: name,
Expand Down
Loading