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

[BUG] HeaderControl with setAppRightControls mount point not updated if not mounted during initial render #8313

Open
wanglam opened this issue Sep 24, 2024 · 0 comments
Labels
bug Something isn't working untriaged

Comments

@wanglam
Copy link
Contributor

wanglam commented Sep 24, 2024

Describe the bug

The HeaderControl component, which is used to render controls in the header's right side, does not update its content when the controls prop changes if the component was not mounted during the initial render. This issue occurs when the HeaderControl component is conditionally rendered based on a state or prop that changes over time, and the condition is not met during the initial render.

To Reproduce

  1. Create a component that conditionally renders the HeaderControl component based on a state or prop that changes over time.
  2. Ensure that the condition for rendering the HeaderControl component is not met during the initial render.
  3. After the state or prop changes, and the condition is met, the HeaderControl component should be rendered, but its content (the controls prop) is not updated.

Sample Code

const [currentTime, setCurrentTime] = useState<number>();
useEffect(() => {
  let intervalId = -1;
  intervalId = window.setInterval(() => {
    setCurrentTime(new Date().getTime());
  });
  return () => {
    clearInterval(intervalId);
  };
}, []);

return (
  <>
    {!!currentTime && (
      <navigationUI.HeaderControl
        controls={[
          {
            description: `current time: ${currentTime}`,
          },
        ]}
        setMountPoint={application!.setAppRightControls}
      />
    )}
  </>
);

In the above code, the HeaderControl component should not be rendered at the first time (when currentTime is undefined) and should be rendered after 1 second with the current time. However, the current time is not updated after the initial render.

Expected behavior
The HeaderControl component should update its content (the controls prop) whenever the state or prop changes, even if the component was not mounted during the initial render.

OpenSearch Version
N/A

Dashboards Version
Latest main

Plugins
N/A

Screenshots
N/A

Host/Environment (please complete the following information):

  • OS: N/A
  • Browser and version: N/A

Additional context
The root cause might be related to the headerRightControl not being rendered due to a conditional return statement in the Header component: https://github.com/opensearch-project/OpenSearch-Dashboards/blob/main/src/core/public/chrome/ui/header/header.tsx#L398

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working untriaged
Projects
None yet
Development

No branches or pull requests

1 participant