2020-07-23 16:22:57 +02:00
|
|
|
// @flow
|
2021-10-18 17:54:59 +02:00
|
|
|
import { lazyImport } from 'util/lazyImport';
|
2021-11-05 20:31:51 +01:00
|
|
|
import { Menu, MenuList, MenuButton, MenuItem } from '@reach/menu-button';
|
|
|
|
import { NavLink } from 'react-router-dom';
|
|
|
|
import { parseURI } from 'util/lbryURI';
|
2021-07-08 04:50:51 +02:00
|
|
|
import { RULE } from 'constants/notifications';
|
2021-11-05 20:31:51 +01:00
|
|
|
import { useHistory } from 'react-router';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2020-08-31 19:12:34 +02:00
|
|
|
import Button from 'component/button';
|
2020-07-23 16:22:57 +02:00
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
2021-11-05 20:31:51 +01:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import DateTime from 'component/dateTime';
|
2020-11-02 17:51:08 +01:00
|
|
|
import FileThumbnail from 'component/fileThumbnail';
|
2021-11-05 20:31:51 +01:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import NotificationContentChannelMenu from 'component/notificationContentChannelMenu';
|
|
|
|
import React from 'react';
|
2021-08-18 23:40:36 +02:00
|
|
|
import UriIndicator from 'component/uriIndicator';
|
2022-07-05 12:55:04 +02:00
|
|
|
import { getNotificationLink, getNotificationTarget } from './helpers/target';
|
Make notification titles translatable
## Issue
Desktop 7022: i18n: Notification title localization lost
## Approach
The current code breaks up the string into an array of strings, so i18n is somewhat impossible.
Since 99%¹ of dynamic notifications come with a `dynamic` property for all the replaceable values, we can actually reconstruct the string however we want.
¹ <sub>_as far as I can find, only `fiat_tip` does not provide the value via `dynamic`, i.e. hardcoded in the string. Boo._</sub>
### Benefits of this approach:
- able to localize the string again
- able to customize the string (e.g. making claim titles italic, fix typos, use more concise strings, etc.)
### Problems with this approach:
- if the api overloads a particular notification type with several strings, then the approach is broken.
- Ex. For the case of `comment` type, the overload is whether the comment is a normal comment or hyperchat. But I was able to replicate the logic to differentiate them, so all is good.
- For the case of "livestream reminder in 30 minutes", we would have to inspect the string to differentiate against "is live streaming". I think this reminder is not being used, so I didn't do it yet.
- some work is needed to maintain the code when the server side updates something. But we are already manually maintaining the i18n strings anyway, so it shouldn't be too much of a burden.
- With the exception of the overload problem, any new notification type will simply pass through as un-localized, so things should continue to work, i.e. no need to update the front-end immediately 🤞
2022-02-14 10:04:12 +01:00
|
|
|
import { generateNotificationTitle } from './helpers/title';
|
|
|
|
import { generateNotificationText } from './helpers/text';
|
2020-07-23 16:22:57 +02:00
|
|
|
|
2021-10-18 17:54:59 +02:00
|
|
|
const CommentCreate = lazyImport(() => import('component/commentCreate' /* webpackChunkName: "comments" */));
|
|
|
|
const CommentReactions = lazyImport(() => import('component/commentReactions' /* webpackChunkName: "comments" */));
|
|
|
|
const CommentsReplies = lazyImport(() => import('component/commentsReplies' /* webpackChunkName: "comments" */));
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
type Props = {
|
|
|
|
menuButton: boolean,
|
2021-11-05 20:31:51 +01:00
|
|
|
notification: WebNotification,
|
|
|
|
deleteNotification: () => void,
|
|
|
|
readNotification: () => void,
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function Notification(props: Props) {
|
2021-11-05 20:31:51 +01:00
|
|
|
const { menuButton = false, notification, readNotification, deleteNotification } = props;
|
|
|
|
|
|
|
|
const { notification_rule, notification_parameters, is_read } = notification;
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
const { push } = useHistory();
|
2021-08-27 12:29:58 +02:00
|
|
|
const [isReplying, setReplying] = React.useState(false);
|
|
|
|
const [quickReply, setQuickReply] = React.useState();
|
2021-08-18 23:40:36 +02:00
|
|
|
|
2020-09-16 18:35:59 +02:00
|
|
|
const isCommentNotification =
|
2021-07-08 04:50:51 +02:00
|
|
|
notification_rule === RULE.COMMENT ||
|
|
|
|
notification_rule === RULE.COMMENT_REPLY ||
|
|
|
|
notification_rule === RULE.CREATOR_COMMENT;
|
2022-07-05 12:55:04 +02:00
|
|
|
|
|
|
|
const notificationTarget = getNotificationTarget(notification);
|
|
|
|
const notificationLink = getNotificationLink(notification, notificationTarget);
|
2020-09-16 18:35:59 +02:00
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
const creatorIcon = (channelUrl, channelThumbnail) => (
|
2022-02-25 21:15:54 +01:00
|
|
|
<UriIndicator uri={channelUrl} link showAtSign channelInfo={{ uri: channelUrl, name: '' }}>
|
2022-02-07 21:59:20 +01:00
|
|
|
<ChannelThumbnail small thumbnailPreview={channelThumbnail} uri={channelThumbnail ? undefined : channelUrl} />
|
2021-11-05 20:31:51 +01:00
|
|
|
</UriIndicator>
|
|
|
|
);
|
2022-02-07 21:59:20 +01:00
|
|
|
|
2021-08-18 23:40:36 +02:00
|
|
|
let channelUrl;
|
2020-07-23 16:22:57 +02:00
|
|
|
let icon;
|
2020-08-21 21:44:54 +02:00
|
|
|
switch (notification_rule) {
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.CREATOR_SUBSCRIBER:
|
2020-11-13 18:58:31 +01:00
|
|
|
icon = <Icon icon={ICONS.SUBSCRIBE} sectionIcon />;
|
2020-07-23 16:22:57 +02:00
|
|
|
break;
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.COMMENT:
|
|
|
|
case RULE.CREATOR_COMMENT:
|
2021-08-18 23:40:36 +02:00
|
|
|
channelUrl = notification_parameters.dynamic.comment_author;
|
2022-02-07 21:59:20 +01:00
|
|
|
icon = creatorIcon(channelUrl, notification_parameters?.dynamic?.comment_author_thumbnail);
|
2020-07-23 16:22:57 +02:00
|
|
|
break;
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.COMMENT_REPLY:
|
2021-08-18 23:40:36 +02:00
|
|
|
channelUrl = notification_parameters.dynamic.reply_author;
|
2022-02-07 21:59:20 +01:00
|
|
|
icon = creatorIcon(channelUrl, notification_parameters?.dynamic?.comment_author_thumbnail);
|
2020-09-08 22:56:48 +02:00
|
|
|
break;
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.NEW_CONTENT:
|
2021-08-18 23:40:36 +02:00
|
|
|
channelUrl = notification_parameters.dynamic.channel_url;
|
2022-02-07 21:59:20 +01:00
|
|
|
icon = creatorIcon(channelUrl, notification_parameters?.dynamic?.channel_thumbnail);
|
2020-11-02 17:51:08 +01:00
|
|
|
break;
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.NEW_LIVESTREAM:
|
2021-08-18 23:40:36 +02:00
|
|
|
channelUrl = notification_parameters.dynamic.channel_url;
|
2022-02-07 21:59:20 +01:00
|
|
|
icon = creatorIcon(channelUrl, notification_parameters?.dynamic?.channel_thumbnail);
|
2021-04-16 21:53:33 +02:00
|
|
|
break;
|
2022-02-11 19:50:55 +01:00
|
|
|
case RULE.WEEKLY_WATCH_REMINDER:
|
2021-07-08 04:50:51 +02:00
|
|
|
case RULE.DAILY_WATCH_AVAILABLE:
|
|
|
|
case RULE.DAILY_WATCH_REMIND:
|
|
|
|
case RULE.MISSED_OUT:
|
|
|
|
case RULE.REWARDS_APPROVAL_PROMPT:
|
2020-11-13 06:36:23 +01:00
|
|
|
icon = <Icon icon={ICONS.LBC} sectionIcon />;
|
2020-10-22 18:40:09 +02:00
|
|
|
break;
|
2021-07-08 05:19:51 +02:00
|
|
|
case RULE.FIAT_TIP:
|
|
|
|
icon = <Icon icon={ICONS.FINANCE} sectionIcon />;
|
|
|
|
break;
|
2020-07-23 16:22:57 +02:00
|
|
|
default:
|
2020-11-13 18:58:31 +01:00
|
|
|
icon = <Icon icon={ICONS.NOTIFICATION} sectionIcon />;
|
2020-07-23 16:22:57 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:28:32 +02:00
|
|
|
let channelName;
|
2021-10-17 10:36:14 +02:00
|
|
|
if (channelUrl) {
|
|
|
|
try {
|
2021-11-05 20:31:51 +01:00
|
|
|
({ claimName: channelName } = parseURI(channelUrl));
|
2021-10-17 10:36:14 +02:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
2021-08-18 23:40:36 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
const navLinkProps = { to: notificationLink, onClick: (e) => e.stopPropagation() };
|
2021-08-18 23:40:36 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
function handleNotificationClick() {
|
|
|
|
if (!is_read) readNotification();
|
|
|
|
if (menuButton && notificationLink) push(notificationLink);
|
2020-08-21 21:44:54 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
const Wrapper = menuButton
|
|
|
|
? (props: { children: any }) => (
|
2020-08-21 21:44:54 +02:00
|
|
|
<MenuItem className="menu__link--notification" onSelect={handleNotificationClick}>
|
2020-07-23 16:22:57 +02:00
|
|
|
{props.children}
|
|
|
|
</MenuItem>
|
|
|
|
)
|
2020-08-21 21:44:54 +02:00
|
|
|
: notificationLink
|
|
|
|
? (props: { children: any }) => (
|
2021-08-27 12:29:58 +02:00
|
|
|
<NavLink {...navLinkProps} className="menu__link--notification" onClick={handleNotificationClick}>
|
|
|
|
{props.children}
|
2021-08-18 23:40:36 +02:00
|
|
|
</NavLink>
|
2020-08-21 21:44:54 +02:00
|
|
|
)
|
|
|
|
: (props: { children: any }) => (
|
|
|
|
<span
|
2020-12-14 19:52:17 +01:00
|
|
|
className={is_read ? 'menu__link--notification-nolink' : 'menu__link--notification'}
|
2020-08-21 21:44:54 +02:00
|
|
|
onClick={handleNotificationClick}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</span>
|
2020-07-23 16:22:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2021-11-05 20:31:51 +01:00
|
|
|
<div className={classnames('notification__wrapper', { 'notification__wrapper--unread': !is_read })}>
|
2021-08-27 12:29:58 +02:00
|
|
|
<Wrapper>
|
2020-07-23 16:22:57 +02:00
|
|
|
<div className="notification__icon">{icon}</div>
|
2020-07-30 22:14:12 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
<div className="notificationContent__wrapper">
|
2020-11-03 19:45:36 +01:00
|
|
|
<div className="notification__content">
|
2021-11-05 20:31:51 +01:00
|
|
|
<div className="notificationText__wrapper">
|
Make notification titles translatable
## Issue
Desktop 7022: i18n: Notification title localization lost
## Approach
The current code breaks up the string into an array of strings, so i18n is somewhat impossible.
Since 99%¹ of dynamic notifications come with a `dynamic` property for all the replaceable values, we can actually reconstruct the string however we want.
¹ <sub>_as far as I can find, only `fiat_tip` does not provide the value via `dynamic`, i.e. hardcoded in the string. Boo._</sub>
### Benefits of this approach:
- able to localize the string again
- able to customize the string (e.g. making claim titles italic, fix typos, use more concise strings, etc.)
### Problems with this approach:
- if the api overloads a particular notification type with several strings, then the approach is broken.
- Ex. For the case of `comment` type, the overload is whether the comment is a normal comment or hyperchat. But I was able to replicate the logic to differentiate them, so all is good.
- For the case of "livestream reminder in 30 minutes", we would have to inspect the string to differentiate against "is live streaming". I think this reminder is not being used, so I didn't do it yet.
- some work is needed to maintain the code when the server side updates something. But we are already manually maintaining the i18n strings anyway, so it shouldn't be too much of a burden.
- With the exception of the overload problem, any new notification type will simply pass through as un-localized, so things should continue to work, i.e. no need to update the front-end immediately 🤞
2022-02-14 10:04:12 +01:00
|
|
|
<div className="notification__title">
|
|
|
|
{generateNotificationTitle(notification_rule, notification_parameters, channelName)}
|
|
|
|
</div>
|
|
|
|
{generateNotificationText(notification_rule, notification_parameters)}
|
2020-11-03 19:45:36 +01:00
|
|
|
</div>
|
|
|
|
|
2021-07-08 04:50:51 +02:00
|
|
|
{notification_rule === RULE.NEW_CONTENT && (
|
2022-02-07 21:59:20 +01:00
|
|
|
<FileThumbnail
|
|
|
|
uri={notification_parameters.device.target}
|
|
|
|
thumbnail={notification_parameters?.dynamic?.claim_thumbnail}
|
|
|
|
className="notificationContent__thumbnail"
|
|
|
|
/>
|
2021-04-16 21:53:33 +02:00
|
|
|
)}
|
2021-07-08 04:50:51 +02:00
|
|
|
{notification_rule === RULE.NEW_LIVESTREAM && (
|
2021-04-16 21:53:33 +02:00
|
|
|
<FileThumbnail
|
|
|
|
thumbnail={notification_parameters.device.image_url}
|
2021-11-05 20:31:51 +01:00
|
|
|
className="notificationContent__thumbnail"
|
2021-04-16 21:53:33 +02:00
|
|
|
/>
|
2020-08-21 21:44:54 +02:00
|
|
|
)}
|
2020-07-30 22:14:12 +02:00
|
|
|
</div>
|
|
|
|
|
2020-08-31 19:12:34 +02:00
|
|
|
<div className="notification__extra">
|
2021-08-27 12:29:58 +02:00
|
|
|
{!is_read && (
|
|
|
|
<Button
|
2021-11-05 20:31:51 +01:00
|
|
|
className="notification__markSeen"
|
2021-08-27 12:29:58 +02:00
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
2021-11-05 20:31:51 +01:00
|
|
|
readNotification();
|
2021-08-27 12:29:58 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2020-08-31 19:12:34 +02:00
|
|
|
<div className="notification__time">
|
2020-09-15 15:54:05 +02:00
|
|
|
<DateTime timeAgo date={notification.active_at} />
|
2020-08-31 19:12:34 +02:00
|
|
|
</div>
|
2020-07-23 16:22:57 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-11-03 21:09:56 +01:00
|
|
|
|
|
|
|
<div className="notification__menu">
|
|
|
|
<Menu>
|
2021-08-18 23:40:36 +02:00
|
|
|
<MenuButton
|
2021-11-05 20:31:51 +01:00
|
|
|
className="menu__button notification__menuButton"
|
2021-08-18 23:40:36 +02:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}}
|
|
|
|
>
|
2020-11-03 21:09:56 +01:00
|
|
|
<Icon size={18} icon={ICONS.MORE_VERTICAL} />
|
|
|
|
</MenuButton>
|
2021-03-03 19:50:16 +01:00
|
|
|
<MenuList className="menu__list">
|
2021-11-05 20:31:51 +01:00
|
|
|
<MenuItem className="menu__link" onSelect={() => deleteNotification()}>
|
2020-11-03 21:09:56 +01:00
|
|
|
<Icon aria-hidden icon={ICONS.DELETE} />
|
|
|
|
{__('Delete')}
|
|
|
|
</MenuItem>
|
2021-07-08 04:50:51 +02:00
|
|
|
{notification_rule === RULE.NEW_CONTENT && channelUrl ? (
|
2020-11-03 21:09:56 +01:00
|
|
|
<NotificationContentChannelMenu uri={channelUrl} />
|
|
|
|
) : null}
|
|
|
|
</MenuList>
|
|
|
|
</Menu>
|
|
|
|
</div>
|
2021-08-27 12:29:58 +02:00
|
|
|
</Wrapper>
|
|
|
|
|
|
|
|
{isCommentNotification && (
|
|
|
|
<div>
|
|
|
|
<div className="notification__reactions">
|
|
|
|
<Button
|
|
|
|
label={__('Reply')}
|
|
|
|
className="comment__action"
|
|
|
|
onClick={() => setReplying(!isReplying)}
|
|
|
|
icon={ICONS.REPLY}
|
|
|
|
/>
|
|
|
|
<CommentReactions
|
|
|
|
uri={notificationTarget}
|
|
|
|
commentId={notification_parameters.dynamic.hash}
|
|
|
|
hideCreatorLike
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{isReplying && (
|
2021-10-18 17:54:59 +02:00
|
|
|
<React.Suspense fallback={null}>
|
|
|
|
<CommentCreate
|
|
|
|
isReply
|
|
|
|
uri={notificationTarget}
|
|
|
|
parentId={notification_parameters.dynamic.hash}
|
|
|
|
onDoneReplying={() => setReplying(false)}
|
|
|
|
onCancelReplying={() => setReplying(false)}
|
|
|
|
setQuickReply={setQuickReply}
|
|
|
|
supportDisabled
|
|
|
|
shouldFetchComment
|
|
|
|
/>
|
|
|
|
</React.Suspense>
|
2021-08-27 12:29:58 +02:00
|
|
|
)}
|
|
|
|
|
|
|
|
{quickReply && (
|
|
|
|
<CommentsReplies
|
|
|
|
uri={notificationTarget}
|
|
|
|
parentId={notification_parameters.dynamic.hash}
|
|
|
|
numDirectReplies={1}
|
|
|
|
supportDisabled
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-07-23 16:22:57 +02:00
|
|
|
);
|
|
|
|
}
|