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

Open all external links in a new tab #870

Merged
merged 11 commits into from
Apr 2, 2024
Merged

Conversation

sandesh-sp
Copy link
Contributor

@sandesh-sp sandesh-sp commented Mar 5, 2024

Closes #873

@sandesh-sp sandesh-sp requested a review from slesaad March 5, 2024 15:50
Copy link

netlify bot commented Mar 5, 2024

Deploy Preview for veda-ui ready!

Name Link
🔨 Latest commit 58c89ec
🔍 Latest deploy log https://app.netlify.com/sites/veda-ui/deploys/6605c50b63a8cc00084c4776
😎 Deploy Preview https://deploy-preview-870--veda-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@slesaad
Copy link
Member

slesaad commented Mar 5, 2024

@sandesh-sp i tried the deploy preview link and this external link opened in the same tab 👀
image

@sandesh-sp
Copy link
Contributor Author

@slesaad I didn't consider the external links on the cards but only within the stories. Let me look at that right away.

@sandesh-sp
Copy link
Contributor Author

@slesaad External links present on cards should also open in a new tab now

@j08lue j08lue requested a review from sandrahoang686 March 6, 2024 10:00
Copy link
Contributor

@j08lue j08lue left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, @sandesh-sp! Please always reference the ticket you are working off of.

I guess it was this one:

But I had not considered that we would change this centrally in VEDA UI, so I now created a ticket in this repo for it:

As I stated in the ticket, we need to clarify: Would it be very complex to make this behavior optional?

@slesaad
Copy link
Member

slesaad commented Mar 6, 2024

@j08lue @sandrahoang686 yes, we do want to make the behavior optional. @sandesh-sp is looking at making it configurable via the config file in the the veda-config repo probably via the https://github.com/NASA-IMPACT/veda-config/blob/develop/veda.config.js file or an env var?

@j08lue
Copy link
Contributor

j08lue commented Mar 6, 2024

via the veda.config.js file or an env var?

For the E&A feature flag, we chose the .env file, like here.

But I'll defer to @sandrahoang686 to advise.

@sandrahoang686
Copy link
Collaborator

sandrahoang686 commented Mar 7, 2024

@slesaad @sandesh-sp I would recommend to make this type of configuration in the veda.config.js file since we use that file to specify overrideable behavior to things like stylistic changes already. Also usually env vars are typically related at the deployment level and the deployment environment while I think of configuration files at the code level and storing info in relation to the app, in this case, instance.

@sandesh-sp
Copy link
Contributor Author

Creating a new pull request in veda-config-ghg
US-GHG-Center/veda-config-ghg#326

@j08lue
Copy link
Contributor

j08lue commented Mar 19, 2024

I see you closed this pull request, @sandesh-sp. Is the change in VEDA UI not needed?

@slesaad slesaad reopened this Mar 19, 2024
@slesaad
Copy link
Member

slesaad commented Mar 19, 2024

@j08lue I think that was a mistake. Reopening. @sandrahoang686 @hanbyul-here can you guys have a look at the approach followed here and let us know what you think?

@sandrahoang686
Copy link
Collaborator

@slesaad @sandesh-sp this looks like a better path, thanks for making the updates! Could we possibly move this defining of linkProps into a more global util file? I think app/scripts/utils/url.ts should be fine. Then maybe update the method to work for those three cases.. something like below possibly:

const getLinkProps = (
  isExternalLink: string[] | boolean,
  linkTo: string,
  as?: React.ForwardRefExoticComponent<LinkProps & React.RefAttributes<HTMLAnchorElement>>,
  onClick?: () => void
) => {
  const externalLinksInNewTab = getBoolean('externalLinksInNewTab');
  return isExternalLink
  ? {
      href: linkTo,
      ...(externalLinksInNewTab
        ? {target: '_blank', rel: 'noopener noreferrer'}
        : {}),
      ...(onClick ? {onClick: onClick} : {})
    }
  : {
      ...(as ? {as: as} : {}),
      to: linkTo,
      ...(onClick ? {onClick: onClick} : {})
    };
}

