-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
+203
−2
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
779f7db
feat: Use SPA navigation from datasets list to Explore
kgabryje cbabb4f
fix lint
kgabryje 15051b1
Fix lint
kgabryje 8581f26
Fix type
kgabryje ab26012
Make external links work without protocols
kgabryje 09db402
add comment
kgabryje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
superset-frontend/src/components/GenericLink/GenericLink.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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/'); | ||
}); | ||
|
||
test('navigates to external URL without host', () => { | ||
render( | ||
<GenericLink to="superset.apache.org/"> | ||
Link to external website | ||
</GenericLink>, | ||
{ useRouter: true }, | ||
); | ||
const externalLink = screen.getByTestId('external-link'); | ||
expect(externalLink).toHaveAttribute('href', '//superset.apache.org/'); | ||
}); |
52 changes: 52 additions & 0 deletions
52
superset-frontend/src/components/GenericLink/GenericLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 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'; | ||
import { isUrlExternal, parseUrl } from 'src/utils/urlUtils'; | ||
|
||
export const GenericLink = <S,>({ | ||
to, | ||
component, | ||
replace, | ||
innerRef, | ||
children, | ||
...rest | ||
}: React.PropsWithoutRef<LinkProps<S>> & | ||
React.RefAttributes<HTMLAnchorElement>) => { | ||
if (typeof to === 'string' && isUrlExternal(to)) { | ||
return ( | ||
<a data-test="external-link" href={parseUrl(to)} {...rest}> | ||
{children} | ||
</a> | ||
); | ||
} | ||
return ( | ||
<Link | ||
data-test="internal-link" | ||
to={to} | ||
component={component} | ||
replace={replace} | ||
innerRef={innerRef} | ||
{...rest} | ||
> | ||
{children} | ||
</Link> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* 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 { isUrlExternal, parseUrl } from './urlUtils'; | ||
|
||
test('isUrlExternal', () => { | ||
expect(isUrlExternal('http://google.com')).toBeTruthy(); | ||
expect(isUrlExternal('https://google.com')).toBeTruthy(); | ||
expect(isUrlExternal('//google.com')).toBeTruthy(); | ||
expect(isUrlExternal('google.com')).toBeTruthy(); | ||
expect(isUrlExternal('www.google.com')).toBeTruthy(); | ||
expect(isUrlExternal('mailto:mail@example.com')).toBeTruthy(); | ||
|
||
// treat all urls starting with protocol or hostname as external | ||
// such urls are not handled well by react-router Link component | ||
expect(isUrlExternal('http://localhost:8888/port')).toBeTruthy(); | ||
expect(isUrlExternal('https://localhost/secure')).toBeTruthy(); | ||
expect(isUrlExternal('http://localhost/about')).toBeTruthy(); | ||
expect(isUrlExternal('HTTP://localhost/about')).toBeTruthy(); | ||
expect(isUrlExternal('//localhost/about')).toBeTruthy(); | ||
expect(isUrlExternal('localhost/about')).toBeTruthy(); | ||
|
||
expect(isUrlExternal('/about')).toBeFalsy(); | ||
expect(isUrlExternal('#anchor')).toBeFalsy(); | ||
}); | ||
|
||
test('parseUrl', () => { | ||
expect(parseUrl('http://google.com')).toEqual('http://google.com'); | ||
expect(parseUrl('//google.com')).toEqual('//google.com'); | ||
expect(parseUrl('mailto:mail@example.com')).toEqual( | ||
'mailto:mail@example.com', | ||
); | ||
expect(parseUrl('google.com')).toEqual('//google.com'); | ||
expect(parseUrl('www.google.com')).toEqual('//www.google.com'); | ||
|
||
expect(parseUrl('/about')).toEqual('/about'); | ||
expect(parseUrl('#anchor')).toEqual('#anchor'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small suggestion for a follow-up: You could return the parsed value when it's valid and undefined or another thing when it's invalid. That way you would only need to apply the regex once by combining
isUrlExternal
withparseUrl
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I'll do that in a separate PR