ente/src/components/Sidebar.tsx

258 lines
9.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { slide as Menu } from 'react-burger-menu';
import { CONFIRM_ACTION } from 'components/ConfirmDialog';
import Spinner from 'react-bootstrap/Spinner';
import billingService, { Subscription } from 'services/billingService';
import constants from 'utils/strings/constants';
2021-03-12 04:50:58 +00:00
import { getData, LS_KEYS } from 'utils/storage/localStorage';
import { getToken } from 'utils/common/key';
2021-03-14 07:41:50 +00:00
import { getEndpoint } from 'utils/common/apiUtil';
import { Button } from 'react-bootstrap';
2021-03-17 07:27:26 +00:00
import {
isSubscriptionActive,
2021-03-17 07:27:26 +00:00
convertBytesToGBs,
getUserSubscription,
2021-03-17 10:44:29 +00:00
isOnFreePlan,
2021-03-18 15:26:43 +00:00
isSubscriptionCancelled,
isSubscribed,
2021-03-17 07:27:26 +00:00
} from 'utils/billingUtil';
2021-03-27 13:52:53 +00:00
import exportService from 'services/exportService';
import { file } from 'services/fileService';
import isElectron from 'is-electron';
import { collection } from 'services/collectionService';
2021-04-05 05:12:37 +00:00
import { useRouter } from 'next/router';
2021-04-05 07:41:48 +00:00
import RecoveryKeyModal from './RecoveryKeyModal';
import { justSignedUp } from 'utils/storage';
2021-03-12 04:50:58 +00:00
interface Props {
2021-03-27 13:52:53 +00:00
files: file[];
collections: collection[];
setConfirmAction: any;
somethingWentWrong: any;
setPlanModalView;
setBannerMessage;
2021-03-12 04:50:58 +00:00
}
export default function Sidebar(props: Props) {
const [usage, SetUsage] = useState<string>(null);
2021-03-17 07:27:26 +00:00
const [user, setUser] = useState(null);
const [subscription, setSubscription] = useState<Subscription>(null);
useEffect(() => {
setUser(getData(LS_KEYS.USER));
setSubscription(getUserSubscription());
}, []);
const [isOpen, setIsOpen] = useState(false);
const [modalView, setModalView] = useState(justSignedUp());
useEffect(() => {
const main = async () => {
if (!isOpen) {
return;
}
const usage = await billingService.getUsage();
SetUsage(usage);
2021-03-17 07:27:26 +00:00
setSubscription(getUserSubscription());
};
main();
}, [isOpen]);
2021-03-14 07:41:50 +00:00
function openFeedbackURL() {
2021-03-16 04:40:27 +00:00
const feedbackURL: string =
getEndpoint() + '/users/feedback?token=' + getToken();
2021-03-14 07:41:50 +00:00
var win = window.open(feedbackURL, '_blank');
win.focus();
}
2021-03-29 05:15:01 +00:00
function exportFiles() {
if (isElectron()) {
exportService.exportFiles(props.files, props.collections);
2021-03-29 05:15:01 +00:00
} else {
props.setConfirmAction(CONFIRM_ACTION.DOWNLOAD_APP);
2021-03-29 05:15:01 +00:00
}
}
2021-04-08 05:08:15 +00:00
async function onManageClick(event) {
try {
event.preventDefault();
await billingService.redirectToCustomerPortal();
} catch (error) {
props.somethingWentWrong();
}
}
2021-04-05 05:12:37 +00:00
const router = useRouter();
2021-03-14 07:41:50 +00:00
return (
<Menu
isOpen={isOpen}
onStateChange={(state) => setIsOpen(state.isOpen)}
itemListElement="div"
>
<div style={{ marginBottom: '12px', outline: 'none' }}>
Hi {user?.email} !!
</div>
<div style={{ outline: 'none' }}>
2021-03-16 04:40:27 +00:00
<h5 style={{ marginBottom: '12px' }}>
{constants.SUBSCRIPTION_PLAN}
</h5>
2021-03-14 07:19:41 +00:00
<div style={{ color: '#959595' }}>
{isSubscriptionActive(subscription) ? (
2021-03-17 10:44:29 +00:00
isOnFreePlan(subscription) ? (
constants.FREE_SUBSCRIPTION_INFO(
subscription?.expiryTime
)
2021-03-18 15:26:43 +00:00
) : isSubscriptionCancelled(subscription) ? (
constants.RENEWAL_CANCELLED_SUBSCRIPTION_INFO(
subscription?.expiryTime
)
) : (
constants.RENEWAL_ACTIVE_SUBSCRIPTION_INFO(
subscription?.expiryTime
)
)
) : (
<p>{constants.SUBSCRIPTION_EXPIRED}</p>
)}
2021-03-14 07:19:41 +00:00
</div>
2021-04-08 05:08:15 +00:00
<div
style={{
display: 'flex',
flexDirection: 'column',
}}
>
2021-03-18 15:26:43 +00:00
{isSubscribed(subscription) ? (
<>
2021-04-08 05:52:16 +00:00
<div style={{ marginBottom: '10px' }}>
2021-04-08 05:08:15 +00:00
<Button
variant="success"
size="sm"
onClick={() => {
setIsOpen(false);
props.setPlanModalView(true);
}}
>
{constants.CHANGE}
</Button>
<Button
variant="danger"
size="sm"
onClick={() => {
2021-04-18 07:28:49 +00:00
setIsOpen(false);
2021-04-08 05:08:15 +00:00
props.setConfirmAction(
CONFIRM_ACTION.CANCEL_SUBSCRIPTION
);
}}
style={{ marginLeft: '10px' }}
>
{constants.CANCEL_SUBSCRIPTION}
</Button>
</div>
<Button
2021-04-08 05:08:15 +00:00
variant="secondary"
size="sm"
2021-04-08 05:08:15 +00:00
onClick={onManageClick}
style={{ width: '88%' }}
>
2021-04-08 05:08:15 +00:00
{constants.MANAGEMENT_PORTAL}
</Button>
</>
) : (
<Button
variant="success"
size="sm"
onClick={() => {
setIsOpen(false);
props.setPlanModalView(true);
}}
>
{constants.SUBSCRIBE}
</Button>
)}
</div>
</div>
<div style={{ outline: 'none', marginTop: '30px' }}></div>
<div>
2021-03-16 04:40:27 +00:00
<h5 style={{ marginBottom: '12px' }}>
{constants.USAGE_DETAILS}
</h5>
2021-03-14 07:19:41 +00:00
<div style={{ color: '#959595' }}>
{usage ? (
constants.USAGE_INFO(
usage,
Math.ceil(
2021-03-17 07:27:26 +00:00
Number(convertBytesToGBs(subscription?.storage))
)
)
) : (
<Spinner animation="border" />
)}
</div>
</div>
2021-03-16 04:40:27 +00:00
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
></div>
<h5
style={{ cursor: 'pointer', marginTop: '40px' }}
onClick={openFeedbackURL}
>
2021-03-14 07:41:50 +00:00
request feature
</h5>
2021-03-14 07:46:22 +00:00
<h5 style={{ cursor: 'pointer', marginTop: '30px' }}>
2021-03-16 04:40:27 +00:00
<a
href="mailto:contact@ente.io"
style={{ textDecoration: 'inherit', color: 'inherit' }}
2021-03-20 15:18:17 +00:00
target="_blank"
rel="noreferrer noopener"
2021-03-16 04:40:27 +00:00
>
2021-03-14 07:46:22 +00:00
support
</a>
</h5>
<RecoveryKeyModal
show={modalView}
onHide={() => setModalView(false)}
somethingWentWrong={props.somethingWentWrong}
/>
<h5
style={{ cursor: 'pointer', marginTop: '30px' }}
onClick={() => setModalView(true)}
>
{constants.DOWNLOAD_RECOVERY_KEY}
</h5>
2021-04-05 14:48:57 +00:00
<h5
style={{ cursor: 'pointer', marginTop: '30px' }}
onClick={() => router.push('changePassword')}
>
{constants.CHANGE_PASSWORD}
</h5>
<h5
style={{ cursor: 'pointer', marginTop: '30px' }}
onClick={exportFiles}
>
{constants.EXPORT}
</h5>
2021-04-05 13:55:41 +00:00
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
></div>
<h5
style={{
cursor: 'pointer',
color: '#F96C6C',
2021-04-11 05:13:36 +00:00
margin: '30px 0',
}}
onClick={() => props.setConfirmAction(CONFIRM_ACTION.LOGOUT)}
>
logout
</h5>
</Menu>
);
}