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';
|
2023-03-27 12:31:19 +02:00
|
|
|
import { Typography } from '@mui/material';
|
2022-05-25 23:26:05 +02:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2023-06-19 13:48:26 +02:00
|
|
|
import { IInstanceStatus, InstancePlan } from 'interfaces/instance';
|
2022-05-25 23:26:05 +02:00
|
|
|
import { ADMIN } from 'component/providers/AccessProvider/permissions';
|
|
|
|
import AccessContext from 'contexts/AccessContext';
|
|
|
|
import useInstanceStatusApi from 'hooks/api/actions/useInstanceStatusApi/useInstanceStatusApi';
|
2022-06-21 11:22:27 +02:00
|
|
|
import { trialHasExpired, canExtendTrial } 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-21 11:22:27 +02:00
|
|
|
const expired = trialHasExpired(instanceStatus);
|
|
|
|
const [dialogOpen, setDialogOpen] = useState(expired);
|
2022-05-25 23:26:05 +02:00
|
|
|
|
2022-06-23 10:29:17 +02:00
|
|
|
const onClose = (event: React.SyntheticEvent, muiCloseReason?: string) => {
|
|
|
|
if (!muiCloseReason) {
|
|
|
|
setDialogOpen(false);
|
|
|
|
if (canExtendTrial(instanceStatus)) {
|
|
|
|
onExtendTrial().catch(console.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-25 23:26:05 +02:00
|
|
|
useEffect(() => {
|
2022-06-21 11:22:27 +02:00
|
|
|
setDialogOpen(expired);
|
2022-05-25 23:26:05 +02:00
|
|
|
const interval = setInterval(() => {
|
2022-06-21 11:22:27 +02:00
|
|
|
setDialogOpen(expired);
|
2022-05-25 23:26:05 +02:00
|
|
|
}, 60000);
|
|
|
|
return () => clearInterval(interval);
|
2022-06-21 11:22:27 +02:00
|
|
|
}, [expired]);
|
2022-05-25 23:26:05 +02:00
|
|
|
|
2023-06-19 13:48:26 +02:00
|
|
|
if (instanceStatus.plan === InstancePlan.ENTERPRISE) {
|
|
|
|
return (
|
|
|
|
<Dialogue
|
|
|
|
open={dialogOpen}
|
2023-10-02 14:25:46 +02:00
|
|
|
secondaryButtonText='Remind me later'
|
2023-06-19 13:48:26 +02:00
|
|
|
onClose={() => {
|
|
|
|
setDialogOpen(false);
|
|
|
|
}}
|
|
|
|
title={`Your free ${instanceStatus.plan} trial has expired!`}
|
|
|
|
>
|
|
|
|
<Typography>
|
|
|
|
Please contact your Unleash sales representative to avoid{' '}
|
|
|
|
<strong>deletion of your Unleash account.</strong>
|
|
|
|
</Typography>
|
|
|
|
</Dialogue>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:26:05 +02:00
|
|
|
if (hasAccess(ADMIN)) {
|
|
|
|
return (
|
|
|
|
<Dialogue
|
|
|
|
open={dialogOpen}
|
2023-10-02 14:25:46 +02:00
|
|
|
primaryButtonText='Upgrade trial'
|
2022-05-25 23:26:05 +02:00
|
|
|
secondaryButtonText={
|
2022-06-21 11:22:27 +02:00
|
|
|
canExtendTrial(instanceStatus)
|
|
|
|
? 'Extend trial (5 days)'
|
|
|
|
: 'Remind me later'
|
2022-05-25 23:26:05 +02:00
|
|
|
}
|
|
|
|
onClick={() => {
|
|
|
|
navigate('/admin/billing');
|
|
|
|
setDialogOpen(false);
|
|
|
|
}}
|
2022-06-23 10:29:17 +02:00
|
|
|
onClose={onClose}
|
2022-05-25 23:26:05 +02:00
|
|
|
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}
|
2023-10-02 14:25:46 +02:00
|
|
|
secondaryButtonText='Remind me later'
|
2022-05-25 23:26:05 +02:00
|
|
|
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 }) => {
|
2023-06-21 16:08:52 +02:00
|
|
|
const { instanceStatus, refetchInstanceStatus } = useInstanceStatus();
|
2022-05-25 23:26:05 +02:00
|
|
|
const { extendTrial } = useInstanceStatusApi();
|
|
|
|
const { setToastApiError } = useToast();
|
|
|
|
|
|
|
|
const onExtendTrial = async () => {
|
2022-06-21 11:22:27 +02:00
|
|
|
try {
|
|
|
|
await extendTrial();
|
|
|
|
await refetchInstanceStatus();
|
|
|
|
} catch (error: unknown) {
|
|
|
|
setToastApiError(formatUnknownError(error));
|
2022-05-25 23:26:05 +02:00
|
|
|
}
|
|
|
|
};
|
2022-05-19 14:06:18 +02:00
|
|
|
|
|
|
|
return (
|
2023-03-22 13:15:53 +01:00
|
|
|
<>
|
2022-05-19 14:06:18 +02:00
|
|
|
<ConditionallyRender
|
2023-06-16 13:35:24 +02:00
|
|
|
condition={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}
|
2023-03-22 13:15:53 +01:00
|
|
|
</>
|
2022-05-19 14:06:18 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const InstanceStatusBarMemo = React.memo(InstanceStatusBar);
|