1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/useUsersPlan.ts
Nuno Góis 570e9f88be feat: upgrade users table (#1040)
* feat: upgrade users table

* fix misc ui/ux bugs

* refactor: address PR comments

* fix: searching by `undefined`

* fix: searching for undefined on invoices, table placeholder centering

* refactor: abstract users list actions into new component

* refactor: move styled components to top of files
2022-05-31 07:59:09 +01:00

51 lines
1.4 KiB
TypeScript

import { IUser } from 'interfaces/user';
import { useMemo } from 'react';
import { useInstanceStatus } from './api/getters/useInstanceStatus/useInstanceStatus';
import { STRIPE } from 'component/admin/billing/flags';
import { InstancePlan } from 'interfaces/instance';
export interface IUsersPlanOutput {
planUsers: IUser[];
isBillingUsers: boolean;
}
export const useUsersPlan = (users: IUser[]): IUsersPlanOutput => {
const { instanceStatus } = useInstanceStatus();
const isBillingUsers = STRIPE && instanceStatus?.plan === InstancePlan.PRO;
const seats = instanceStatus?.seats ?? 5;
const planUsers = useMemo(
() => calculatePaidUsers(users, isBillingUsers, seats),
[users, isBillingUsers, seats]
);
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;
};