ente/src/components/Sidebar.tsx

227 lines
7.8 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { slide as Menu } from 'react-burger-menu';
2021-03-17 07:27:26 +00:00
import ConfirmDialog 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 { logoutUser } from 'services/userService';
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 {
isPlanActive,
2021-03-17 07:27:26 +00:00
convertBytesToGBs,
getUserSubscription,
2021-03-17 10:44:29 +00:00
isOnFreePlan,
isPlanCancelled,
hasRenewingPaidPlan,
2021-03-17 07:27:26 +00:00
} from 'utils/billingUtil';
2021-03-17 07:27:26 +00:00
enum Action {
logout = 'logout',
cancelSubscription = 'cancel_subscription',
}
2021-03-12 04:50:58 +00:00
interface Props {
setNavbarIconView;
setPlanModalView;
setBannerMessage;
2021-03-12 04:50:58 +00:00
}
export default function Sidebar(props: Props) {
2021-03-17 07:27:26 +00:00
const [confirmModalView, setConfirmModalView] = useState(false);
function closeConfirmModal() {
setConfirmModalView(false);
}
const [usage, SetUsage] = useState<string>(null);
2021-03-17 07:27:26 +00:00
const [action, setAction] = useState<string>(null);
const [user, setUser] = useState(null);
const [subscription, setSubscription] = useState<Subscription>(null);
useEffect(() => {
setUser(getData(LS_KEYS.USER));
setSubscription(getUserSubscription());
}, []);
const [isOpen, setIsOpen] = 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]);
const logout = async () => {
2021-03-17 07:27:26 +00:00
setConfirmModalView(false);
setIsOpen(false);
2021-03-12 04:50:58 +00:00
props.setNavbarIconView(false);
logoutUser();
};
2021-03-14 07:41:50 +00:00
2021-03-17 07:27:26 +00:00
const cancelSubscription = async () => {
setConfirmModalView(false);
setIsOpen(false);
try {
await billingService.cancelSubscription();
} catch (e) {
props.setBannerMessage({
message: constants.SUBSCRIPTION_CANCEL_FAILED,
variant: 'danger',
});
}
props.setBannerMessage({
message: constants.SUBSCRIPTION_CANCEL_SUCCESS,
variant: 'secondary',
});
2021-03-17 07:27:26 +00:00
};
let callback = [];
callback[Action.logout] = logout;
callback[Action.cancelSubscription] = cancelSubscription;
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();
}
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' }}>
{isPlanActive(subscription) ? (
2021-03-17 10:44:29 +00:00
isOnFreePlan(subscription) ? (
constants.FREE_SUBSCRIPTION_INFO(
subscription?.expiryTime
)
) : isPlanCancelled(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 style={{ display: 'flex' }}>
{hasRenewingPaidPlan(subscription) ? (
<>
<Button
variant="success"
size="sm"
onClick={() => {
setIsOpen(false);
props.setPlanModalView(true);
}}
>
{constants.MANAGE}
</Button>
<Button
variant="danger"
size="sm"
onClick={() => {
setAction(Action.cancelSubscription);
setConfirmModalView(true);
}}
style={{ marginLeft: '10px' }}
>
{constants.CANCEL_SUBSCRIPTION}
</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-14 07:46:22 +00:00
support
</a>
</h5>
<>
2021-03-17 07:27:26 +00:00
<ConfirmDialog
show={confirmModalView}
onHide={closeConfirmModal}
callback={callback}
action={action}
/>
2021-03-16 04:40:27 +00:00
<h5
style={{
cursor: 'pointer',
color: '#F96C6C',
marginTop: '30px',
}}
2021-03-17 07:27:26 +00:00
onClick={() => {
setAction(Action.logout);
setConfirmModalView(true);
}}
2021-03-16 04:40:27 +00:00
>
logout
2021-03-14 07:41:50 +00:00
</h5>
</>
</Menu>
);
}