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

Fix infinite loop with layout cards module #132

Merged
merged 3 commits into from
Nov 7, 2023
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
1 change: 1 addition & 0 deletions src/core/card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function Card({
>
<div
className={classNames(
styles.content,
backgroundColor
? colorStyles[`background${getColor(backgroundColor)}`]
: isDelimited && colorStyles.backgroundFog,
Expand Down
6 changes: 6 additions & 0 deletions src/core/card/card.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@
}
}

.content {
@include respond_above(s) {
min-height: var(--height-image-desktop);
}
}

.textWrapper {
display: flex;
flex-direction: column;
Expand Down
11 changes: 8 additions & 3 deletions src/core/pageContent/PageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { CardModule } from '../pageModules/CardModule/CardModule';
import { CardsModule } from '../pageModules/CardsModule/CardsModule';
import { ImageModule } from '../pageModules/ImageModule/ImageModule';
import { StepsModule } from '../pageModules/StepsModule/StepsModule';
import createHashKey from '../utils/createHashKey';

export type PageContentProps = {
page?: PageType | ArticleType;
Expand Down Expand Up @@ -67,17 +68,20 @@ export const defaultContentModules = (
page: PageType | ArticleType,
): React.ReactNode[] => {
const contentModules: React.ReactNode[] = [];
page?.modules?.map((module) => {
page?.modules?.map((module, index) => {
const uniqueKey = createHashKey(`${index}-${JSON.stringify(module)}`);
if (isLayoutContent(module)) {
contentModules.push(
<ContentModule
key={uniqueKey}
content={module.content}
backgroundColor={module.backgroundColor}
/>,
);
} else if (isLayoutCard(module)) {
contentModules.push(
<CardModule
key={uniqueKey}
title={module.title}
text={module.description}
backgroundColor={module.backgroundColor}
Expand All @@ -91,12 +95,13 @@ export const defaultContentModules = (
/>,
);
} else if (isLayoutCards(module)) {
contentModules.push(<CardsModule items={module.cards} />);
contentModules.push(<CardsModule key={uniqueKey} items={module.cards} />);
} else if (isLayoutImage(module)) {
contentModules.push(<ImageModule />);
contentModules.push(<ImageModule key={uniqueKey} />);
} else if (isLayoutSteps(module)) {
contentModules.push(
<StepsModule
key={uniqueKey}
title={module.title}
steps={module.steps.map((step) => ({
title: step.title,
Expand Down
32 changes: 20 additions & 12 deletions src/core/pageModules/CardsModule/CardsModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames';

import styles from '../pageModules.module.scss';
import { SimpleCard } from './SimpleCard';
import createHashKey from '../../utils/createHashKey';

type Card = {
backgroundColor?: string;
Expand Down Expand Up @@ -32,18 +33,25 @@ export function CardsModule({ items }: CardsModuleProps) {
items.length === 1 && styles.singleGridWrapper,
)}
>
{items?.map((card) => (
<SimpleCard
title={card.title}
description={card.description}
linkTarget={card.link.target}
linkTitle={card.link.title}
linkUrl={card.link.url}
backgroundColor={card.backgroundColor}
direction={items.length === 1 ? 'horisontal' : 'vertical'}
icon={card.icon}
/>
))}
{items?.map((card, index) => {
// The card module does not contain any proeprty that could be used as an unique id, so one needs to be created
const uniqueKey = createHashKey(
`${index}-${card.title}-${card.description}`,
);
return (
<SimpleCard
key={uniqueKey}
title={card.title}
description={card.description}
linkTarget={card.link.target}
linkTitle={card.link.title}
linkUrl={card.link.url}
backgroundColor={card.backgroundColor}
direction={items.length === 1 ? 'horisontal' : 'vertical'}
icon={card.icon}
/>
);
})}
</div>
);
}
12 changes: 8 additions & 4 deletions src/core/pageModules/CardsModule/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ type IconProps = {
export function Icon({ name }: IconProps) {
// eslint-disable-next-line react/jsx-no-useless-fragment
const fallback = () => <div />;
const IconComponent = React.lazy(() =>
import('../../../common/components/icons').then((module) => ({
default: module[name] || fallback,
})),
const IconComponent = React.useMemo(
() =>
React.lazy(() =>
import('../../../common/components/icons').then((module) => ({
default: module[name] || fallback,
})),
),
[name],
);
return (
<Suspense fallback={<div className={styles.fallbackIcon} />}>
Expand Down
16 changes: 12 additions & 4 deletions src/core/pageModules/StepsModule/StepsModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import classNames from 'classnames';
import styles from '../pageModules.module.scss';
import colorStyles from '../../styles/background.module.scss';
import { getColor, getTextFromHtml, isWhiteText } from '../../utils/string';
import createHashKey from '../../utils/createHashKey';

export type Step = {
content: string;
Expand All @@ -29,6 +30,16 @@ export function StepsModule({
type,
className,
}: StepsModuleProps) {
const stepsContents = steps?.map((step, index) => {
// The card module does not contain any proeprty that could be used as an unique id, so one needs to be created
const uniqueKey = createHashKey(`${index}-${step.title}-${step.content}`);
return {
key: uniqueKey,
title: step.title,
description: getTextFromHtml(step.content),
};
});

return (
<div className={styles.pageModuleWrapper}>
<StepByStep
Expand All @@ -40,10 +51,7 @@ export function StepsModule({
className,
)}
helpText={getTextFromHtml(helpText)}
steps={steps?.map((step) => ({
title: step.title,
description: getTextFromHtml(step.content),
}))}
steps={stepsContents}
title={title}
/>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/core/utils/createHashKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const MAX_KEY_LENGTH = 200;
export default function createHashKey(source: string | object) {
const data = typeof source === 'string' ? source : JSON.stringify(source);
return btoa(unescape(encodeURIComponent(data))).substring(0, MAX_KEY_LENGTH);
}
Loading