|
1 | 1 | import { |
2 | 2 | IntendedAudienceSignifier, |
3 | | - type IntendedAudienceSignifierProps, |
4 | | -} from "@guardian/stand/intendedAudienceSignifier"; |
| 3 | + mapTagsToSourceAndTarget |
| 4 | +} from "@guardian/stand/IntendedAudienceSignifier"; |
5 | 5 | import React from "react"; |
6 | | -import { isAudienceTagSlug } from "../lib/model/intended-audience"; |
7 | 6 |
|
8 | 7 | type Props = { |
9 | 8 | intendedAudience?: string; |
10 | | - productionOffice?: string; |
11 | 9 | }; |
12 | 10 |
|
13 | | -const parseProductionOfficeToSource = ( |
14 | | - source?: string, |
15 | | -): IntendedAudienceSignifierProps["source"] => { |
16 | | - if (!source) { |
17 | | - return "UK"; |
| 11 | +const getSourceAndTarget = (intendedAudience?: string) => { |
| 12 | + if (!intendedAudience) { |
| 13 | + return undefined; |
18 | 14 | } |
19 | | - switch (source?.toUpperCase()) { |
20 | | - case "US": |
21 | | - return "US"; |
22 | | - case "AU": |
23 | | - case "AUS": |
24 | | - return "AUS"; |
25 | | - case "UK": |
26 | | - default: |
27 | | - return "UK"; |
28 | | - } |
29 | | -}; |
30 | 15 |
|
31 | | -const slugsToSource = ( |
32 | | - audienceTagTokens: string[], |
33 | | -): IntendedAudienceSignifierProps["source"] => { |
34 | | - if (audienceTagTokens.includes("au")) { |
35 | | - return "AUS"; |
36 | | - } |
37 | | - if (audienceTagTokens.includes("us")) { |
38 | | - return "US"; |
39 | | - } |
40 | | - return "UK"; |
41 | | -}; |
| 16 | + // We intend to deprecate the stub.externalData.intendedAudience (Option[String], comma separated tag slugs) and use |
| 17 | + // stub.externalData.trackingTags (Option[List[String]], array of tag paths) |
42 | 18 |
|
43 | | -const slugsToIntendedAudience = ( |
44 | | - audienceTagTokens: string[], |
45 | | -): IntendedAudienceSignifierProps["intendedAudience"] => { |
46 | | - if (audienceTagTokens.length === 0) { |
47 | | - return "Don't know"; |
48 | | - } |
49 | | - if (audienceTagTokens.includes("global")) { |
50 | | - return audienceTagTokens.length === 1 ? "Global" : "Domestic For Global"; |
51 | | - } |
52 | | - return "Domestic for Domestic"; |
53 | | -}; |
54 | | - |
55 | | -const deriveProps = ( |
56 | | - stubIntendedAudience: string | undefined, |
57 | | - productionOffice: string | undefined, |
58 | | -): { |
59 | | - source: IntendedAudienceSignifierProps["source"]; |
60 | | - intendedAudience: IntendedAudienceSignifierProps["intendedAudience"]; |
61 | | -} => { |
62 | | - if (!stubIntendedAudience) { |
63 | | - return { |
64 | | - source: parseProductionOfficeToSource(productionOffice), |
65 | | - intendedAudience: "Don't know", |
66 | | - }; |
67 | | - } |
68 | | - |
69 | | - const audienceTagSlugs = stubIntendedAudience |
70 | | - .split(",") |
71 | | - .filter(isAudienceTagSlug); |
72 | | - |
73 | | - return { |
74 | | - source: slugsToSource(audienceTagSlugs), |
75 | | - intendedAudience: slugsToIntendedAudience(audienceTagSlugs), |
76 | | - }; |
| 19 | + return mapTagsToSourceAndTarget( |
| 20 | + intendedAudience |
| 21 | + .split(",") |
| 22 | + .map((slug) => ({ path: `tracking/audience/${slug}` })), |
| 23 | + ); |
77 | 24 | }; |
78 | 25 |
|
79 | 26 | export const IntendedAudienceWrapper: React.FunctionComponent<Props> = ({ |
80 | | - intendedAudience: stubIntendedAudience, |
81 | | - productionOffice, |
| 27 | + intendedAudience, |
82 | 28 | }: Props) => { |
83 | | - const { intendedAudience, source } = deriveProps( |
84 | | - stubIntendedAudience, |
85 | | - productionOffice, |
86 | | - ); |
| 29 | + const sourceAndTarget = getSourceAndTarget(intendedAudience); |
| 30 | + if (!sourceAndTarget) { |
| 31 | + return null; // do not render the "Don't know" signifier in the table |
| 32 | + } |
87 | 33 |
|
88 | 34 | return ( |
89 | 35 | <IntendedAudienceSignifier |
90 | | - intendedAudience={intendedAudience} |
91 | | - source={source} |
92 | | - theme={{ |
93 | | - svg: { |
94 | | - width: "16px", |
95 | | - height: "12px", |
96 | | - }, |
97 | | - typography: { |
98 | | - font: "normal 460 12px GuardianAgateSans1Web", |
99 | | - }, |
100 | | - }} |
| 36 | + target={sourceAndTarget.target} |
| 37 | + source={sourceAndTarget.source} |
101 | 38 | /> |
102 | 39 | ); |
103 | 40 | }; |
0 commit comments