2022-05-19 14:06:18 +02:00
|
|
|
import { useInstanceStatus } from 'hooks/api/getters/useInstanceStatus/useInstanceStatus';
|
2022-05-25 23:26:05 +02:00
|
|
|
import React, { FC, VFC, useEffect, useState, useContext } from 'react';
|
2022-05-19 14:06:18 +02:00
|
|
|
import { InstanceStatusBar } from 'component/common/InstanceStatus/InstanceStatusBar';
|
|
|
|
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
2022-05-25 23:26:05 +02:00
|
|
|
import { Dialogue } from 'component/common/Dialogue/Dialogue';
|
|
|
|
import { Typography } from '@mui/material';
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
import { IInstanceStatus, InstanceState } from 'interfaces/instance';
|
|
|
|
import { ADMIN } from 'component/providers/AccessProvider/permissions';
|
|
|
|
import AccessContext from 'contexts/AccessContext';
|
|
|
|
import useInstanceStatusApi from 'hooks/api/actions/useInstanceStatusApi/useInstanceStatusApi';
|
2022-06-14 11:51:11 +02:00
|
|
|
import { hasTrialExpired } from 'utils/instanceTrial';
|
2022-05-25 23:26:05 +02:00
|
|
|
import useToast from 'hooks/useToast';
|
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
|
|
|
|
|
|
interface ITrialDialogProps {
|
|
|
|
instanceStatus: IInstanceStatus;
|
|
|
|
onExtendTrial: () => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TrialDialog: VFC<ITrialDialogProps> = ({
|
|
|
|
instanceStatus,
|
|
|
|
onExtendTrial,
|
|
|
|
}) => {
|
|
|
|
const { hasAccess } = useContext(AccessContext);
|
|
|
|
const navigate = useNavigate();
|
2022-06-14 11:51:11 +02:00
|
|
|
const trialHasExpired = hasTrialExpired(instanceStatus);
|
|
|
|
const [dialogOpen, setDialogOpen] = useState(trialHasExpired);
|
2022-05-25 23:26:05 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-14 11:51:11 +02:00
|
|
|
setDialogOpen(trialHasExpired);
|
2022-05-25 23:26:05 +02:00
|
|
|
const interval = setInterval(() => {
|
2022-06-14 11:51:11 +02:00
|
|
|
setDialogOpen(trialHasExpired);
|
2022-05-25 23:26:05 +02:00
|
|
|
}, 60000);
|
|
|
|
return () => clearInterval(interval);
|
2022-06-14 11:51:11 +02:00
|
|
|
}, [trialHasExpired]);
|
2022-05-25 23:26:05 +02:00
|
|
|
|
|
|
|
if (hasAccess(ADMIN)) {
|
|
|
|
return (
|
|
|
|
<Dialogue
|
|
|
|
open={dialogOpen}
|
|
|
|
primaryButtonText="Upgrade trial"
|
|
|
|
secondaryButtonText={
|
|
|
|
instanceStatus?.trialExtended
|
|
|
|
? 'Remind me later'
|
|
|
|
: 'Extend trial (5 days)'
|
|
|
|
}
|
|
|
|
onClick={() => {
|
|
|
|
navigate('/admin/billing');
|
|
|
|
setDialogOpen(false);
|
|
|
|
}}
|
|
|
|
onClose={(_: any, reason?: string) => {
|
|
|
|
if (
|
|
|
|
reason !== 'backdropClick' &&
|
|
|
|
reason !== 'escapeKeyDown'
|
|
|
|
) {
|
|
|
|
onExtendTrial();
|
|
|
|
setDialogOpen(false);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
title={`Your free ${instanceStatus.plan} trial has expired!`}
|
|
|
|
>
|
|
|
|
<Typography>
|
|
|
|
<strong>Upgrade trial</strong> otherwise your{' '}
|
|
|
|
<strong>account will be deleted.</strong>
|
|
|
|
</Typography>
|
|
|
|
</Dialogue>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialogue
|
|
|
|
open={dialogOpen}
|
|
|
|
secondaryButtonText="Remind me later"
|
|
|
|
onClose={() => {
|
|
|
|
setDialogOpen(false);
|
|
|
|
}}
|
|
|
|
title={`Your free ${instanceStatus.plan} trial has expired!`}
|
|
|
|
>
|
|
|
|
<Typography>
|
|
|
|
Please inform your admin to <strong>Upgrade trial</strong> or
|
|
|
|
your <strong>account will be deleted.</strong>
|
|
|
|
</Typography>
|
|
|
|
</Dialogue>
|
|
|
|
);
|
|
|
|
};
|
2022-05-19 14:06:18 +02:00
|
|
|
|
|
|
|
export const InstanceStatus: FC = ({ children }) => {
|
2022-05-25 23:26:05 +02:00
|
|
|
const { instanceStatus, refetchInstanceStatus, isBilling } =
|
|
|
|
useInstanceStatus();
|
|
|
|
const { extendTrial } = useInstanceStatusApi();
|
|
|
|
const { setToastApiError } = useToast();
|
|
|
|
|
|
|
|
const onExtendTrial = async () => {
|
|
|
|
if (
|
|
|
|
instanceStatus?.state === InstanceState.TRIAL &&
|
|
|
|
!instanceStatus?.trialExtended
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await extendTrial();
|
|
|
|
await refetchInstanceStatus();
|
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-05-19 14:06:18 +02:00
|
|
|
|
|
|
|
return (
|
2022-05-25 23:26:05 +02:00
|
|
|
<div style={{ height: '100%' }}>
|
2022-05-19 14:06:18 +02:00
|
|
|
<ConditionallyRender
|
2022-05-25 23:26:05 +02:00
|
|
|
condition={isBilling && Boolean(instanceStatus)}
|
2022-05-19 14:06:18 +02:00
|
|
|
show={() => (
|
2022-05-25 23:26:05 +02:00
|
|
|
<>
|
|
|
|
<InstanceStatusBarMemo
|
|
|
|
instanceStatus={instanceStatus!}
|
|
|
|
/>
|
|
|
|
<TrialDialog
|
|
|
|
instanceStatus={instanceStatus!}
|
|
|
|
onExtendTrial={onExtendTrial}
|
|
|
|
/>
|
|
|
|
</>
|
2022-05-19 14:06:18 +02:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const InstanceStatusBarMemo = React.memo(InstanceStatusBar);
|