import { useInstanceStatus } from 'hooks/api/getters/useInstanceStatus/useInstanceStatus'; import React, { FC, VFC, useEffect, useState, useContext } from 'react'; import { InstanceStatusBar } from 'component/common/InstanceStatus/InstanceStatusBar'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { Dialogue } from 'component/common/Dialogue/Dialogue'; import { Typography } from '@mui/material'; import { useNavigate } from 'react-router-dom'; import { IInstanceStatus, InstancePlan } from 'interfaces/instance'; import { ADMIN } from 'component/providers/AccessProvider/permissions'; import AccessContext from 'contexts/AccessContext'; import useInstanceStatusApi from 'hooks/api/actions/useInstanceStatusApi/useInstanceStatusApi'; import { trialHasExpired, canExtendTrial } from 'utils/instanceTrial'; import useToast from 'hooks/useToast'; import { formatUnknownError } from 'utils/formatUnknownError'; interface ITrialDialogProps { instanceStatus: IInstanceStatus; onExtendTrial: () => Promise; } const TrialDialog: VFC = ({ instanceStatus, onExtendTrial, }) => { const { hasAccess } = useContext(AccessContext); const navigate = useNavigate(); const expired = trialHasExpired(instanceStatus); const [dialogOpen, setDialogOpen] = useState(expired); const onClose = (event: React.SyntheticEvent, muiCloseReason?: string) => { if (!muiCloseReason) { setDialogOpen(false); if (canExtendTrial(instanceStatus)) { onExtendTrial().catch(console.error); } } }; useEffect(() => { setDialogOpen(expired); const interval = setInterval(() => { setDialogOpen(expired); }, 60000); return () => clearInterval(interval); }, [expired]); if (instanceStatus.plan === InstancePlan.ENTERPRISE) { return ( { setDialogOpen(false); }} title={`Your free ${instanceStatus.plan} trial has expired!`} > Please contact your Unleash sales representative to avoid{' '} deletion of your Unleash account. ); } if (hasAccess(ADMIN)) { return ( { navigate('/admin/billing'); setDialogOpen(false); }} onClose={onClose} title={`Your free ${instanceStatus.plan} trial has expired!`} > Upgrade trial otherwise your{' '} account will be deleted. ); } return ( { setDialogOpen(false); }} title={`Your free ${instanceStatus.plan} trial has expired!`} > Please inform your admin to Upgrade trial or your account will be deleted. ); }; export const InstanceStatus: FC = ({ children }) => { const { instanceStatus, refetchInstanceStatus } = useInstanceStatus(); const { extendTrial } = useInstanceStatusApi(); const { setToastApiError } = useToast(); const onExtendTrial = async () => { try { await extendTrial(); await refetchInstanceStatus(); } catch (error: unknown) { setToastApiError(formatUnknownError(error)); } }; return ( <> ( <> )} /> {children} ); }; const InstanceStatusBarMemo = React.memo(InstanceStatusBar);