For SmartLink it would then look like...

export default function SmartLink(props: SmartLinkProps) {
  const { to, ...rest } = props;
  const isExternalLink = /^https?:\/\//.test(to);
  const linkProps = getLinkProps(isExternalLink, to);
  return isExternalLink ? (
    externalLinksInNewTab ? (
      <a {...linkProps} {...rest} />
  ) : (
    <Link {...linkProps} {...rest} />
  );
}

@sandesh-sp
Copy link
Contributor Author

Thanks @sandrahoang686 ! I'll make the changes as suggested

@sandesh-sp
Copy link
Contributor Author

@sandrahoang686 could you have a look to the new changes. I defined getLinkProps on utils/url which was then used to get the needed attributes for the links.
ts-check is throwing an error around the type of onClick argument (currently defined as MouseEventHandler) and I'm not sure what its value should be.

const getLinkProps = (
  isExternalLink: string[] | boolean,
  linkTo: string,
  as?: React.ForwardRefExoticComponent<LinkProps & React.RefAttributes<HTMLAnchorElement>>,
  onClick?: MouseEventHandler
)

@sandrahoang686
Copy link
Collaborator

sandrahoang686 commented Mar 25, 2024

@sandesh-sp what is your exact error? Maybe try typing onClick with onClick?: (() => void) | MouseEventHandler;.

Another error looks like Link is expecting the to prop and in this case it looks like typescript is not catching on since it does look like your code would return that prop if !isExternalLink.. a solution might be to check that linkProps does return to or type the return?

@sandesh-sp
Copy link
Contributor Author

Thanks @sandrahoang686, this seems to be working. Could you please review it.

return isExternalLink
? {
href: linkTo,
to: linkTo,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this work? I tested in the deploy preview and looks like the external link I clicked wasn't opening in a new tab?

Screenshot 2024-03-28 at 5 20 34 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sandrahoang686 that's because we also need externalLinksInNewTab set to true on veda-config/veda.config.js.
It is working on veda-config-ghg (US-GHG-Center/veda-config-ghg#326) and also on my local machine.
I had to set it this way to prevent typescript error which was expecting "to" prop from the function

Copy link
Collaborator

@sandrahoang686 sandrahoang686 left a comment

Choose a reason for hiding this comment

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

lgtm :shipit:

@sandesh-sp
Copy link
Contributor Author

@j08lue @slesaad Could you please review the changes?

@sandrahoang686
Copy link
Collaborator

sandrahoang686 commented Mar 29, 2024

@sandesh-sp letting you know Jonas is out and should be back monday !

Copy link
Contributor

@j08lue j08lue left a comment

Choose a reason for hiding this comment

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

Referring to @sandrahoang686 for an authoritative review of the technical implementation.

From a feature perspective, this seems complete now with the option to enable / disable external links to open in new tabs per-instance.

@j08lue j08lue merged commit 9412b06 into main Apr 2, 2024
8 checks passed
@j08lue j08lue deleted the feat/external-links-new-tab branch April 2, 2024 09:35
@j08lue j08lue removed the request for review from slesaad April 2, 2024 09:37
hanbyul-here added a commit that referenced this pull request May 1, 2024
## 🎉 Features
- Zoom in AOI, TOI when analysis is run in
#906
- Add custom javascript injection
#846
- ADR for V2 Refactor: #875
- Open all external links in a new tab in
#870
- Include dataset id to filter layers in
#910
- Some datasets can only be analyzed with layers from the same source in
#913
- Create minimal partial data layer scaffold starting off with Data
Catalog for VEDA2 Refactor in
#893
- Add analysis preset in #921

## 🚀 Improvements
- Chart style improvement in
#903
- Data Catalog enhancement with floating filter sidebar in
#918
- Sum as statistics option in
#925
-
## 🐛 Fixes
- Sort featured stories based on publication date in descending order in
#907
- Replace latency with temporalResolution in layer info in
#898
- Add a workaround for Safari scroll problem in
#909
- Handle empty result in #922
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to open all external links in new tabs
5 participants