Skip to content

Commit 547c0e5

Browse files
authored
Merge pull request #14861 from guardian/dina/fix-duplicated-branding-on-fronts
Fix duplicated same branding on Fronts
2 parents 8b0d402 + 1071f14 commit 547c0e5

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

dotcom-rendering/src/lib/branding.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const everyCardHasSameSponsor = ([
8888
/**
8989
* TODO verify that this is what is necessary for two branding to be equal
9090
*/
91-
const brandingEqual = (b1: Branding, b2: Branding) => {
91+
export const brandingEqual = (b1: Branding, b2: Branding): boolean => {
9292
return (
9393
b1.brandingType?.name === b2.brandingType?.name &&
9494
b1.sponsorName === b2.sponsorName
@@ -101,6 +101,33 @@ export const isPaidContentSameBranding = (
101101
collectionBranding?.kind === 'paid-content' &&
102102
!collectionBranding.hasMultipleBranding;
103103

104+
/**
105+
* Determines if cards should have their branding stripped
106+
* Returns true if all cards have paid-content branding with the same sponsor
107+
*
108+
* This can be called with either:
109+
* - A CollectionBranding object (when available from decideCollectionBranding)
110+
* - Raw cards array (when CollectionBranding is undefined but we still need to check)
111+
*/
112+
export const shouldStripBrandingFromCards = (
113+
input: CollectionBranding | CardWithBranding[],
114+
editionId: EditionId,
115+
): boolean => {
116+
// If we have a CollectionBranding object, use it directly
117+
if (!Array.isArray(input)) {
118+
return isPaidContentSameBranding(input);
119+
}
120+
121+
const brandingsFromCards = getBrandingFromCards(input, editionId);
122+
if (!brandingsFromCards) {
123+
return false;
124+
}
125+
126+
const sameSponsor = everyCardHasSameSponsor(brandingsFromCards);
127+
128+
return sameSponsor;
129+
};
130+
104131
export const badgeFromBranding = (
105132
collectionBranding: CollectionBranding | undefined,
106133
): DCRBadgeType | undefined => {

dotcom-rendering/src/model/enhanceCollections.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { FECollection } from '../frontend/feFront';
22
import {
3+
brandingEqual,
34
decideCollectionBranding,
4-
isPaidContentSameBranding,
5+
shouldStripBrandingFromCards,
56
} from '../lib/branding';
67
import type { EditionId } from '../lib/edition';
78
import type { Branding } from '../types/branding';
@@ -77,11 +78,16 @@ export const enhanceCollections = ({
7778
const indexToShowFrontBranding =
7879
findCollectionSuitableForFrontBranding(collections);
7980

81+
// Track the branding from the first primary container to detect duplicates in subsequent primary containers
82+
let firstContainerBranding: Branding | undefined;
83+
8084
return collections.filter(isSupported).map((collection, index) => {
8185
const { id, displayName, collectionType, hasMore, href, description } =
8286
collection;
8387
const allCards = [...collection.curated, ...collection.backfill];
84-
const collectionBranding = decideCollectionBranding({
88+
89+
// First, get the raw collection branding without considering previous containers
90+
const rawCollectionBranding = decideCollectionBranding({
8591
frontBranding,
8692
couldDisplayFrontBranding: index === indexToShowFrontBranding,
8793
cards: allCards,
@@ -91,8 +97,34 @@ export const enhanceCollections = ({
9197
({ type }) => type === 'Branded',
9298
) ?? false,
9399
});
94-
const stripBrandingFromCards =
95-
isPaidContentSameBranding(collectionBranding);
100+
101+
// Determine if we should hide collection branding due to a previous container
102+
const shouldHideCollectionBranding =
103+
!!firstContainerBranding &&
104+
!!rawCollectionBranding?.branding &&
105+
brandingEqual(
106+
firstContainerBranding,
107+
rawCollectionBranding.branding,
108+
);
109+
110+
// The actual collection branding to display (hide if duplicate branding of previous container)
111+
const collectionBranding = shouldHideCollectionBranding
112+
? undefined
113+
: rawCollectionBranding;
114+
115+
// Store the branding from the first container
116+
if (!firstContainerBranding && rawCollectionBranding) {
117+
firstContainerBranding = rawCollectionBranding.branding;
118+
}
119+
120+
/** Determine if we should strip branding from cards
121+
* We need to check this directly in here because decideCollectionBranding might return undefined
122+
* when cards have same branding as frontBranding, but we still need to strip in that case
123+
*/
124+
const stripBrandingFromCards = shouldStripBrandingFromCards(
125+
rawCollectionBranding ?? allCards,
126+
editionId,
127+
);
96128

97129
const containerPalette = decideContainerPalette(
98130
collection.config.metadata?.map((meta) => meta.type),

0 commit comments

Comments
 (0)