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

feat: add extension point to the right side of the menu bar #20514

Merged
merged 5 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type UiGeneratorText<P = void> = (props: P) => string | React.ReactElement;
export type UiOverrides = Partial<{
'embedded.documentation.description': UiGeneratorText;
'embedded.documentation.url': string;
'navbar.right': React.ComponentType;
}>;

/**
Expand Down
3 changes: 3 additions & 0 deletions superset-frontend/src/preamble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { configure, makeApi, supersetTheme } from '@superset-ui/core';
import { merge } from 'lodash';
import setupClient from './setup/setupClient';
import setupColors from './setup/setupColors';
import setupExtensions from './setup/setupExtensions';
import setupFormatters from './setup/setupFormatters';
import setupDashboardComponents from './setup/setupDasboardComponents';
import { BootstrapUser, User } from './types/bootstrapTypes';
Expand All @@ -32,6 +33,8 @@ if (process.env.WEBPACK_MODE === 'development') {
setHotLoaderConfig({ logLevel: 'debug', trackTailUpdates: false });
}

setupExtensions();

// eslint-disable-next-line import/no-mutable-exports
export let bootstrapData: {
user?: BootstrapUser;
Expand Down
21 changes: 21 additions & 0 deletions superset-frontend/src/setup/setupExtensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// For individual deployments to add custom overrides
export default function setupExtensions() {}
18 changes: 18 additions & 0 deletions superset-frontend/src/views/components/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import React from 'react';
import * as reactRedux from 'react-redux';
import fetchMock from 'fetch-mock';
import { render, screen } from 'spec/helpers/testing-library';
import setupExtensions from 'src/setup/setupExtensions';
import userEvent from '@testing-library/user-event';
import { getUiOverrideRegistry } from '@superset-ui/core';
import { Menu } from './Menu';

const dropdownItems = [
Expand Down Expand Up @@ -482,3 +484,19 @@ test('should render without QueryParamProvider', () => {
render(<Menu {...mockedProps} />, { useRedux: true });
expect(screen.queryByTestId('new-dropdown')).not.toBeInTheDocument();
});

test('should render an extension component if one is supplied', () => {
const uiOverrideRegistry = getUiOverrideRegistry();

uiOverrideRegistry.set('navbar.right', () => (
<>navbar.right extension component</>
));

setupExtensions();

render(<Menu {...mockedProps} />);

expect(
screen.getByText('navbar.right extension component'),
).toBeInTheDocument();
});
2 changes: 1 addition & 1 deletion superset-frontend/src/views/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Link } from 'react-router-dom';
import Icons from 'src/components/Icons';
import { useUiConfig } from 'src/components/UiConfigContext';
import { URL_PARAMS } from 'src/constants';
import RightMenu from './MenuRight';
import RightMenu from './RightMenu';
import { Languages } from './LanguagePicker';

interface BrandProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
css,
SupersetTheme,
SupersetClient,
getUiOverrideRegistry,
} from '@superset-ui/core';
import { MainNav as Menu } from 'src/components/Menu';
import { Tooltip } from 'src/components/Tooltip';
Expand All @@ -46,6 +47,8 @@ import {
} from './types';
import { MenuObjectProps } from './Menu';

const uiOverrideRegistry = getUiOverrideRegistry();

const versionInfoStyles = (theme: SupersetTheme) => css`
padding: ${theme.gridUnit * 1.5}px ${theme.gridUnit * 4}px
${theme.gridUnit * 4}px ${theme.gridUnit * 7}px;
Expand Down Expand Up @@ -255,6 +258,7 @@ const RightMenu = ({
}
return null;
};
const RightMenuExtension = uiOverrideRegistry.get('navbar.right');

const handleDatabaseAdd = () => setQuery({ databaseAdded: true });

Expand All @@ -274,6 +278,7 @@ const RightMenu = ({
onClick={handleMenuSelection}
onOpenChange={onMenuOpen}
>
{RightMenuExtension && <RightMenuExtension />}
{!navbarRight.user_is_anonymous && showActionDropdown && (
<SubMenu
data-test="new-dropdown"
Expand Down