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

fix(react-components): Don't recreate toolbar buttons on rerender #4626

Merged
merged 1 commit into from
Jun 20, 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
21 changes: 12 additions & 9 deletions react-components/src/components/Architecture/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { ToolBar } from '@cognite/cogs.js';
import styled from 'styled-components';
import { useState, type ReactElement } from 'react';
import { useMemo, useState, type ReactElement } from 'react';
import { withSuppressRevealEvents } from '../../higher-order-components/withSuppressRevealEvents';
import { CommandButtons } from './CommandButton';
import { type BaseCommand } from '../../architecture/base/commands/BaseCommand';
Expand All @@ -20,12 +20,12 @@ export const MainToolbar = (): ReactElement => {
if (config === undefined) {
return <></>;
}
const commands = config.createMainToolbar();
const commands = useMemo(() => config.createMainToolbar(), [config]);
if (commands.length === 0) {
return <></>;
}
const style = config.createMainToolbarStyle();
return CreateToolToolbar(commands, style);
return <ToolbarContent commands={commands} style={style} />;
};

export const ActiveToolToolbar = (): ReactElement => {
Expand All @@ -41,15 +41,18 @@ export const ActiveToolToolbar = (): ReactElement => {
if (activeTool === undefined) {
return <></>;
}
const commands = activeTool.getToolbar();
const commands = useMemo(() => activeTool.getToolbar(), [activeTool]);
const style = activeTool.getToolbarStyle();
return CreateToolToolbar(commands, style);
return <ToolbarContent commands={commands} style={style} />;
};

const CreateToolToolbar = (
commands: Array<BaseCommand | undefined>,
style: PopupStyle
): ReactElement => {
const ToolbarContent = ({
commands,
style
}: {
commands: Array<BaseCommand | undefined>;
style: PopupStyle;
}): ReactElement => {
//
if (commands.length === 0) {
return <></>;
Expand Down
Loading