Skip to content

Commit

Permalink
fix: post-Gatsby image work TS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehenson authored and kennethkalmer committed Mar 12, 2024
1 parent 90b6e5a commit c7c38f3
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 111 deletions.
24 changes: 8 additions & 16 deletions src/components/Homepage/BodySection/BodySection.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import cn from 'classnames';
import { ImageProps } from 'src/components/Image';
import { ImageProps, getImageFromList } from 'src/components/Image';
import { SectionProps } from '../HomepageContent';
import { BodySectionDescription } from './BodySectionDescription';
import { HeroCard } from './Card/HeroCard';
Expand Down Expand Up @@ -33,25 +32,13 @@ const gridGapVariants = {
4: 'gap-24',
};

const getImage = (images = [], name): { images: ImageProps[]; name: string } => {
const result = images.find((image) => image.base === name);

if (name && result === undefined) {
console.warn(`Could not find image '${name}' in list`, images);
}

return result;
};

export const BodySection = ({ section, images }: { section: SectionProps; images: ImageProps[] }) => {
const cards = section.cards ?? [];
const cardsExist = cards.length > 0;
const columns = section.columns;
const singleColumn = columns == 1;
const bottomMargin = sectionBottomMarginVariants[section.bottomMargin];

console.log(images);

return (
<section className={bottomMargin}>
{section.title && <h2 className="ui-text-h2">{section.title}</h2>}
Expand All @@ -64,8 +51,13 @@ export const BodySection = ({ section, images }: { section: SectionProps; images
>
{cards.map((card, index) => {
const Card = cardTypes[card.type];
const image = getImage(images, card.image);
return <Card key={index} {...card} image={image} />;
return (
<Card
key={index}
{...card}
{...(card.type !== 'hero' && { image: getImageFromList(images, card.image) })}
/>
);
})}
</div>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Homepage/BodySection/Card/FeatureCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Image } from '../../../Image';

export const FeatureCard = ({ title, content, image, links }: CardProps) => (
<div className="flex justify-between flex-col">
<Image image={image}></Image>
<Image image={image} />
<h2 style={{ fontSize: '1.5rem' }} className="mt-24 font-medium">
{title}
</h2>
<p style={{ fontSize: '1.125rem' }} className="text-dark-grey mt-8 mb-24 mr-8 leading-6">
{content}
</p>
<Links links={links}></Links>
<Links links={links} />
</div>
);
2 changes: 1 addition & 1 deletion src/components/Homepage/HomepageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ImageProps } from 'src/components/Image';
export type CardProps = {
title: string;
content: string;
image: string;
image: ImageProps;
type: 'hero' | 'feature' | 'sdk';
links?: LinkProps[];
callToAction?: CallToActionProps;
Expand Down
32 changes: 24 additions & 8 deletions src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { GatsbyImage, GatsbyImageData, getImage } from 'gatsby-plugin-image';
import { GatsbyImage, IGatsbyImageData, getImage } from 'gatsby-plugin-image';
import { ComponentProps } from 'react';

export type ImageProp = {
childImageSharp: GatsbyImageData;
export type ImageProps = {
childImageSharp: IGatsbyImageData;
extension: string;
publicURL: string;
src?: string;
base?: string;
};

export type ImageComponentProps = ComponentProps<'img'> & {
image: ImageProp;
image: ImageProps;
};

export const Image = ({ image, src, ...attribs }: ImageProps) => {
export const getImageFromList = (images: ImageProps[] = [], name?: string | ImageProps): ImageProps | undefined => {
const imageName = typeof name === 'string' ? name : name?.base;
const result = images.find((image) => image.base === imageName);

if (name && result === undefined) {
console.warn(`Could not find image '${name}' in list`, images);
}

return result;
};

export const Image = ({ image, src, ...attribs }: ImageComponentProps) => {
if (!image) {
return;
}
Expand All @@ -27,8 +40,11 @@ export const Image = ({ image, src, ...attribs }: ImageProps) => {
return <img {...attribs} src={publicURL} />;
}

const { alt } = attribs ?? '';
delete attribs.alt;
const { alt, className } = attribs ?? {};
const imageAttributes = { alt: alt ?? '', className };
const fetchedImage = getImage(image.childImageSharp);

return <GatsbyImage image={getImage(image)} alt={alt} {...attribs} />;
if (fetchedImage) {
return <GatsbyImage image={fetchedImage} {...imageAttributes} />;
}
};
3 changes: 1 addition & 2 deletions src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ export default function Link<TState>({
to,
external,
...props
}: React.PropsWithoutRef<GatsbyLinkProps<TState>>) {
}: React.PropsWithoutRef<GatsbyLinkProps<TState>> & { external?: boolean }) {
const isInternal = checkLinkIsInternal(to);
const isOnPage = to?.startsWith('#');

/**
* Relevant page of documentation: https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-link/#recommendations-for-programmatic-in-app-navigation
* "If you need this (in-app navigation) behavior, you should either use an anchor tag or import the navigate helper from gatsby"
*/
/* console.log(to, isInternal, !external, !isOnPage); */
if (isInternal && !external && !isOnPage) {
const href = normalizeLegacyDocsLink(to);

Expand Down
21 changes: 9 additions & 12 deletions src/components/ProductPage/BodySection/BodySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { QuickstartCard } from './Card/QuickstartCard';
import { ExampleCard } from './Card/ExampleCard';
import { TutorialCard } from './Card/TutorialCard';
import { CallToAction } from './CallToAction';
import { ImageProps, getImageFromList } from 'src/components/Image';

const cardTypes = {
feature: FeatureCard,
Expand All @@ -22,16 +23,6 @@ const betaPillStyle = {
paddingTop: '0.313rem',
};

const getImage = (images = [], name): { images: ImageProps[]; name: string } => {
const result = images.find((image) => image.base === name);

if (name && result === undefined) {
console.warn(`Could not find image '${name}' in list`, images);
}

return result;
};

export const BodySection = ({ section, images }: { section: SectionProps; images: ImageProps[] }) => {
const cards = section.cards ?? [];
const cardsExist = cards.length > 0;
Expand Down Expand Up @@ -64,8 +55,14 @@ export const BodySection = ({ section, images }: { section: SectionProps; images
>
{cards.map((card, index) => {
const Card = cardTypes[card.type as keyof typeof cardTypes];
const image = getImage(images, card.image);
return <Card key={index} {...card} image={image} />;

return (
<Card
key={index}
{...card}
{...(card.type !== 'hero' && { image: getImageFromList(images, card.image) })}
/>
);
})}
</div>
)}
Expand Down
3 changes: 1 addition & 2 deletions src/components/ProductPage/BodySection/Card/ExampleCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { CardProps } from '../../ProductPageContent';
import { Image } from '../../../Image';
import Link from '../../../Link';
Expand All @@ -9,7 +8,7 @@ export const ExampleCard = ({ title, image, link, external }: CardProps) => (
external={external}
className="items-center border border-extra-light-grey rounded-lg bg-extra-light-grey flex flex-col h-full hover:border-mid-grey hover:text-cool-black group"
>
<Image image={image} style={{ pointerEvents: 'none' }}></Image>
<Image image={image} style={{ pointerEvents: 'none' }} />
<p className="pb-24 ml-8" style={{ fontSize: '1rem' }}>
{title}
</p>
Expand Down
7 changes: 3 additions & 4 deletions src/components/ProductPage/BodySection/Card/FeatureCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import { CardProps } from '../../ProductPageContent';
import { Links } from './Links';
import { Image } from '../../../Image';

export const FeatureCard = ({ title, content, image, links }: CardProps) => (
<div className="border border-extra-light-grey rounded-lg bg-extra-light-grey flex justify-between block hover:border-mid-grey hover:text-cool-black group">
<div className="border border-extra-light-grey rounded-lg bg-extra-light-grey flex justify-between hover:border-mid-grey hover:text-cool-black group">
<div className="py-24 pl-24">
<div className="flex flex-col h-full">
<div>
Expand All @@ -13,9 +12,9 @@ export const FeatureCard = ({ title, content, image, links }: CardProps) => (
{content}
</p>
</div>
<Links links={links}></Links>
<Links links={links} />
</div>
</div>
<Image image={image}></Image>
<Image image={image} />
</div>
);
87 changes: 45 additions & 42 deletions src/components/ProductPage/BodySection/Card/Links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@ import Icon from '@ably/ui/core/Icon';
import { LinkProps } from '../../ProductPageContent';
import Link from '../../../Link';

export const Links = ({ links }: { links: LinkProps[] }) => (
<div className="mt-auto">
{links && links?.length == 1 && (
<div>
{links.map(({ href, external, text }, index) => {
return (
<div key={index + text} className="h-full flex">
<Link
to={href}
external={external}
className="text-gui-default font-medium mr-4"
target="_blank"
rel="noopener"
>
{text}
</Link>
<Icon name="icon-gui-arrow-right" size="1rem" />
</div>
);
})}
export const Links = ({ links }: { links?: LinkProps[] }) =>
links ? (
<>
<div className="mt-auto">
{links.length === 1 && (
<div>
{links.map(({ href, external, text }, index) => {
return (
<div key={index + text} className="h-full flex">
<Link
to={href}
external={external}
className="text-gui-default font-medium mr-4"
target="_blank"
rel="noopener"
>
{text}
</Link>
<Icon name="icon-gui-arrow-right" size="1rem" />
</div>
);
})}
</div>
)}
{links.length > 1 && (
<ul className="ui-unordered-list" style={{ marginLeft: '0.75rem', marginBottom: '0' }}>
{links.map(({ href, external, text }, index) => {
return (
<li key={index + text}>
<Link
to={href}
external={external}
className="text-gui-default"
target="_blank"
rel="noopener noreferrer"
>
{text}
</Link>
</li>
);
})}
</ul>
)}
</div>
)}
{links && links?.length > 1 && (
<ul className="ui-unordered-list" style={{ marginLeft: '0.75rem', marginBottom: '0' }}>
{links.map(({ href, external, text }, index) => {
return (
<li key={index + text}>
<Link
to={href}
external={external}
className="text-gui-default"
target="_blank"
rel="noopener noreferrer"
>
{text}
</Link>
</li>
);
})}
</ul>
)}
</div>
);
</>
) : null;
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import { CardProps } from '../../ProductPageContent';
import { Image } from '../../../Image';
import { Links } from './Links';

export const QuickstartCard = ({ title, content, image, links }: CardProps) => (
<div className="p-24 border border-extra-light-grey rounded-lg bg-extra-light-grey flex flex-col h-full justify-between hover:border-mid-grey hover:text-cool-black group">
<div className="flex items-center">
<Image image={image} alt={`${title}`} width="32px" />
<Image image={image} alt={title} width="32px" />
<h2 className="ml-8" style={{ fontSize: '1.25rem' }}>
{title}
</h2>
Expand All @@ -15,6 +14,6 @@ export const QuickstartCard = ({ title, content, image, links }: CardProps) => (
{content}
</p>

<Links links={links}></Links>
<Links links={links} />
</div>
);
3 changes: 1 addition & 2 deletions src/components/ProductPage/BodySection/Card/TutorialCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import { CardProps } from '../../ProductPageContent';
import { Image } from '../../../Image';
import { Links } from './Links';

export const TutorialCard = ({ title, image, links }: CardProps) => (
<div className="p-24 border border-extra-light-grey rounded-lg bg-extra-light-grey flex flex-col h-full justify-between block hover:border-mid-grey hover:text-cool-black group">
<div className="p-24 border border-extra-light-grey rounded-lg bg-extra-light-grey flex flex-col h-full justify-between hover:border-mid-grey hover:text-cool-black group">
<div className="flex mb-20">
<Image image={image} alt={title} width="32px" />
<h2 className="ml-8" style={{ fontSize: '1rem' }}>
Expand Down
5 changes: 2 additions & 3 deletions src/components/ProductPage/ProductPageContent.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react';
import { BodySection } from './BodySection/BodySection';
import { ImageProps } from 'src/components/Image';

export type LinkProps = {
text: string;
href: string;
external: string;
external: boolean;
};

export type CallToActionProps = {
Expand All @@ -19,7 +18,7 @@ export type CardProps = {
title: string;
type: string;
content: string;
image: string;
image: ImageProps;
link: string;
external: boolean;
links: LinkProps[];
Expand Down
2 changes: 1 addition & 1 deletion src/components/SDKsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Content = ({ tab }: { tab: string }) => {
<h1 className="w-full text-title-xl font-medium">{data.hero.title}</h1>
<p className="max-w-md text-h3 text-dark-grey font-medium pt-16">{data.hero.subtitle}</p>
</div>
<src className="hidden sm:block" src={hero}></src>
<img className="hidden sm:block" src={hero} />
</div>
</div>
<MainSection tab={tab as Tab} />
Expand Down
6 changes: 5 additions & 1 deletion src/components/StaticImage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import { graphql, useStaticQuery, withPrefix } from 'gatsby';
import { ComponentProps } from 'react';

Expand All @@ -10,6 +9,11 @@ export const StaticImage = ({ src, ...attribs }: ComponentProps<'img'>) => {
}
}
`);

if (!src) {
return;
}

const assetPrefix = result?.site.assetPrefix ?? '';
const srcUrl = `${assetPrefix}${withPrefix(src)}`;

Expand Down
Loading

0 comments on commit c7c38f3

Please sign in to comment.