Skip to content

Commit 70ec301

Browse files
committed
feat: add ParagraphWithLinks component for rendering links in changelog
1 parent f64e90b commit 70ec301

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

apps/extension/src/pages/main/components/update-note-modal/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { InExtensionMessageRequester } from "@keplr-wallet/router-extension";
2121
import { BACKGROUND_PORT } from "@keplr-wallet/router";
2222
import { observer } from "mobx-react-lite";
2323
import { useStore } from "../../../../stores";
24+
import { ParagraphWithLinks } from "./paragraph-with-links";
2425

2526
export type UpdateNotePageData = {
2627
title: string;
@@ -32,6 +33,9 @@ export type UpdateNotePageData = {
3233
}
3334
| undefined;
3435
paragraph: string;
36+
links?: {
37+
[key: string]: string;
38+
};
3539
isSidePanelBeta?: boolean;
3640
};
3741

@@ -271,13 +275,9 @@ const CarouselPage: FunctionComponent<{
271275
: ColorPalette["gray-100"]
272276
}
273277
>
274-
<FormattedMessage
275-
id="update-node/paragraph/noop"
276-
defaultMessage={notePageData.paragraph}
277-
values={{
278-
br: <br />,
279-
b: (...chunks: any) => <b>{chunks}</b>,
280-
}}
278+
<ParagraphWithLinks
279+
paragraph={notePageData.paragraph}
280+
links={notePageData.links}
281281
/>
282282
</Body2>
283283
</Box>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React, { FunctionComponent } from "react";
2+
import { FormattedMessage } from "react-intl";
3+
import styled from "styled-components";
4+
import { ColorPalette } from "../../../../styles";
5+
import { UpdateNotePageData } from ".";
6+
7+
const Styles = {
8+
Link: styled.span`
9+
color: ${(props) =>
10+
props.theme.mode === "light"
11+
? ColorPalette["gray-600"]
12+
: ColorPalette["gray-50"]};
13+
14+
cursor: pointer;
15+
text-decoration: underline;
16+
`,
17+
};
18+
19+
export const ParagraphWithLinks: FunctionComponent<{
20+
paragraph: string;
21+
links?: UpdateNotePageData["links"];
22+
}> = ({ paragraph, links }) => {
23+
return (
24+
<FormattedMessage
25+
id="update-node/paragraph/noop"
26+
defaultMessage={paragraph}
27+
values={{
28+
br: <br />,
29+
b: (...chunks: React.ReactNode[]) => <b>{chunks}</b>,
30+
link: links
31+
? (...chunks: React.ReactNode[]) => {
32+
const flattenedChunks: React.ReactNode[] = [];
33+
34+
for (const chunk of chunks) {
35+
if (Array.isArray(chunk)) {
36+
flattenedChunks.push(...chunk);
37+
} else {
38+
flattenedChunks.push(chunk);
39+
}
40+
}
41+
42+
const linkText = flattenedChunks
43+
.filter(
44+
(chunk): chunk is string | number =>
45+
typeof chunk === "string" || typeof chunk === "number"
46+
)
47+
.map((chunk) => String(chunk))
48+
.join("")
49+
.trim();
50+
51+
const url = links[linkText];
52+
53+
if (url) {
54+
return (
55+
<Styles.Link
56+
onClick={(e) => {
57+
e.preventDefault();
58+
59+
browser.tabs.create({ url });
60+
}}
61+
>
62+
{chunks}
63+
</Styles.Link>
64+
);
65+
}
66+
67+
return <React.Fragment>{chunks}</React.Fragment>;
68+
}
69+
: undefined,
70+
}}
71+
/>
72+
);
73+
};

0 commit comments

Comments
 (0)