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

Hotfix to hide the external link badge from cards #1231

Merged
merged 3 commits into from
Nov 5, 2024
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
16 changes: 7 additions & 9 deletions app/scripts/components/common/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ export interface LinkProperties {

export interface LinkWithPathProperties extends LinkProperties {
linkTo: string;
isLinkExternal?: boolean;
}

export interface CardComponentBaseProps {
Expand All @@ -243,6 +242,7 @@ export interface CardComponentBaseProps {
parentTo?: string;
tagLabels?: string[];
footerContent?: JSX.Element;
hideExternalLinkBadge?: boolean;
onCardClickCapture?: MouseEventHandler;
}

Expand All @@ -251,7 +251,6 @@ export interface CardComponentBaseProps {
export interface CardComponentPropsDeprecated extends CardComponentBaseProps {
linkTo: string;
onLinkClick?: MouseEventHandler;
isLinkExternal?: boolean;
}
export interface CardComponentProps extends CardComponentBaseProps {
linkProperties: LinkWithPathProperties;
Expand All @@ -278,6 +277,7 @@ function CardComponent(props: CardComponentPropsType) {
tagLabels,
parentTo,
footerContent,
hideExternalLinkBadge,
onCardClickCapture
} = props;
// @TODO: This process is not necessary once all the instances adapt the linkProperties syntax
Expand All @@ -289,25 +289,23 @@ function CardComponent(props: CardComponentPropsType) {
const { linkProperties: linkPropertiesProps } = props;
linkProperties = linkPropertiesProps;
} else {
const { linkTo, onLinkClick, isLinkExternal } = props;
const { linkTo, onLinkClick } = props;
linkProperties = {
linkTo,
onLinkClick,
pathAttributeKeyName: 'to',
LinkElement: SmartLink,
isLinkExternal
LinkElement: SmartLink
};
}

const isExternalLink = linkProperties.isLinkExternal ?? /^https?:\/\//.test(linkProperties.linkTo);
const isExternalLink = /^https?:\/\//.test(linkProperties.linkTo);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yes! this is what we all needed. Thanks for thinking through this again 🙇


return (
<ElementInteractive
linkProps={{
as: linkProperties.LinkElement,
[linkProperties.pathAttributeKeyName]: linkProperties.linkTo,
onLinkClick: linkProperties.onLinkClick,
isLinkExternal: isExternalLink
onLinkClick: linkProperties.onLinkClick
}}
as={CardItem}
cardType={cardType}
Expand All @@ -322,7 +320,7 @@ function CardComponent(props: CardComponentPropsType) {
<CardHeadline>
<CardTitle>{title}</CardTitle>
<CardOverline as='div'>
{isExternalLink && <ExternalLinkFlag />}
{(hideExternalLinkBadge !== true && isExternalLink) && <ExternalLinkFlag />}
{!isExternalLink && tagLabels && parentTo && (
tagLabels.map((label) => (
<CardLabel as={linkProperties.LinkElement} to={parentTo} key={label}>
Expand Down
3 changes: 1 addition & 2 deletions app/scripts/components/common/featured-slider-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ function FeaturedSliderSection(props: FeaturedSliderSectionProps) {
linkProperties={{
linkTo: `${d.asLink?.url ?? getItemPath(d)}`,
pathAttributeKeyName: 'to',
LinkElement: SmartLink,
isLinkExternal: d.isLinkExternal
LinkElement: SmartLink
}}
title={d.name}
overline={
Expand Down
4 changes: 1 addition & 3 deletions app/scripts/components/common/related-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ interface FormatBlock {
date: string;
link: string;
asLink?: LinkContentData;
isLinkExternal?: boolean;
parentLink: string;
media: Media;
parent: RelatedContentData['type'];
Expand Down Expand Up @@ -152,8 +151,7 @@ export default function RelatedContent(props: RelatedContentProps) {
linkProperties={{
linkTo: `${t.asLink?.url ?? t.link}`,
LinkElement: SmartLink,
pathAttributeKeyName: 'to',
isLinkExternal: t.isLinkExternal
pathAttributeKeyName: 'to'
}}
title={t.name}
date={
Expand Down
16 changes: 7 additions & 9 deletions app/scripts/components/common/smart-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { getLinkProps } from '$utils/url';

interface SmartLinkProps {
to: string;
isLinkExternal?: boolean;
onLinkClick?: ()=> void;
children?: ReactNode;
}

/**
* Switches between a `a` and a `Link` depending on the optional `isLinkExternal` prop or the url.
* Switches between a `a` and a `Link` depending on the url.
*/
export default function SmartLink(props: SmartLinkProps) {
const { to, isLinkExternal, onLinkClick, children, ...rest } = props;
const isExternalLink = isLinkExternal ?? /^https?:\/\//.test(to);
const linkProps = getLinkProps(to, isLinkExternal, undefined, onLinkClick);
const { to, onLinkClick, children, ...rest } = props;
const isExternalLink = /^https?:\/\//.test(to);
const linkProps = getLinkProps(to, undefined, onLinkClick);

return isExternalLink ? (
<a {...linkProps} {...rest}> {children} </a>
Expand All @@ -28,15 +27,14 @@ export default function SmartLink(props: SmartLinkProps) {

interface CustomLinkProps {
href: string;
isLinkExternal?: boolean;
}

/**
* For links defined as markdown, this component will open the external link in a new tab depending on the optional `isLinkExternal` prop or the url.
* For links defined as markdown, this component will open the external link in a new tab.
*/
export function CustomLink(props: CustomLinkProps) {
const { href, isLinkExternal, ...rest } = props;
const isExternalLink = isLinkExternal ?? /^https?:\/\//.test(href);
const { href, ...rest } = props;
const isExternalLink = /^https?:\/\//.test(href);
const linkProps = getLinkProps(href);
return isExternalLink ? (
<a {...linkProps} {...rest} />
Expand Down
3 changes: 1 addition & 2 deletions app/scripts/components/home/featured-stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ function FeaturedStories() {
linkProperties={{
linkTo: `${d.asLink?.url ?? getStoryPath(d)}`,
LinkElement: SmartLink,
pathAttributeKeyName: 'to',
isLinkExternal: d.isLinkExternal
pathAttributeKeyName: 'to'
}}
title={d.name}
tagLabels={[getString('stories').one]}
Expand Down
2 changes: 0 additions & 2 deletions app/scripts/components/sandbox/cards/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function SandboxCards() {
<Card
linkLabel='View more'
linkTo='/'
isLinkExternal={false}
title='Cities Experiencing Clearer Air During Lockdowns'
description='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce varius erat in vulputate.'
date={new Date('2021-10-26')}
Expand All @@ -29,7 +28,6 @@ function SandboxCards() {
cardType='cover'
linkLabel='View more'
linkTo='/'
isLinkExternal={false}
title='Nitrogen Dioxide (NO₂)'
description='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce varius erat in vulputate.'
tagLabels={['Dataset']}
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/components/stories/hub/hub-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ export default function HubContent(props:HubContentProps) {
linkProperties={{
linkTo: `${d.asLink?.url ?? d.path}`,
LinkElement,
pathAttributeKeyName,
isLinkExternal: d.isLinkExternal
pathAttributeKeyName
}}
title={
<TextHighlight
Expand All @@ -187,6 +186,7 @@ export default function HubContent(props:HubContentProps) {
{d.description}
</TextHighlight>
}
hideExternalLinkBadge={d.hideExternalLinkBadge}
imgSrc={d.media?.src}
imgAlt={d.media?.alt}
footerContent={
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/types/veda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface StoryData {
taxonomy: Taxonomy[];
related?: RelatedContentData[];
asLink?: LinkContentData;
isLinkExternal?: boolean;
hideExternalLinkBadge?: boolean;
isHidden?: boolean;
}

Expand Down
3 changes: 1 addition & 2 deletions app/scripts/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ export function isExternalLink(link: string): boolean {

export const getLinkProps = (
linkTo: string,
isLinkExternal?: boolean,
as?: React.ForwardRefExoticComponent<
LinkProps & React.RefAttributes<HTMLAnchorElement>
>,
onClick?: (() => void) | MouseEventHandler
) => {
// Open the link in a new tab when link is external
const isExternalLink = isLinkExternal ?? /^https?:\/\//.test(linkTo);
const isExternalLink = /^https?:\/\//.test(linkTo);
return isExternalLink
? {
href: linkTo,
Expand Down
2 changes: 1 addition & 1 deletion mock/stories/external-link-example.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ media:
name: Unsplash
url: https://unsplash.com/
pubDate: 2023-02-09
hideExternalLinkBadge: false
taxonomy:
- name: Topics
values:
Expand All @@ -24,5 +25,4 @@ related:
id: air-quality-and-covid-19
asLink:
url: 'https://developmentseed.org'
isLinkExternal: true
---
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@developmentseed/veda-ui",
"description": "Dashboard",
"version": "5.9.0",
"version": "5.9.1",
"author": {
"name": "Development Seed",
"url": "https://developmentseed.org/"
Expand Down
2 changes: 1 addition & 1 deletion parcel-resolver-veda/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ declare module 'veda' {
taxonomy: Taxonomy[];
related?: RelatedContentData[];
asLink?: LinkContentData;
isLinkExternal?: boolean;
hideExternalLinkBadge?: boolean;
isHidden?: boolean;
}

Expand Down
Loading