Skip to content

Commit

Permalink
Consolidate a place where to check linkProps (#1121)
Browse files Browse the repository at this point in the history
**Related Ticket:** Very vaguely related to #946 

This PR makes the Card component delegate what link component to render
to the `SmartLink` component. I initially started working on this PR to
continue the work on #1096,
more specifically, to make the link component come from props. I thought
this change was worth merging regardless of whether we work on the
routing problem now or later.
  • Loading branch information
sandrahoang686 authored Aug 30, 2024
2 parents f0a4ace + 7877108 commit 78c79e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions app/scripts/components/common/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
themeVal,
listReset,
} from '@devseed-ui/theme-provider';
import SmartLink from '../smart-link';
import { CardBody, CardBlank, CardHeader, CardHeadline, CardTitle, CardOverline } from './styles';
import HorizontalInfoCard, { HorizontalCardStyles } from './horizontal-info-card';
import { variableBaseType, variableGlsp } from '$styles/variable-utils';

import { ElementInteractive } from '$components/common/element-interactive';
import { Figure } from '$components/common/figure';
import { getLinkProps } from '$utils/url';



type CardType = 'classic' | 'cover' | 'featured' | 'horizontal-info';

Expand Down Expand Up @@ -259,16 +261,20 @@ function CardComponent(props: CardComponentProps) {
} = props;

const isExternalLink = /^https?:\/\//.test(linkTo);
const linkProps = getLinkProps(linkTo, Link, onLinkClick);


return (
<ElementInteractive
linkProps={{
as: SmartLink,
to: linkTo,
onLinkClick
}}
as={CardItem}
cardType={cardType}
className={className}
linkLabel={linkLabel ?? 'View more'}
linkProps={linkProps}
linkTo={linkTo}
onLinkClick={onLinkClick}
onClickCapture={onCardClickCapture}
>
{
Expand Down
6 changes: 4 additions & 2 deletions app/scripts/components/common/smart-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { getLinkProps } from '$utils/url';

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

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

return isExternalLink ? (
<a {...linkProps} {...rest}> {children} </a>
) : (
Expand Down

0 comments on commit 78c79e1

Please sign in to comment.