Merge pull request #1048 from automatisch/aut-92
feat: add trial status badge in appbar
This commit is contained in:
@@ -9,7 +9,9 @@ const getTrialStatus = async (
|
|||||||
if (!appConfig.isCloud) return;
|
if (!appConfig.isCloud) return;
|
||||||
|
|
||||||
const inTrial = await context.currentUser.inTrial();
|
const inTrial = await context.currentUser.inTrial();
|
||||||
if (!inTrial) return;
|
const hasActiveSubscription = await context.currentUser.hasActiveSubscription();
|
||||||
|
|
||||||
|
if (!inTrial && hasActiveSubscription) return;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
expireAt: context.currentUser.trialExpiryDate,
|
expireAt: context.currentUser.trialExpiryDate,
|
||||||
|
@@ -165,6 +165,16 @@ class User extends Base {
|
|||||||
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async hasActiveSubscription() {
|
||||||
|
if (!appConfig.isCloud) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const subscription = await this.$relatedQuery('currentSubscription');
|
||||||
|
|
||||||
|
return subscription?.isActive;
|
||||||
|
}
|
||||||
|
|
||||||
async inTrial() {
|
async inTrial() {
|
||||||
if (!appConfig.isCloud) {
|
if (!appConfig.isCloud) {
|
||||||
return false;
|
return false;
|
||||||
@@ -174,9 +184,7 @@ class User extends Base {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscription = await this.$relatedQuery('currentSubscription');
|
if (await this.hasActiveSubscription()) {
|
||||||
|
|
||||||
if (subscription?.isActive) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,6 +12,7 @@ import AccountCircleIcon from '@mui/icons-material/AccountCircle';
|
|||||||
|
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import AccountDropdownMenu from 'components/AccountDropdownMenu';
|
import AccountDropdownMenu from 'components/AccountDropdownMenu';
|
||||||
|
import TrialStatusBadge from 'components/TrialStatusBadge/index.ee';
|
||||||
import Container from 'components/Container';
|
import Container from 'components/Container';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
import { Link } from './style';
|
import { Link } from './style';
|
||||||
@@ -67,9 +68,10 @@ export default function AppBar(props: AppBarProps): React.ReactElement {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<TrialStatusBadge />
|
||||||
|
|
||||||
<IconButton
|
<IconButton
|
||||||
size="large"
|
size="large"
|
||||||
edge="start"
|
|
||||||
color="inherit"
|
color="inherit"
|
||||||
onClick={handleAccountMenuOpen}
|
onClick={handleAccountMenuOpen}
|
||||||
aria-controls={accountMenuId}
|
aria-controls={accountMenuId}
|
||||||
|
32
packages/web/src/components/TrialOverAlert/index.ee.tsx
Normal file
32
packages/web/src/components/TrialOverAlert/index.ee.tsx
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import Alert from '@mui/material/Alert';
|
||||||
|
import Typography from '@mui/material/Typography';
|
||||||
|
|
||||||
|
import * as URLS from 'config/urls';
|
||||||
|
import { generateInternalLink } from 'helpers/translation-values';
|
||||||
|
import useTrialStatus from 'hooks/useTrialStatus.ee';
|
||||||
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
|
||||||
|
|
||||||
|
export default function TrialOverAlert() {
|
||||||
|
const formatMessage = useFormatMessage();
|
||||||
|
const trialStatus = useTrialStatus();
|
||||||
|
|
||||||
|
if (!trialStatus || !trialStatus.over) return <React.Fragment />;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Alert
|
||||||
|
severity="error"
|
||||||
|
sx={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography variant="subtitle2" sx={{ lineHeight: 1.5 }}>
|
||||||
|
{formatMessage('trialOverAlert.text', {
|
||||||
|
link: generateInternalLink(URLS.SETTINGS_PLAN_UPGRADE)
|
||||||
|
})}
|
||||||
|
</Typography>
|
||||||
|
</Alert>
|
||||||
|
);
|
||||||
|
}
|
24
packages/web/src/components/TrialStatusBadge/index.ee.tsx
Normal file
24
packages/web/src/components/TrialStatusBadge/index.ee.tsx
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import { Link } from 'react-router-dom';
|
||||||
|
import { Chip } from './style.ee';
|
||||||
|
|
||||||
|
import * as URLS from 'config/urls';
|
||||||
|
import useTrialStatus from 'hooks/useTrialStatus.ee';
|
||||||
|
|
||||||
|
export default function TrialStatusBadge(): React.ReactElement {
|
||||||
|
const data = useTrialStatus();
|
||||||
|
|
||||||
|
if (!data) return <React.Fragment />;
|
||||||
|
|
||||||
|
const { message, status } = data;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Chip
|
||||||
|
component={Link}
|
||||||
|
to={URLS.SETTINGS_BILLING_AND_USAGE}
|
||||||
|
clickable
|
||||||
|
label={message}
|
||||||
|
color={status}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
13
packages/web/src/components/TrialStatusBadge/style.ee.tsx
Normal file
13
packages/web/src/components/TrialStatusBadge/style.ee.tsx
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { styled } from '@mui/material/styles';
|
||||||
|
import MuiChip, { chipClasses } from '@mui/material/Chip';
|
||||||
|
|
||||||
|
export const Chip = styled(MuiChip)`
|
||||||
|
&.${chipClasses.root} {
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.${chipClasses.colorWarning} {
|
||||||
|
background: #fef3c7;
|
||||||
|
color: #78350f;
|
||||||
|
}
|
||||||
|
` as typeof MuiChip;
|
@@ -1,5 +1,7 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
import Alert from '@mui/material/Alert';
|
||||||
|
import Stack from '@mui/material/Stack';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import Chip from '@mui/material/Chip';
|
import Chip from '@mui/material/Chip';
|
||||||
@@ -11,11 +13,13 @@ import Grid from '@mui/material/Grid';
|
|||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
|
|
||||||
import { TBillingCardAction } from '@automatisch/types';
|
import { TBillingCardAction } from '@automatisch/types';
|
||||||
|
import TrialOverAlert from 'components/TrialOverAlert/index.ee';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import useBillingAndUsageData from 'hooks/useBillingAndUsageData.ee';
|
import useBillingAndUsageData from 'hooks/useBillingAndUsageData.ee';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
|
||||||
const capitalize = (str: string) => str[0].toUpperCase() + str.slice(1, str.length);
|
const capitalize = (str: string) =>
|
||||||
|
str[0].toUpperCase() + str.slice(1, str.length);
|
||||||
|
|
||||||
type BillingCardProps = {
|
type BillingCardProps = {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -62,21 +66,13 @@ function Action(props: { action?: TBillingCardAction }) {
|
|||||||
if (type === 'link') {
|
if (type === 'link') {
|
||||||
if (action.src.startsWith('http')) {
|
if (action.src.startsWith('http')) {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button size="small" href={action.src} target="_blank">
|
||||||
size="small"
|
|
||||||
href={action.src}
|
|
||||||
target="_blank"
|
|
||||||
>
|
|
||||||
{text}
|
{text}
|
||||||
</Button>
|
</Button>
|
||||||
)
|
);
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button size="small" component={Link} to={action.src}>
|
||||||
size="small"
|
|
||||||
component={Link}
|
|
||||||
to={action.src}
|
|
||||||
>
|
|
||||||
{text}
|
{text}
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
@@ -100,6 +96,9 @@ export default function UsageDataInformation() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
|
<Stack sx={{ width: '100%', mb: 2 }} spacing={2}>
|
||||||
|
<TrialOverAlert />
|
||||||
|
</Stack>
|
||||||
<Card sx={{ mb: 3, p: 2 }}>
|
<Card sx={{ mb: 3, p: 2 }}>
|
||||||
<CardContent sx={{ display: 'flex', flexDirection: 'column' }}>
|
<CardContent sx={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between' }}>
|
<Box sx={{ mb: 1, display: 'flex', justifyContent: 'space-between' }}>
|
||||||
@@ -137,7 +136,9 @@ export default function UsageDataInformation() {
|
|||||||
<BillingCard
|
<BillingCard
|
||||||
name={formatMessage('usageDataInformation.nextBillAmount')}
|
name={formatMessage('usageDataInformation.nextBillAmount')}
|
||||||
title={billingAndUsageData?.subscription?.nextBillAmount.title}
|
title={billingAndUsageData?.subscription?.nextBillAmount.title}
|
||||||
action={billingAndUsageData?.subscription?.nextBillAmount.action}
|
action={
|
||||||
|
billingAndUsageData?.subscription?.nextBillAmount.action
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
@@ -192,15 +193,17 @@ export default function UsageDataInformation() {
|
|||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{/* free plan has `null` status so that we can show the upgrade button */}
|
{/* free plan has `null` status so that we can show the upgrade button */}
|
||||||
{billingAndUsageData?.subscription?.status === null && <Button
|
{billingAndUsageData?.subscription?.status === null && (
|
||||||
component={Link}
|
<Button
|
||||||
to={URLS.SETTINGS_PLAN_UPGRADE}
|
component={Link}
|
||||||
size="small"
|
to={URLS.SETTINGS_PLAN_UPGRADE}
|
||||||
variant="contained"
|
size="small"
|
||||||
sx={{ mt: 2, alignSelf: 'flex-end' }}
|
variant="contained"
|
||||||
>
|
sx={{ mt: 2, alignSelf: 'flex-end' }}
|
||||||
{formatMessage('usageDataInformation.upgrade')}
|
>
|
||||||
</Button>}
|
{formatMessage('usageDataInformation.upgrade')}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
|
9
packages/web/src/graphql/queries/get-trial-status.ee.ts
Normal file
9
packages/web/src/graphql/queries/get-trial-status.ee.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { gql } from '@apollo/client';
|
||||||
|
|
||||||
|
export const GET_TRIAL_STATUS = gql`
|
||||||
|
query GetTrialStatus {
|
||||||
|
getTrialStatus {
|
||||||
|
expireAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
@@ -1,5 +1,13 @@
|
|||||||
|
import { Link as RouterLink } from 'react-router-dom';
|
||||||
import Link from '@mui/material/Link';
|
import Link from '@mui/material/Link';
|
||||||
|
|
||||||
|
export const generateInternalLink = (link: string) => (str: string) =>
|
||||||
|
(
|
||||||
|
<Link component={RouterLink} to={link}>
|
||||||
|
{str}
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
|
||||||
export const generateExternalLink = (link: string) => (str: string) =>
|
export const generateExternalLink = (link: string) => (str: string) =>
|
||||||
(
|
(
|
||||||
<Link href={link} target="_blank">
|
<Link href={link} target="_blank">
|
||||||
|
@@ -23,7 +23,7 @@ function transform(billingAndUsageData: NonNullable<UseBillingAndUsageDataReturn
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UseBillingAndUsageDataReturn = {
|
type UseBillingAndUsageDataReturn = {
|
||||||
subscription: TSubscription,
|
subscription: TSubscription;
|
||||||
usage: {
|
usage: {
|
||||||
task: number;
|
task: number;
|
||||||
}
|
}
|
||||||
|
70
packages/web/src/hooks/useTrialStatus.ee.ts
Normal file
70
packages/web/src/hooks/useTrialStatus.ee.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import { useQuery } from '@apollo/client';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
|
import { GET_TRIAL_STATUS } from 'graphql/queries/get-trial-status.ee';
|
||||||
|
import useFormatMessage from './useFormatMessage';
|
||||||
|
|
||||||
|
type UseTrialStatusReturn = {
|
||||||
|
expireAt: DateTime;
|
||||||
|
message: string;
|
||||||
|
over: boolean;
|
||||||
|
status: 'error' | 'warning';
|
||||||
|
} | null;
|
||||||
|
|
||||||
|
function getDiffInDays(date: DateTime) {
|
||||||
|
const today = DateTime.now().startOf('day');
|
||||||
|
const diffInDays = date.diff(today, 'days').days;
|
||||||
|
const roundedDiffInDays = Math.round(diffInDays);
|
||||||
|
|
||||||
|
return roundedDiffInDays;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFeedbackPayload(date: DateTime) {
|
||||||
|
const diffInDays = getDiffInDays(date);
|
||||||
|
|
||||||
|
if (diffInDays <= -1) {
|
||||||
|
return {
|
||||||
|
translationEntryId: 'trialBadge.over',
|
||||||
|
status: 'error' as const,
|
||||||
|
over: true,
|
||||||
|
};
|
||||||
|
} else if (diffInDays <= 0) {
|
||||||
|
return {
|
||||||
|
translationEntryId: 'trialBadge.endsToday',
|
||||||
|
status: 'warning' as const,
|
||||||
|
over: false,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
translationEntryId: 'trialBadge.xDaysLeft',
|
||||||
|
translationEntryValues: {
|
||||||
|
remainingDays: diffInDays
|
||||||
|
},
|
||||||
|
status: 'warning' as const,
|
||||||
|
over: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function useTrialStatus(): UseTrialStatusReturn {
|
||||||
|
const formatMessage = useFormatMessage();
|
||||||
|
const { data, loading } = useQuery(GET_TRIAL_STATUS);
|
||||||
|
|
||||||
|
if (loading || !data.getTrialStatus) return null;
|
||||||
|
|
||||||
|
const expireAt = DateTime.fromMillis(Number(data.getTrialStatus.expireAt)).startOf('day');
|
||||||
|
|
||||||
|
const {
|
||||||
|
translationEntryId,
|
||||||
|
translationEntryValues,
|
||||||
|
status,
|
||||||
|
over,
|
||||||
|
} = getFeedbackPayload(expireAt);
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: formatMessage(translationEntryId, translationEntryValues),
|
||||||
|
expireAt,
|
||||||
|
over,
|
||||||
|
status
|
||||||
|
};
|
||||||
|
}
|
@@ -152,5 +152,9 @@
|
|||||||
"invoices.date": "Date",
|
"invoices.date": "Date",
|
||||||
"invoices.amount": "Amount",
|
"invoices.amount": "Amount",
|
||||||
"invoices.invoice": "Invoice",
|
"invoices.invoice": "Invoice",
|
||||||
"invoices.link": "Link"
|
"invoices.link": "Link",
|
||||||
|
"trialBadge.xDaysLeft": "{remainingDays} trial {remainingDays, plural, one {day} other {days}} left",
|
||||||
|
"trialBadge.endsToday": "Trial ends today",
|
||||||
|
"trialBadge.over": "Trial is over",
|
||||||
|
"trialOverAlert.text": "Your free trial is over. Please <link>upgrade</link> your plan to continue using Automatisch."
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user