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: Use SPA navigation from datasets list to Explore #20890

Merged
merged 6 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions superset-frontend/src/components/GenericLink/GenericLink.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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.
*/

import React from 'react';
import { render, screen } from 'spec/helpers/testing-library';
import { GenericLink } from './GenericLink';

test('renders', () => {
render(<GenericLink to="/explore">Link to Explore</GenericLink>, {
useRouter: true,
});
expect(screen.getByText('Link to Explore')).toBeVisible();
});

test('navigates to internal URL', () => {
render(<GenericLink to="/explore">Link to Explore</GenericLink>, {
useRouter: true,
});
const internalLink = screen.getByTestId('internal-link');
expect(internalLink).toHaveAttribute('href', '/explore');
});

test('navigates to external URL', () => {
render(
<GenericLink to="https://superset.apache.org/">
Link to external website
</GenericLink>,
{ useRouter: true },
);
const externalLink = screen.getByTestId('external-link');
expect(externalLink).toHaveAttribute('href', 'https://superset.apache.org/');
});
51 changes: 51 additions & 0 deletions superset-frontend/src/components/GenericLink/GenericLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.
*/

import React from 'react';
import { Link, LinkProps } from 'react-router-dom';

export const GenericLink = ({
to,
component,
replace,
innerRef,
children,
...rest
}: // css prop type check was failing, override with any
Copy link
Member

Choose a reason for hiding this comment

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

The type of Link properties is not LinkProps but React.PropsWithoutRef<LinkProps<S>> & React.RefAttributes<HTMLAnchorElement>.

To fix the Typescript error, you can define your component like:

export const GenericLink = <S,>({
  to,
  component,
  replace,
  innerRef,
  children,
  ...rest
}: React.PropsWithoutRef<LinkProps<S>> &
  React.RefAttributes<HTMLAnchorElement>) => {
   ...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh nice!

LinkProps & { css?: any }) => {
if (typeof to === 'string' && /^https?:\/\//.test(to)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to improve the algorithm to detect internal and external URLs. If we type www.google.com without the https then it breaks. I'm not sure if we already have something similar in the codebase or if we need to find an npm package for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

True. Found some more complex regex, I'll use that one instead + some unit tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually that's not really a valid concern in this case. URL passed to <a> must have a protocol prefix, otherwise it's appended to current pathname. In such case it doesn't matter if we use <a> or <Link>.

image

Copy link
Member

Choose a reason for hiding this comment

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

You can design the component with the protocol restriction, but then we need to handle the problem elsewhere because it will be really common to have users type the URL without the protocol. In this case, when they click on the dataset nothing happens.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I see your point. However, I'm wondering if that should be considered a separate bug. Currently, if you type "www.google.com" in default url field, the link would redirect you to http://localhost:9000/tablemodelview/list/www.google.com.
This PR, even with more foolproof external url detection, wouldn't change that behaviour - we'd still get an <a> element linking to http://localhost:9000/tablemodelview/list/www.google.com.
I'd propose adding external URL detection in this PR (even though it wouldn't really do anything) and handling external links without protocol in a separate PR

Copy link
Member

Choose a reason for hiding this comment

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

This PR, even with more foolproof external url detection, wouldn't change that behaviour - we'd still get an element linking to http://localhost:9000/tablemodelview/list/www.google.com.

I was thinking that upon detecting www.google.com as an external link without a protocol, then the component would add the protocol automatically, setting the <a> tag to http://www.google.com. Of course, we can implement this outside of the component but in that case we would need to always call the helper function before using the component.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done :)

Screen.Recording.2022-07-29.at.14.20.16.mov

return (
<a data-test="external-link" href={to} {...rest}>
{children}
</a>
);
}
return (
<Link
data-test="internal-link"
to={to}
component={component}
replace={replace}
innerRef={innerRef}
{...rest}
>
{children}
</Link>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import ImportModelsModal from 'src/components/ImportModal/index';
import { isFeatureEnabled, FeatureFlag } from 'src/featureFlags';
import WarningIconWithTooltip from 'src/components/WarningIconWithTooltip';
import { isUserAdmin } from 'src/dashboard/util/permissionUtils';
import { GenericLink } from 'src/components/GenericLink/GenericLink';
import AddDatasetModal from './AddDatasetModal';

import {
Expand Down Expand Up @@ -289,7 +290,11 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
},
},
}: any) => {
const titleLink = <a href={exploreURL}>{datasetTitle}</a>;
const titleLink = (
// exploreUrl can be a link to Explore or an external link
// in the first case use SPA routing, else use HTML anchor
<GenericLink to={exploreURL}>{datasetTitle}</GenericLink>
);
try {
const parsedExtra = JSON.parse(extra);
return (
Expand Down