2022-11-10 11:28:34 +01:00
|
|
|
import React, { useContext, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import { I18nContext } from '../../../../contexts/i18n';
|
|
|
|
import Box from '../../../ui/box';
|
|
|
|
import Typography from '../../../ui/typography';
|
|
|
|
import {
|
|
|
|
DISPLAY,
|
|
|
|
FONT_WEIGHT,
|
|
|
|
FLEX_DIRECTION,
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
|
|
|
JustifyContent,
|
|
|
|
Color,
|
|
|
|
BackgroundColor,
|
|
|
|
BorderColor,
|
|
|
|
BorderRadius,
|
|
|
|
TextColor,
|
2022-11-10 11:28:34 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
|
|
|
import SignatureRequestData from '../signature-request-data';
|
|
|
|
|
|
|
|
export default function SignatureRequestMessage({
|
|
|
|
data,
|
|
|
|
onMessageScrolled,
|
|
|
|
setMessageRootRef,
|
|
|
|
messageRootRef,
|
|
|
|
messageIsScrollable,
|
2023-01-12 15:52:02 +01:00
|
|
|
primaryType,
|
2022-11-10 11:28:34 +01:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const [messageIsScrolled, setMessageIsScrolled] = useState(false);
|
|
|
|
const setMessageIsScrolledAtBottom = () => {
|
|
|
|
if (!messageRootRef || messageIsScrolled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = messageRootRef;
|
|
|
|
const isAtBottom = Math.round(scrollTop) + offsetHeight >= scrollHeight;
|
|
|
|
|
|
|
|
if (isAtBottom) {
|
|
|
|
setMessageIsScrolled(true);
|
|
|
|
onMessageScrolled();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
onScroll={debounce(setMessageIsScrolledAtBottom, 25)}
|
|
|
|
className="signature-request-message"
|
|
|
|
>
|
|
|
|
{messageIsScrollable ? (
|
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
borderColor={BorderColor.borderDefault}
|
|
|
|
backgroundColor={BackgroundColor.backgroundDefault}
|
|
|
|
color={Color.iconDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
onClick={() => {
|
|
|
|
setMessageIsScrolled(true);
|
|
|
|
onMessageScrolled();
|
|
|
|
messageRootRef?.scrollTo(0, messageRootRef?.scrollHeight);
|
|
|
|
}}
|
|
|
|
className="signature-request-message__scroll-button"
|
|
|
|
data-testid="signature-request-scroll-button"
|
|
|
|
>
|
|
|
|
<i className="fa fa-arrow-down" aria-label={t('scrollDown')} />
|
|
|
|
</Box>
|
|
|
|
) : null}
|
|
|
|
<Box
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.backgroundDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
paddingBottom={3}
|
|
|
|
paddingTop={3}
|
|
|
|
paddingRight={3}
|
|
|
|
margin={2}
|
2023-02-02 21:15:26 +01:00
|
|
|
borderRadius={BorderRadius.XL}
|
|
|
|
borderColor={BorderColor.borderMuted}
|
2022-11-10 11:28:34 +01:00
|
|
|
className="signature-request-message__root"
|
|
|
|
ref={setMessageRootRef}
|
|
|
|
>
|
|
|
|
<Typography
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
2022-11-10 11:28:34 +01:00
|
|
|
marginLeft={4}
|
|
|
|
className="signature-request-message__title"
|
|
|
|
>
|
2023-01-12 15:52:02 +01:00
|
|
|
{primaryType}
|
2022-11-10 11:28:34 +01:00
|
|
|
</Typography>
|
2023-02-27 17:32:51 +01:00
|
|
|
<SignatureRequestData data={data.value} />
|
2022-11-10 11:28:34 +01:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SignatureRequestMessage.propTypes = {
|
|
|
|
data: PropTypes.object.isRequired,
|
|
|
|
onMessageScrolled: PropTypes.func,
|
|
|
|
setMessageRootRef: PropTypes.func,
|
|
|
|
messageRootRef: PropTypes.object,
|
|
|
|
messageIsScrollable: PropTypes.bool,
|
2023-01-12 15:52:02 +01:00
|
|
|
primaryType: PropTypes.string,
|
2022-11-10 11:28:34 +01:00
|
|
|
};
|