1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/hooks/useUsersPlan.ts
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

61 lines
1.6 KiB
TypeScript

import { IUser } from 'interfaces/user';
import { useMemo } from 'react';
import { useInstanceStatus } from './api/getters/useInstanceStatus/useInstanceStatus';
import { InstancePlan } from 'interfaces/instance';
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
export interface IUsersPlanOutput {
planUsers: IUser[];
isBillingUsers: boolean;
seats: number;
extraSeats: number;
}
export const useUsersPlan = (users: IUser[]): IUsersPlanOutput => {
const { instanceStatus } = useInstanceStatus();
const { uiConfig } = useUiConfig();
const isBillingUsers = Boolean(
uiConfig?.flags?.proPlanAutoCharge &&
instanceStatus?.plan === InstancePlan.PRO,
);
const seats = instanceStatus?.seats ?? 5;
const planUsers = useMemo(
() => calculatePaidUsers(users, isBillingUsers, seats),
[users, isBillingUsers, seats],
);
const extraSeats = planUsers.filter((user) => user.paid).length;
return {
seats,
extraSeats,
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;
};