83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import type { TenantUser } from "@hivegrid/db/prisma-browser";
|
|
import { TenantUserType } from "~/application/enums/tenants/TenantUserType";
|
|
|
|
export const avatarText = (user: { firstName: string | null; lastName: string | null; email: string }): string => {
|
|
if (user) {
|
|
if (user.firstName && user.lastName) {
|
|
if (user.firstName.length > 0 && user.lastName.length > 0) {
|
|
return (user.firstName[0] + user.lastName[0]).toUpperCase();
|
|
} else if (user.firstName.length > 1) {
|
|
return user.firstName.substring(0, 2).toUpperCase();
|
|
} else if (user.email.length > 1) {
|
|
return user.email.substring(0, 2).toUpperCase();
|
|
}
|
|
} else if (user.firstName) {
|
|
return user.firstName.substring(0, 2).toUpperCase();
|
|
} else if (user.email) {
|
|
return user.email.substring(0, 2).toUpperCase();
|
|
}
|
|
}
|
|
return "--";
|
|
};
|
|
|
|
export function getTenantPrefix(item: { name: string }) {
|
|
const words = item.name.split(" ");
|
|
if (words.length > 1) {
|
|
return (words[0].substring(0, 1) + words[1].substring(0, 1)).toUpperCase();
|
|
}
|
|
if (item.name.length > 1) {
|
|
return item.name.substring(0, 2).toUpperCase();
|
|
}
|
|
return item.name.substring(0, 1).toUpperCase();
|
|
}
|
|
|
|
export const profileName = (
|
|
user: { firstName: string | null; lastName: string | null; email: string | null } | null | undefined,
|
|
options: { email?: boolean } = {}
|
|
): string => {
|
|
let profile = "";
|
|
if (user) {
|
|
if (user.firstName && user.lastName) {
|
|
profile = user.firstName + " " + user.lastName;
|
|
} else if (user.firstName) {
|
|
profile = user.firstName;
|
|
} else {
|
|
profile = user.email || "";
|
|
}
|
|
}
|
|
if (options.email && (user?.firstName || user?.lastName) && user?.email) {
|
|
return profile + " (" + user?.email + ")";
|
|
}
|
|
return profile;
|
|
};
|
|
|
|
export const getUserRoleClass = (item: TenantUser) => {
|
|
switch (item.type as TenantUserType) {
|
|
case TenantUserType.OWNER:
|
|
return "bg-secondary text-foreground border border-border";
|
|
case TenantUserType.ADMIN:
|
|
return "bg-rose-50 border border-rose-200";
|
|
case TenantUserType.MEMBER:
|
|
return "bg-blue-50 border border-blue-200";
|
|
}
|
|
};
|
|
|
|
export const fullName = (user: { firstName: string | null; lastName: string | null; email: string } | null): string => {
|
|
if (user) {
|
|
if (user.firstName && user.lastName) {
|
|
return user.firstName + " " + user.lastName;
|
|
} else {
|
|
return user.firstName ?? "";
|
|
}
|
|
}
|
|
return "";
|
|
};
|
|
|
|
export default {
|
|
avatarText,
|
|
profileName,
|
|
getTenantPrefix,
|
|
getUserRoleClass,
|
|
fullName,
|
|
};
|