ente/src/components/Sidebar.tsx

246 lines
8.7 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { slide as Menu } from 'react-burger-menu';
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-04-20 11:09:03 +00:00
SetDialogMessage,
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';
2021-04-18 13:01:40 +00:00
import EnteSpinner from './EnteSpinner';
2021-04-19 08:29:12 +00:00
import LinkButton from 'pages/gallery/components/LinkButton';
2021-04-22 12:56:06 +00:00
import { downloadApp } from 'utils/common';
import { logoutUser } from 'services/userService';
2021-03-12 04:50:58 +00:00
interface Props {
2021-03-27 13:52:53 +00:00
files: file[];
collections: Collection[];
2021-04-20 11:09:03 +00:00
setDialogMessage: SetDialogMessage;
setPlanModalView;
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);
2021-04-22 14:30:07 +00:00
const [recoverModalView, setRecoveryModalView] = useState(false);
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-04-19 08:29:12 +00:00
function openSupportMail() {
var a = document.createElement('a');
a.href = 'mailto:contact@ente.io';
a.target = '_blank';
a.rel = 'noreferrer noopener';
a.click();
}
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.setDialogMessage({
title: constants.DOWNLOAD_APP,
content: constants.DOWNLOAD_APP_MESSAGE(),
staticBackdrop: true,
proceed: {
text: constants.DOWNLOAD,
action: downloadApp,
variant: 'success',
},
close: {
text: constants.CLOSE,
},
2021-04-22 12:56:06 +00:00
});
2021-03-29 05:15:01 +00:00
}
}
2021-04-19 08:29:12 +00:00
2021-04-05 05:12:37 +00:00
const router = useRouter();
function onManageClick() {
setIsOpen(false);
props.setPlanModalView(true);
}
return (
<Menu
isOpen={isOpen}
onStateChange={(state) => setIsOpen(state.isOpen)}
itemListElement="div"
>
<div
style={{
marginBottom: '28px',
outline: 'none',
color: 'rgb(45, 194, 98)',
fontSize: '16px',
}}
>
2021-04-20 11:27:27 +00:00
{user?.email}
</div>
<div style={{ outline: 'none' }}>
2021-04-20 11:27:27 +00:00
<div style={{ display: 'flex' }}>
<h5 style={{ margin: '4px 0 12px 2px' }}>
{constants.SUBSCRIPTION_PLAN}
</h5>
<div style={{ marginLeft: '10px' }}>
{
<Button
variant={
isSubscribed(subscription)
? 'outline-secondary'
: 'outline-success'
}
size="sm"
onClick={onManageClick}
2021-04-20 11:27:27 +00:00
>
{isSubscribed(subscription)
? constants.MANAGE
: constants.SUBSCRIBE}
</Button>
}
</div>
</div>
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>
</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(
Number(convertBytesToGBs(subscription?.storage))
)
)
) : (
<div style={{ textAlign: 'center' }}>
<EnteSpinner
style={{
borderWidth: '2px',
width: '20px',
height: '20px',
}}
/>
</div>
)}
</div>
</div>
2021-03-16 04:40:27 +00:00
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
></div>
2021-04-19 08:29:12 +00:00
<LinkButton onClick={openFeedbackURL}>
{constants.REQUEST_FEATURE}
</LinkButton>
<LinkButton onClick={openSupportMail}>
{constants.SUPPORT}
</LinkButton>
<>
<RecoveryKeyModal
2021-04-20 05:33:57 +00:00
show={recoverModalView}
onHide={() => setRecoveryModalView(false)}
2021-04-20 11:09:03 +00:00
somethingWentWrong={() =>
props.setDialogMessage({
title: constants.RECOVER_KEY_GENERATION_FAILED,
close: { variant: 'danger' },
})
}
2021-04-19 08:29:12 +00:00
/>
2021-04-20 05:33:57 +00:00
<LinkButton onClick={() => setRecoveryModalView(true)}>
2021-04-19 08:29:12 +00:00
{constants.DOWNLOAD_RECOVERY_KEY}
</LinkButton>
</>
<LinkButton onClick={() => router.push('changePassword')}>
2021-04-05 14:48:57 +00:00
{constants.CHANGE_PASSWORD}
2021-04-19 08:29:12 +00:00
</LinkButton>
<LinkButton onClick={exportFiles}>{constants.EXPORT}</LinkButton>
2021-04-05 13:55:41 +00:00
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
></div>
2021-04-19 08:29:12 +00:00
<LinkButton
variant="danger"
style={{ marginBottom: '50px' }}
2021-04-22 12:56:06 +00:00
onClick={() =>
props.setDialogMessage({
title: `${constants.CONFIRM} ${constants.LOGOUT}`,
content: constants.LOGOUT_MESSAGE,
staticBackdrop: true,
proceed: {
text: constants.LOGOUT,
action: logoutUser,
variant: 'danger',
},
close: { text: constants.CANCEL },
2021-04-22 12:56:06 +00:00
})
}
>
logout
2021-04-19 08:29:12 +00:00
</LinkButton>
</Menu>
);
}