2022-05-25 23:26:05 +02:00
|
|
|
import { IUser } from 'interfaces/user';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useInstanceStatus } from './api/getters/useInstanceStatus/useInstanceStatus';
|
|
|
|
import { InstancePlan } from 'interfaces/instance';
|
2023-02-22 20:50:29 +01:00
|
|
|
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
|
2022-05-25 23:26:05 +02:00
|
|
|
|
|
|
|
export interface IUsersPlanOutput {
|
|
|
|
planUsers: IUser[];
|
|
|
|
isBillingUsers: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useUsersPlan = (users: IUser[]): IUsersPlanOutput => {
|
|
|
|
const { instanceStatus } = useInstanceStatus();
|
2023-02-22 20:50:29 +01:00
|
|
|
const { uiConfig } = useUiConfig();
|
2022-05-25 23:26:05 +02:00
|
|
|
|
2023-02-22 20:50:29 +01:00
|
|
|
const isBillingUsers = Boolean(
|
|
|
|
uiConfig?.flags?.proPlanAutoCharge &&
|
|
|
|
instanceStatus?.plan === InstancePlan.PRO
|
|
|
|
);
|
2022-05-31 08:59:09 +02:00
|
|
|
const seats = instanceStatus?.seats ?? 5;
|
2022-05-25 23:26:05 +02:00
|
|
|
|
|
|
|
const planUsers = useMemo(
|
2022-05-31 08:59:09 +02:00
|
|
|
() => calculatePaidUsers(users, isBillingUsers, seats),
|
|
|
|
[users, isBillingUsers, seats]
|
2022-05-25 23:26:05 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
planUsers,
|
|
|
|
isBillingUsers,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const calculatePaidUsers = (
|
|
|
|
users: IUser[],
|
|
|
|
isBillingUsers: boolean,
|
|
|
|
seats: number = 0
|
|
|
|
) => {
|
|
|
|
if (!isBillingUsers || !seats) return users;
|
|
|
|
|
|
|
|
users
|
|
|
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt))
|
|
|
|
.forEach((user, index) => {
|
|
|
|
user.paid = false;
|
|
|
|
|
|
|
|
// If index is greater or equal to seat, the
|
|
|
|
// user isn't paid for and we will add use this
|
|
|
|
// to add costs and icons in the userlist
|
|
|
|
if (index >= seats) {
|
|
|
|
user.paid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return users;
|
|
|
|
};
|