signal-desktop/ts/components/conversation/GroupDescription.tsx

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-06-02 00:24:28 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useEffect, useRef, useState } from 'react';
2021-06-02 00:24:28 +00:00
import { Modal } from '../Modal';
import type { LocalizerType } from '../../types/Util';
2021-06-17 17:15:51 +00:00
import { GroupDescriptionText } from '../GroupDescriptionText';
// Emojification can cause the scroll height to be *slightly* larger than the client
// height, so we add a little wiggle room.
const SHOW_READ_MORE_THRESHOLD = 5;
2021-06-02 00:24:28 +00:00
export type PropsType = {
i18n: LocalizerType;
title: string;
text: string;
};
2022-11-18 00:45:19 +00:00
export function GroupDescription({
2021-06-02 00:24:28 +00:00
i18n,
title,
text,
2022-11-18 00:45:19 +00:00
}: PropsType): JSX.Element {
2021-06-02 00:24:28 +00:00
const textRef = useRef<HTMLDivElement | null>(null);
const [hasReadMore, setHasReadMore] = useState(false);
const [showFullDescription, setShowFullDescription] = useState(false);
useEffect(() => {
2021-06-02 00:24:28 +00:00
if (!textRef || !textRef.current) {
return;
}
2021-06-17 17:15:51 +00:00
setHasReadMore(
textRef.current.scrollHeight - SHOW_READ_MORE_THRESHOLD >
textRef.current.clientHeight
);
}, [setHasReadMore, text, textRef]);
2021-06-02 00:24:28 +00:00
return (
<>
{showFullDescription && (
<Modal
2022-09-27 20:24:21 +00:00
modalName="GroupDescription"
2021-06-02 00:24:28 +00:00
hasXButton
i18n={i18n}
onClose={() => setShowFullDescription(false)}
title={title}
>
2021-06-17 17:15:51 +00:00
<GroupDescriptionText text={text} />
2021-06-02 00:24:28 +00:00
</Modal>
)}
<div className="GroupDescription__text" ref={textRef}>
2021-06-17 17:15:51 +00:00
<GroupDescriptionText text={text} />
2021-06-02 00:24:28 +00:00
</div>
{hasReadMore && (
<button
className="GroupDescription__read-more"
onClick={ev => {
ev.preventDefault();
ev.stopPropagation();
setShowFullDescription(true);
}}
type="button"
>
2023-03-30 00:03:25 +00:00
{i18n('icu:GroupDescription__read-more')}
2021-06-02 00:24:28 +00:00
</button>
)}
</>
);
2022-11-18 00:45:19 +00:00
}