Skip to content

Commit

Permalink
Lazyload SmartLink to avoid loading react-router-dom + use a proper l…
Browse files Browse the repository at this point in the history
…inkProperties (#1160)

**Related Ticket:** follow up of follow up -_-
#1159

### Description of Changes
Urg I used wrong attributes, so the previous fix didn't really fix the
problem.
The previous approach broke the library build too by including
SmartLink, which uses react-router-dom. I think I worked around by lazy
loading the component in this PR. I tested it locally but if you can,
please give it another test 🙇

### Notes & Questions About Changes


### Validation / Testing
ok, this time, I actually created a preview on GHG; this is the page
that uses Card component:
https://deploy-preview-596--ghg-demo.netlify.app/learn
  • Loading branch information
hanbyul-here authored Sep 20, 2024
2 parents 14c9a16 + e7065ff commit 7ce68a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
29 changes: 16 additions & 13 deletions app/scripts/components/common/card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import React, { MouseEventHandler } from 'react';
import React, { lazy, MouseEventHandler, ComponentType } from 'react';
import styled, { css } from 'styled-components';
import { Link } from 'react-router-dom';
import { format } from 'date-fns';
import { CollecticonExpandTopRight } from '@devseed-ui/collecticons';

import { Link } from 'react-router-dom';
const SmartLink = lazy(() => import('../smart-link'));
import {
glsp,
media,
multiply,
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';
Expand Down Expand Up @@ -221,14 +221,17 @@ export function ExternalLinkFlag() {
}

export interface LinkProperties {
LinkElement: JSX.Element | ((props: any) => JSX.Element);
LinkElement: JSX.Element | ((props: any) => JSX.Element) | ComponentType<any>;
pathAttributeKeyName: string;
onLinkClick?: MouseEventHandler;
}

export interface LinkWithPathProperties extends LinkProperties {
linkTo: string;
}

export interface CardComponentBaseProps {
title: JSX.Element | string;

linkLabel?: string;
className?: string;
cardType?: CardType;
Expand All @@ -244,14 +247,13 @@ export interface CardComponentBaseProps {
}

// @TODO: Consolidate these props when the instance adapts the new syntax
// Specifically: https://github.com/US-GHG-Center/veda-config-ghg/blob/develop/custom-pages/news-and-events/component.tsx#L108
export interface CardComponentPropsDeprecated extends CardComponentBaseProps {
linkTo: string;
onLinkClick?: MouseEventHandler;
}
export interface CardComponentProps extends CardComponentBaseProps {
linkProperties: {
linkTo: string,
} & LinkProperties;
linkProperties: LinkWithPathProperties;
}

type CardComponentPropsType = CardComponentProps | CardComponentPropsDeprecated;
Expand All @@ -277,8 +279,9 @@ function CardComponent(props: CardComponentPropsType) {
footerContent,
onCardClickCapture
} = props;

let linkProperties;
// @TODO: This process is not necessary once all the instances adapt the linkProperties syntax
// Consolidate them to use LinkProperties only
let linkProperties: LinkWithPathProperties;

if (hasLinkProperties(props)) {
// Handle new props with linkProperties
Expand All @@ -287,10 +290,10 @@ function CardComponent(props: CardComponentPropsType) {
} else {
const { linkTo, onLinkClick, } = props;
linkProperties = {
to: linkTo,
linkTo,
onLinkClick,
pathAttributeKeyName: 'to',
linkComponent: SmartLink
LinkElement: SmartLink
};
}

Expand Down
3 changes: 3 additions & 0 deletions app/scripts/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import Hug from "$styles/hug";
import { Actions } from "$components/common/browse-controls/use-browse-controls";
import Image from "$components/common/blocks/images";

import SmartLink from "$components/common/smart-link";

export {
STORIES_PATH,
DATASETS_PATH,
Expand All @@ -67,6 +69,7 @@ export {
FoldProse,
Hug,
Image,
SmartLink,
Pill,
Tip,
VarHeading
Expand Down

0 comments on commit 7ce68a2

Please sign in to comment.