-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
index.tsx
166 lines (144 loc) · 6.11 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {useIntl} from 'react-intl';
import type {FormatDateOptions} from 'react-intl';
import {useSelector} from 'react-redux';
import {EyeOffOutlineIcon} from '@mattermost/compass-icons/components';
import type {GlobalState} from '@mattermost/types/store';
import {getOldestPostTimeInChannel} from 'mattermost-redux/selectors/entities/posts';
import {getCurrentTeam} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';
import {isAdmin} from 'mattermost-redux/utils/user_utils';
import useGetLimits from 'components/common/hooks/useGetLimits';
import {NotifyStatus} from 'components/common/hooks/useGetNotifyAdmin';
import useOpenPricingModal from 'components/common/hooks/useOpenPricingModal';
import {useNotifyAdmin} from 'components/notify_admin_cta/notify_admin_cta';
import {LicenseSkus, MattermostFeatures} from 'utils/constants';
import './index.scss';
const ONE_DAY_MS = 1000 * 60 * 60 * 24;
const ONE_YEAR_MS = ONE_DAY_MS * 365;
interface Props {
channelId?: string;
firstInaccessiblePostTime?: number;
}
// returns the same time on the next day.
function getNextDay(timestamp?: number): number {
if (timestamp === undefined) {
return 0;
}
return timestamp + ONE_DAY_MS;
}
export default function CenterMessageLock(props: Props) {
const intl = useIntl();
const openPricingModal = useOpenPricingModal();
const isAdminUser = isAdmin(useSelector(getCurrentUser).roles);
const [cloudLimits, limitsLoaded] = useGetLimits();
const currentTeam = useSelector(getCurrentTeam);
// firstInaccessiblePostTime is the most recently inaccessible post's created at date.
// It is used as a backup for when there are no available posts in the channel;
// The message then shows that the user can retrieve messages prior to the day
// **after** the most recent day with inaccessible posts.
const oldestPostTime = useSelector((state: GlobalState) => getOldestPostTimeInChannel(state, props.channelId || '')) || getNextDay(props.firstInaccessiblePostTime);
const [notifyAdminBtnText, notifyAdmin, notifyRequestStatus] = useNotifyAdmin({
ctaText: intl.formatMessage({
id: 'workspace_limits.message_history.locked.cta.end_user',
defaultMessage: 'Notify Admin',
}),
}, {
required_feature: MattermostFeatures.UNLIMITED_MESSAGES,
required_plan: LicenseSkus.Professional,
trial_notification: false,
});
if (!limitsLoaded) {
return null;
}
const dateFormat: FormatDateOptions = {month: 'long', day: 'numeric'};
if (Date.now() - oldestPostTime >= ONE_YEAR_MS) {
dateFormat.year = 'numeric';
}
const titleValues = {
date: intl.formatDate(oldestPostTime, dateFormat),
team: currentTeam?.display_name,
};
const limit = intl.formatNumber(cloudLimits?.messages?.history || 0);
let title = intl.formatMessage(
{
id: 'workspace_limits.message_history.locked.title.end_user',
defaultMessage: 'Notify your admin to unlock messages prior to {date} in {team}',
},
titleValues,
);
let description: React.ReactNode = intl.formatMessage(
{
id: 'workspace_limits.message_history.locked.description.end_user',
defaultMessage: 'Some older messages may not be shown because your workspace has over {limit} messages. Select Notify Admin to send an automatic request to your System Admins to upgrade.',
},
{
limit,
},
);
let cta = (
<button
className='btn btn-primary'
onClick={(e) => notifyAdmin(e, 'center_channel_posts_over_limit_banner')}
disabled={notifyRequestStatus === NotifyStatus.AlreadyComplete}
>
{notifyAdminBtnText}
</button>);
if (isAdminUser) {
title = intl.formatMessage({
id: 'workspace_limits.message_history.locked.title.admin',
defaultMessage: 'Unlock messages prior to {date} in {team}',
}, titleValues);
description = intl.formatMessage(
{
id: 'workspace_limits.message_history.locked.description.admin',
defaultMessage: 'To view and search all of the messages in your workspace’s history, rather than just the most recent {limit} messages, upgrade to one of our paid plans. <a>Review our plan options and pricing.</a>',
},
{
limit,
a: (chunks: React.ReactNode | React.ReactNodeArray) => (
<a
href='#'
onClick={(e: React.MouseEvent) => {
e.preventDefault();
openPricingModal({trackingLocation: 'center_channel_posts_over_limit_banner'});
}}
>
{chunks}
</a>
),
},
);
cta = (
<button
className='btn is-admin'
onClick={() => openPricingModal({trackingLocation: 'center_channel_posts_over_limit_banner'})}
>
{
intl.formatMessage({
id: 'workspace_limits.message_history.locked.cta.admin',
defaultMessage: 'Upgrade now',
})
}
</button>
);
}
return (<div className='CenterMessageLock'>
<div className='CenterMessageLock__left'>
<EyeOffOutlineIcon color={'rgba(var(--center-channel-color-rgb), 0.75)'}/>
</div>
<div className='CenterMessageLock__right'>
<div className='CenterMessageLock__title'>
{title}
</div>
<div className='CenterMessageLock__description'>
{description}
</div>
<div className='CenterMessageLock__cta'>
{cta}
</div>
</div>
</div>);
}