ente/src/components/Sidebar.tsx

282 lines
10 KiB
TypeScript
Raw Normal View History

2021-05-30 16:56:48 +00:00
import React, { useEffect, useState } from 'react';
2021-05-30 16:56:48 +00:00
import { slide as Menu } from 'react-burger-menu';
import billingService, { Subscription } from 'services/billingService';
import constants from 'utils/strings/constants';
2021-05-30 16:56:48 +00:00
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { getToken } from 'utils/common/key';
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';
2021-05-30 16:56:48 +00:00
import { File } from 'services/fileService';
2021-03-27 13:52:53 +00:00
import isElectron from 'is-electron';
2021-05-30 16:56:48 +00:00
import { Collection } from 'services/collectionService';
import { useRouter } from 'next/router';
import LinkButton from './pages/gallery/LinkButton';
2021-05-30 16:56:48 +00:00
import { downloadApp } from 'utils/common';
import { logoutUser } from 'services/userService';
import { LogoImage } from 'pages/_app';
import { SetDialogMessage } from './MessageDialog';
2021-05-29 06:27:52 +00:00
import EnteSpinner from './EnteSpinner';
import RecoveryKeyModal from './RecoveryKeyModal';
2021-06-24 08:31:05 +00:00
import TwoFactorModal from './TwoFactorModal';
import { SetLoading } from 'pages/gallery';
2021-03-12 04:50:58 +00:00
interface Props {
files: File[];
collections: Collection[];
2021-04-20 11:09:03 +00:00
setDialogMessage: SetDialogMessage;
2021-06-24 08:31:05 +00:00
setLoading: SetLoading,
showPlanSelectorModal: () => void;
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);
2021-06-24 08:31:05 +00:00
const [twoFactorModalView, setTwoFactorModalView] = 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-06-12 18:33:25 +00:00
const feedbackURL: string = `${getEndpoint()}/users/feedback?token=${encodeURIComponent(getToken())}`;
2021-05-29 06:27:52 +00:00
const win = window.open(feedbackURL, '_blank');
2021-03-14 07:41:50 +00:00
win.focus();
}
2021-04-19 08:29:12 +00:00
function openSupportMail() {
2021-05-29 06:27:52 +00:00
const a = document.createElement('a');
2021-04-19 08:29:12 +00:00
a.href = 'mailto:contact@ente.io';
2021-06-04 11:44:30 +00:00
2021-04-19 08:29:12 +00:00
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.showPlanSelectorModal();
}
return (
<Menu
isOpen={isOpen}
onStateChange={(state) => setIsOpen(state.isOpen)}
itemListElement="div"
>
2021-06-04 11:42:23 +00:00
<div style={{ display: 'flex', outline: 'none', textAlign: 'center' }}>
2021-05-24 15:58:45 +00:00
<LogoImage
2021-05-30 16:56:48 +00:00
style={{ height: '24px', padding: '3px' }}
2021-05-24 15:58:45 +00:00
alt="logo"
src="/icon.svg"
/>
</div>
<div
style={{
outline: 'none',
color: 'rgb(45, 194, 98)',
fontSize: '16px',
}}
>
2021-04-20 11:27:27 +00:00
{user?.email}
</div>
2021-06-04 11:42:23 +00:00
<div style={{ flex: 1, overflow: 'auto', outline: 'none', paddingTop: '0' }}>
2021-05-30 16:56:48 +00:00
<div style={{ outline: 'none' }}>
<div style={{ display: 'flex' }}>
<h5 style={{ margin: '4px 0 12px 2px' }}>
{constants.SUBSCRIPTION_PLAN}
</h5>
2021-04-20 11:27:27 +00:00
</div>
2021-05-30 16:56:48 +00:00
<div style={{ color: '#959595' }}>
{isSubscriptionActive(subscription) ? (
isOnFreePlan(subscription) ? (
constants.FREE_SUBSCRIPTION_INFO(
2021-05-29 06:27:52 +00:00
subscription?.expiryTime,
)
) : isSubscriptionCancelled(subscription) ? (
constants.RENEWAL_CANCELLED_SUBSCRIPTION_INFO(
2021-05-29 06:27:52 +00:00
subscription?.expiryTime,
)
) : (
constants.RENEWAL_ACTIVE_SUBSCRIPTION_INFO(
2021-05-29 06:27:52 +00:00
subscription?.expiryTime,
)
)
) : (
<p>{constants.SUBSCRIPTION_EXPIRED}</p>
)}
<Button
2021-05-23 12:04:29 +00:00
variant="outline-success"
block
size="sm"
onClick={onManageClick}
>
2021-05-29 06:27:52 +00:00
{isSubscribed(subscription) ?
constants.MANAGE :
constants.SUBSCRIBE}
</Button>
</div>
2021-03-14 07:19:41 +00:00
</div>
2021-05-30 16:56:48 +00:00
<div style={{ outline: 'none', marginTop: '30px' }} />
<div>
2021-05-30 16:56:48 +00:00
<h5 style={{ marginBottom: '12px' }}>
{constants.USAGE_DETAILS}
</h5>
2021-05-30 16:56:48 +00:00
<div style={{ color: '#959595' }}>
{usage ? (
constants.USAGE_INFO(
usage,
2021-05-29 06:27:52 +00:00
Number(convertBytesToGBs(subscription?.storage)),
)
) : (
2021-05-30 16:56:48 +00:00
<div style={{ textAlign: 'center' }}>
<EnteSpinner
style={{
borderWidth: '2px',
width: '20px',
height: '20px',
}}
/>
</div>
)}
</div>
</div>
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
/>
<LinkButton
2021-05-30 16:56:48 +00:00
style={{ marginTop: '30px' }}
onClick={openFeedbackURL}
>
{constants.REQUEST_FEATURE}
</LinkButton>
<LinkButton
2021-05-30 16:56:48 +00:00
style={{ marginTop: '30px' }}
onClick={openSupportMail}
>
{constants.SUPPORT}
</LinkButton>
<>
<RecoveryKeyModal
show={recoverModalView}
onHide={() => setRecoveryModalView(false)}
2021-05-29 06:27:52 +00:00
somethingWentWrong={() => props.setDialogMessage({
title: constants.RECOVER_KEY_GENERATION_FAILED,
2021-05-30 16:56:48 +00:00
close: { variant: 'danger' },
2021-05-29 06:27:52 +00:00
})}
/>
<LinkButton
2021-05-30 16:56:48 +00:00
style={{ marginTop: '30px' }}
onClick={() => setRecoveryModalView(true)}
>
{constants.DOWNLOAD_RECOVERY_KEY}
</LinkButton>
</>
2021-06-24 08:31:05 +00:00
<>
<TwoFactorModal
show={twoFactorModalView}
onHide={() => setTwoFactorModalView(false)}
setDialogMessage={props.setDialogMessage}
setLoading={props.setLoading}
/>
<LinkButton
style={{ marginTop: '30px' }}
onClick={() => setTwoFactorModalView(true)}
>
{constants.TWO_FACTOR}
</LinkButton>
</>
<LinkButton
2021-05-30 16:56:48 +00:00
style={{ marginTop: '30px' }}
onClick={() => {
2021-05-30 16:56:48 +00:00
setData(LS_KEYS.SHOW_BACK_BUTTON, { value: true });
2021-06-26 11:44:06 +00:00
router.push('change-password');
}}
>
{constants.CHANGE_PASSWORD}
</LinkButton>
2021-05-30 16:56:48 +00:00
<LinkButton style={{ marginTop: '30px' }} onClick={exportFiles}>
{constants.EXPORT}
</LinkButton>
<div
style={{
height: '1px',
marginTop: '40px',
background: '#242424',
width: '100%',
}}
/>
<LinkButton
variant="danger"
2021-05-30 16:56:48 +00:00
style={{ marginTop: '30px' }}
2021-05-29 06:27:52 +00:00
onClick={() => props.setDialogMessage({
title: `${constants.CONFIRM} ${constants.LOGOUT}`,
content: constants.LOGOUT_MESSAGE,
staticBackdrop: true,
proceed: {
text: constants.LOGOUT,
action: logoutUser,
variant: 'danger',
},
2021-05-30 16:56:48 +00:00
close: { text: constants.CANCEL },
2021-05-29 06:27:52 +00:00
})}
2021-04-28 06:09:56 +00:00
>
2021-06-04 11:42:23 +00:00
{constants.LOGOUT}
2021-04-19 08:29:12 +00:00
</LinkButton>
<div
style={{
marginTop: '40px',
width: '100%',
}}
/>
</div>
</Menu>
);
}