Skip to content

Commit 2d579fc

Browse files
authored
Merge pull request #2002 from Sandeep-FED/dev-feat-listitemcomments
feat[ListItemComments]: make links present in comments clickable
2 parents 8f932d8 + aed3c92 commit 2d579fc

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const PHOTO_URL = "/_layouts/15/userphoto.aspx?size=M&accountname=";
22
export const TILE_HEIGHT: number = 70;
3+
export const URL_REGEX = /(https?:\/\/[^\s]+)/g;

src/controls/listItemComments/components/Comments/CommentText.tsx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { useContext, useEffect, useState } from "react";
33
import { Mention } from "./IComment";
44
import { Text } from "@fluentui/react/lib/Text";
55
import { LivePersona } from "../../../LivePersona";
6-
import { AppContext } from "../../common";
7-
import regexifyString from "regexify-string";
8-
import { Stack } from "@fluentui/react/lib/Stack";
9-
import { isArray, isObject } from "lodash";
6+
import { AppContext, URL_REGEX } from '../../common';
7+
import regexifyString from 'regexify-string';
8+
import { Stack } from '@fluentui/react/lib/Stack';
9+
import { isArray, isObject } from 'lodash';
1010
import he from 'he';
11+
import { Link } from '@fluentui/react';
1112
export interface ICommentTextProps {
1213
text: string;
1314
mentions: Mention[];
@@ -46,26 +47,55 @@ export const CommentText: React.FunctionComponent<ICommentTextProps> = (
4647
setCommentText(result);
4748
}, []);
4849

50+
const convertTextToLinksAndText = (
51+
text: string
52+
): (string | JSX.Element)[] => {
53+
const parts = text.split(URL_REGEX);
54+
return parts.map((part, index) => {
55+
if (part.match(URL_REGEX)) {
56+
return (
57+
<Link
58+
key={index}
59+
href={part}
60+
target="_blank"
61+
rel="noopener noreferrer"
62+
style={{ color: theme.link }}
63+
>
64+
{part}
65+
</Link>
66+
);
67+
}
68+
return part;
69+
});
70+
};
71+
4972
return (
5073
<>
5174
<Stack wrap horizontal horizontalAlign="start" verticalAlign="center">
5275
{isArray(commentText) ? (
53-
(commentText as any[]).map((el, i) => { // eslint-disable-line @typescript-eslint/no-explicit-any
76+
(commentText as any[]).map((el, i) => {
77+
// eslint-disable-line @typescript-eslint/no-explicit-any
5478
if (isObject(el)) {
55-
return <span key={i} style={{ paddingRight: 5 }}>{el}</span>;
79+
return (
80+
<span key={i} style={{ paddingRight: 5 }}>
81+
{el}
82+
</span>
83+
);
5684
} else {
5785
const _el: string = el.trim();
5886
if (_el.length) {
5987
return (
6088
<Text style={{ paddingRight: 5 }} variant="small" key={i}>
61-
{he.decode(_el)}
89+
{convertTextToLinksAndText(he.decode(_el))}
6290
</Text>
6391
);
6492
}
6593
}
6694
})
6795
) : (
68-
<Text variant="small">{he.decode(commentText)}</Text>
96+
<Text variant="small">
97+
{convertTextToLinksAndText(he.decode(commentText))}
98+
</Text>
6999
)}
70100
</Stack>
71101
</>

0 commit comments

Comments
 (0)