Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyford committed Dec 16, 2024
1 parent 5556115 commit f8901e9
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 69 deletions.
19 changes: 4 additions & 15 deletions dotcom-rendering/src/components/MultiByline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import { slugify } from '../model/enhance-H2s';
import { palette } from '../palette';
import type { MultiByline as MultiBylineModel } from '../types/content';
import type { TagType } from '../types/tag';
import { Avatar } from './Avatar';
import { Bio } from './Bio';
import { EndNote } from './EndNote';
Expand Down Expand Up @@ -162,21 +161,18 @@ const bylineImageStyles = css`
interface Props {
multiByline: MultiBylineModel;
format: ArticleFormat;
tags: TagType[];
children: React.ReactNode;
}

export const MultiByline = ({ multiByline, format, tags, children }: Props) => {
export const MultiByline = ({ multiByline, format, children }: Props) => {
return (
<div css={multiBylineStyles} data-spacefinder-role="nested">
<Byline
title={multiByline.title}
byline={multiByline.byline}
bylineHtml={multiByline.bylineHtml}
contributorIds={multiByline.contributorIds}
imageOverrideUrl={multiByline.imageOverrideUrl}
imageUrl={multiByline.imageUrl}
format={format}
tags={tags}
/>
<Bio html={multiByline.bio} />
{children}
Expand All @@ -191,30 +187,23 @@ type BylineProps = {
title: string;
bylineHtml: string;
byline: string;
imageOverrideUrl?: string;
contributorIds: string[];
imageUrl?: string;
format: ArticleFormat;
tags: TagType[];
};

const Byline = ({
title,
bylineHtml,
byline,
imageOverrideUrl,
contributorIds,
imageUrl,
format,
tags,
}: BylineProps) => {
const sanitizedHtml = sanitise(bylineHtml, {
allowedAttributes: {
...defaults.allowedAttributes,
span: ['data-contributor-rel'],
},
});
const imageUrl =
imageOverrideUrl ??
tags.find((tag) => tag.id === contributorIds[0])?.bylineImageUrl;

return (
<div css={bylineWrapperStyles}>
Expand Down
33 changes: 1 addition & 32 deletions dotcom-rendering/src/components/MultiBylines.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@ const multiBylineWithLongHeader = {
byline: 'Richard Hillgrove Political Editor',
bylineHtml:
"<a href='/profile/richard-hillgrove'>Richard Hillgrove</a> Political Editor",
contributorIds: ['profile/richard-hillgrove'],
};

const multiBylineWithImageOverride = {
title: 'A byline with an image override url',
bio: testBioText,
body: [testTextElement],
byline: 'Guardian Contributor',
bylineHtml: "<a href='/profile/richard-hillgrove'>Richard Hillgrove</a>",
contributorIds: ['profile/richard-hillgrove'],
imageOverrideUrl:
imageUrl:
'https://i.guim.co.uk/img/uploads/2024/09/17/Maurice_Casey.png?width=180&dpr=1&s=none',
};

Expand All @@ -65,25 +55,14 @@ const multiBylineWithNoContributorLink = {
byline: 'Steve McQueen on Paul Gilroy',
bylineHtml:
"<span data-contributor-rel='author'>Steve McQueen</span> on Paul Gilroy",
contributorIds: [],
};

export const ThemeVariations = {
args: {
multiBylines: [
multiBylineWithLongHeader,
multiBylineWithImageOverride,
multiBylineWithNoContributorLink,
],
tags: [
{
title: 'Richard Hillgrove',
id: 'profile/richard-hillgrove',
type: 'contributor',
bylineImageUrl:
'https://i.guim.co.uk/img/static/sys-images/Guardian/Pix/pictures/2011/5/24/1306249890287/Richard-Hillgrove.jpg?width=100&dpr=2&s=none',
},
],
isLastElement: true,
/**
* This will be replaced by the `formats` parameter, but it's
Expand Down Expand Up @@ -142,14 +121,6 @@ export const DesignVariations = {
},
} satisfies Story;

export const DesignVariationsWithImageOverride = {
args: {
...ThemeVariations.args,
multiBylines: [multiBylineWithImageOverride],
},
parameters: DesignVariations.parameters,
} satisfies Story;

export const DesignVariationsWithNoContributorLink = {
args: {
...ThemeVariations.args,
Expand Down Expand Up @@ -217,7 +188,6 @@ export const Images = {
byline: 'Richard Hillgrove Guardian Contributor',
bylineHtml:
"<a href='/profile/richard-hillgrove'>Richard Hillgrove</a>",
contributorIds: ['profile/richard-hillgrove'],
},
{
title: 'The second byline',
Expand All @@ -232,7 +202,6 @@ export const Images = {
byline: 'Steve McQueen on Paul Gilroy',
bylineHtml:
"<span data-contributor-rel='author'>Steve McQueen</span> on Paul Gilroy",
contributorIds: [],
},
],
},
Expand Down
4 changes: 0 additions & 4 deletions dotcom-rendering/src/components/MultiBylines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
MultiByline as MultiBylineModel,
StarRating,
} from '../types/content';
import type { TagType } from '../types/tag';
import { MultiByline } from './MultiByline';

interface MultiBylineProps {
Expand All @@ -25,7 +24,6 @@ interface MultiBylineProps {
starRating?: StarRating;
multiBylines: MultiBylineModel[];
RenderArticleElement: ArticleElementRenderer;
tags: TagType[];
/**
* Whether this is the last element in the article. If true, no separator will be rendered.
*/
Expand All @@ -52,7 +50,6 @@ export const MultiBylines = ({
hideCaption,
starRating,
RenderArticleElement,
tags,
isLastElement,
}: MultiBylineProps) => {
return (
Expand All @@ -61,7 +58,6 @@ export const MultiBylines = ({
<MultiByline
multiByline={multiByline}
format={format}
tags={tags}
key={multiByline.title}
>
{multiByline.body.map((element) => (
Expand Down
1 change: 0 additions & 1 deletion dotcom-rendering/src/lib/ArticleRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const ArticleRenderer = ({
editionId={editionId}
totalElements={length}
isSectionedMiniProfilesArticle={isSectionedMiniProfilesArticle}
tags={tags}
/>
);
});
Expand Down
6 changes: 0 additions & 6 deletions dotcom-rendering/src/lib/renderElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import {
} from '../layouts/lib/interactiveLegacyStyling';
import type { ServerSideTests, Switches } from '../types/config';
import type { FEElement, RoleType, StarRating } from '../types/content';
import type { TagType } from '../types/tag';
import { ArticleDesign, type ArticleFormat } from './articleFormat';
import type { EditionId } from './edition';
import { getSharingUrls } from './sharing-urls';
Expand All @@ -94,7 +93,6 @@ type Props = {
totalElements?: number;
isListElement?: boolean;
isSectionedMiniProfilesArticle?: boolean;
tags?: TagType[];
};

// updateRole modifies the role of an element in a way appropriate for most
Expand Down Expand Up @@ -155,7 +153,6 @@ export const renderElement = ({
totalElements = 0,
isListElement = false,
isSectionedMiniProfilesArticle = false,
tags = [],
}: Props) => {
const isBlog =
format.design === ArticleDesign.LiveBlog ||
Expand Down Expand Up @@ -509,7 +506,6 @@ export const renderElement = ({
switches={switches}
editionId={editionId}
RenderArticleElement={RenderArticleElement}
tags={tags}
isLastElement={index === totalElements - 1}
/>
);
Expand Down Expand Up @@ -914,7 +910,6 @@ export const RenderArticleElement = ({
totalElements,
isListElement,
isSectionedMiniProfilesArticle,
tags = [],
}: Props) => {
const withUpdatedRole = updateRole(element, format);

Expand All @@ -940,7 +935,6 @@ export const RenderArticleElement = ({
totalElements,
isListElement,
isSectionedMiniProfilesArticle,
tags,
});

const needsFigure = !bareElements.has(element._type);
Expand Down
7 changes: 6 additions & 1 deletion dotcom-rendering/src/model/enhanceBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Newsletter,
} from '../types/content';
import type { RenderingTarget } from '../types/renderingTarget';
import type { TagType } from '../types/tag';
import { enhanceAdPlaceholders } from './enhance-ad-placeholders';
import { enhanceBlockquotes } from './enhance-blockquotes';
import { enhanceDisclaimer } from './enhance-disclaimer';
Expand All @@ -30,6 +31,7 @@ type Options = {
imagesForLightbox: ImageForLightbox[];
hasAffiliateLinksDisclaimer: boolean;
audioArticleImage?: ImageBlockElement;
tags?: TagType[];
};

const enhanceNewsletterSignup =
Expand All @@ -55,7 +57,10 @@ export const enhanceElements =
(format: ArticleFormat, blockId: string, options: Options) =>
(elements: FEElement[]): FEElement[] =>
[
enhanceLists(enhanceElements(format, blockId, options)),
enhanceLists(
enhanceElements(format, blockId, options),
options.tags,
),
enhanceTimeline(enhanceElements(format, blockId, options)),
enhanceDividers,
enhanceH2s,
Expand Down
27 changes: 19 additions & 8 deletions dotcom-rendering/src/model/enhanceLists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isUndefined } from '@guardian/libs';
import type { FEElement, ListBlockElement, ListItem } from '../types/content';
import type { TagType } from '../types/tag';

type ElementsEnhancer = (elements: FEElement[]) => FEElement[];

Expand Down Expand Up @@ -61,7 +62,7 @@ const constructMiniProfile =
};

const constructMultiByline =
(enhanceElements: ElementsEnhancer) =>
(enhanceElements: ElementsEnhancer, tags?: TagType[]) =>
({
title,
elements,
Expand All @@ -72,15 +73,24 @@ const constructMultiByline =
byline,
bylineHtml,
}: ListItem) => {
const contributorIdsArray = contributorIds ?? [];

let imageUrl;
if (imageOverrideUrl) {
imageUrl = imageOverrideUrl;
} else if (tags) {
imageUrl = tags.find((tag) => tag.id === contributorIdsArray[0])
?.bylineImageUrl;
}

// if the element is missing its title for any reason, we will skip it
if (!isUndefined(title) && title !== '') {
return [
{
title,
bio,
endNote,
imageOverrideUrl,
contributorIds: contributorIds ?? [],
imageUrl,
byline: byline ?? '',
bylineHtml: bylineHtml ?? '',
body: enhanceElements(elements),
Expand All @@ -93,6 +103,7 @@ const constructMultiByline =
const enhanceListBlockElement = (
element: ListBlockElement,
elementsEnhancer: ElementsEnhancer,
tags?: TagType[],
): FEElement[] => {
switch (element.listElementType) {
case 'KeyTakeaways':
Expand Down Expand Up @@ -127,7 +138,7 @@ const enhanceListBlockElement = (
{
_type: 'model.dotcomrendering.pageElements.MultiBylinesBlockElement',
multiBylines: element.items.flatMap(
constructMultiByline(elementsEnhancer),
constructMultiByline(elementsEnhancer, tags),
),
},
];
Expand All @@ -141,18 +152,18 @@ const enhanceListBlockElement = (
};

const enhance =
(elementsEnhancer: ElementsEnhancer) =>
(elementsEnhancer: ElementsEnhancer, tags?: TagType[]) =>
(element: FEElement): FEElement[] => {
switch (element._type) {
case 'model.dotcomrendering.pageElements.ListBlockElement': {
return enhanceListBlockElement(element, elementsEnhancer);
return enhanceListBlockElement(element, elementsEnhancer, tags);
}
default:
return [element];
}
};

export const enhanceLists =
(elementsEnhancer: ElementsEnhancer) =>
(elementsEnhancer: ElementsEnhancer, tags?: TagType[]) =>
(elements: FEElement[]): FEElement[] =>
elements.flatMap(enhance(elementsEnhancer));
elements.flatMap(enhance(elementsEnhancer, tags));
1 change: 1 addition & 0 deletions dotcom-rendering/src/types/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const enhanceArticleType = (
imagesForLightbox,
hasAffiliateLinksDisclaimer: !!data.affiliateLinksDisclaimer,
audioArticleImage: data.audioArticleImage,
tags: data.tags,
});

const mainMediaElements = enhanceMainMedia(
Expand Down
3 changes: 1 addition & 2 deletions dotcom-rendering/src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ export interface MultiByline {
body: FEElement[];
bio?: string;
endNote?: string;
imageOverrideUrl?: string;
contributorIds: string[];
imageUrl?: string;
byline: string;
bylineHtml: string;
}
Expand Down

0 comments on commit f8901e9

Please sign in to comment.