Skip to content

Commit

Permalink
fix: UP-6890 - Redirect to user page on mention (#3)
Browse files Browse the repository at this point in the history
* fix: UP-6890 - Redirect to user page on mention

* fix: use memoized functional comps
  • Loading branch information
kaungmyatlwin authored Feb 11, 2022
1 parent 6e01b59 commit 63630e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/social/components/Comment/CommentText.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import React, { useState } from 'react';
import React, { useState, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import Truncate from 'react-truncate-markup';
import Highlighter from 'react-highlight-words';
import { findChunks } from '~/helpers/utils';
import Linkify from '~/core/components/Linkify';
import { useNavigation } from '~/social/providers/NavigationProvider';
import { CommentContent, ReadMoreButton, Highlighted } from './styles';

const COMMENT_MAX_LINES = 8;

const CommentText = ({ text, className, mentionees, maxLines = COMMENT_MAX_LINES }) => {
const { onClickUser } = useNavigation();
const [isExpanded, setIsExpanded] = useState(false);
const expand = () => setIsExpanded(true);

const highlightTag = useCallback(
({ children: toHighLight, highlightIndex }) => (
<Highlighted onClick={() => onClickUser(mentionees[highlightIndex]?.userId)}>
{toHighLight}
</Highlighted>
),
[mentionees, onClickUser],
);

const textContent = text && (
<CommentContent className={className}>
<Truncate.Atom>
<Highlighter
autoEscape
highlightTag={({ children: toHighLight }) => <Highlighted>{toHighLight}</Highlighted>}
highlightTag={highlightTag}
findChunks={() => findChunks(mentionees)}
textToHighlight={text}
/>
Expand Down
1 change: 1 addition & 0 deletions src/social/components/Comment/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,6 @@ export const CommentEditTextarea = styled(InputText).attrs({ rows: 1, maxRows: 1
`;

export const Highlighted = styled.span`
cursor: pointer;
color: ${({ theme }) => theme.palette.primary.main};
`;
19 changes: 16 additions & 3 deletions src/social/components/post/TextContent/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { FormattedMessage } from 'react-intl';
Expand All @@ -9,6 +9,7 @@ import customizableComponent from '~/core/hocs/customization';
import Linkify from '~/core/components/Linkify';
import Button from '~/core/components/Button';
import { findChunks } from '~/helpers/utils';
import { useNavigation } from '~/social/providers/NavigationProvider';

export const PostContent = styled.div`
overflow-wrap: break-word;
Expand All @@ -24,16 +25,28 @@ export const ReadMoreButton = styled(Button).attrs({ variant: 'secondary' })`
`;

export const Highlighted = styled.span`
cursor: pointer;
color: ${({ theme }) => theme.palette.primary.main};
`;

const TextContent = ({ text, postMaxLines, mentionees }) => {
const { onClickUser } = useNavigation();

const highlightTag = useCallback(
({ children, highlightIndex }) => (
<Highlighted onClick={() => onClickUser(mentionees[highlightIndex]?.userId)}>
{children}
</Highlighted>
),
[mentionees, onClickUser],
);

const textContent = text && (
<PostContent>
<Truncate.Atom>
<Highlighter
autoEscape
highlightTag={({ children }) => <Highlighted>{children}</Highlighted>}
highlightTag={highlightTag}
findChunks={() => findChunks(mentionees)}
textToHighlight={text}
/>
Expand Down Expand Up @@ -67,7 +80,7 @@ const TextContent = ({ text, postMaxLines, mentionees }) => {
};

TextContent.propTypes = {
text: PropTypes.node,
text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
postMaxLines: PropTypes.number,
mentionees: PropTypes.arrayOf(
PropTypes.shape({
Expand Down

0 comments on commit 63630e2

Please sign in to comment.