-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhyDoThisTooltip.tsx
More file actions
75 lines (63 loc) · 1.87 KB
/
Copy pathWhyDoThisTooltip.tsx
File metadata and controls
75 lines (63 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { FC } from 'react';
import { sanitizeHtml } from '@lib/sanitizeHtml';
import Heading from '@components/Heading';
import { WhyDoThisTooltipProps } from './WhyDoThisTooltip.types';
import styles from './WhyDoThisTooltip.module.scss';
const WhyDoThisTooltip: FC<WhyDoThisTooltipProps> = ({
whatDamagesText,
headline,
locale,
}) => {
// TODO: This is very temporary workaround for strapi data
const looksLikeHtml = (s: string) => /<\/?[a-z][\s\S]*>/i.test(s);
const escapeHtml = (s: string) =>
s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
const textToHtml = (input: string) => {
let s = escapeHtml(input);
s = s.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
const paragraphs = s
.split(/\n\s*\n/)
.map(p => p.trim())
.filter(Boolean);
return paragraphs.map(p => `<p>${p.replace(/\n/g, '<br />')}</p>`).join('');
};
function RichText({ value }: { value?: string | null }) {
if (!value) return null;
const html = looksLikeHtml(value) ? value : textToHtml(value);
return (
<div
dangerouslySetInnerHTML={{ __html: sanitizeHtml(html) }}
className={styles.content}
/>
);
}
return (
<div className={styles.whyDoThisTooltip} data-cy="why-do-this-content">
<div>
<video
src="/assets/longevity/habits/brain-vid.mp4"
autoPlay
muted
playsInline
className={styles.video}
/>
{headline && (
<Heading
text={headline ? headline : ''}
Tag={'h4'}
showLeftIcon={false}
showRightIcon={false}
className={styles.heading}
/>
)}
<RichText value={whatDamagesText} />
</div>
</div>
);
};
export default WhyDoThisTooltip;