Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { useState } from "react";
import { LucideIcon, MailIcon, UserIcon, UsersIcon } from "lucide-react";
import { Combobox } from "@app/components/v3";
import { useOrganization } from "@app/context";
import { isValidEmail } from "@app/helpers/email";
import {
useGetOrganizationGroups,
useGetOrgUsers,
useGetWorkspaceUsers,
useListWorkspaceGroups
} from "@app/hooks/api";
import { AlertPrincipalType, TAlertChannelRecipient } from "@app/hooks/api/alerts";
type RecipientOption = {
principalType: AlertPrincipalType;
principalId: string;
label: string;
groupLabel: "Project" | "Users" | "Groups" | "Emails";
};
const projectMembersOption = (projectId: string): RecipientOption => ({
principalType: AlertPrincipalType.ProjectMembers,
principalId: projectId,
label: "All Project Members",
groupLabel: "Project"
});
const userLabel = (user: { firstName?: string; lastName?: string; username?: string }): string =>
user.username || `${user.firstName ?? ""} ${user.lastName ?? ""}`.trim() || "Unknown user";
const buildOptions = (
users: { user: { id: string; firstName?: string; lastName?: string; username?: string } }[],
groups: { id: string; name: string }[]
): RecipientOption[] => [
...users.map((membership) => ({
principalType: AlertPrincipalType.User,
principalId: membership.user.id,
label: userLabel(membership.user),
groupLabel: "Users" as const
})),
...groups.map((group) => ({
principalType: AlertPrincipalType.Group,
principalId: group.id,
label: group.name,
groupLabel: "Groups" as const
}))
];
const RECIPIENT_ICONS: Partial<Record<AlertPrincipalType, LucideIcon>> = {
[AlertPrincipalType.User]: UserIcon,
[AlertPrincipalType.Email]: MailIcon
};
const formatOptionLabel = (option: RecipientOption) => {
const Icon = RECIPIENT_ICONS[option.principalType] ?? UsersIcon;
return (
<span className="flex items-center gap-2">
<Icon className="size-3.5 text-muted" />
{option.label}
</span>
);
};
type SelectProps = {
value: TAlertChannelRecipient[];
onChange: (recipients: TAlertChannelRecipient[]) => void;
isError?: boolean;
allowEmailAddresses?: boolean;
};
const RecipientSelect = ({
options,
labelledOptions,
value,
onChange,
isError,
allowEmailAddresses
}: SelectProps & { options: RecipientOption[]; labelledOptions?: RecipientOption[] }) => {
const [inputValue, setInputValue] = useState("");
const byKey = new Map(
(labelledOptions ?? options).map((o) => [`${o.principalType}-${o.principalId}`, o])
);
const email = inputValue.trim().toLowerCase();
const canAddEmail =
allowEmailAddresses &&
isValidEmail(email) &&
!value.some((recipient) => recipient.principalId === email);
const emailOption: RecipientOption = {
principalType: AlertPrincipalType.Email,
principalId: email,
label: email,
groupLabel: "Emails"
};
const selected = value.map(
(recipient): RecipientOption =>
byKey.get(`${recipient.principalType}-${recipient.principalId}`) ?? {
principalType: recipient.principalType,
principalId: recipient.principalId,
label: recipient.principalId,
groupLabel: recipient.principalType === AlertPrincipalType.Group ? "Groups" : "Users"
}
);
return (
<Combobox
multiple
placeholder={
allowEmailAddresses ? "Add users, groups, or emails..." : "Add users or groups..."
}
searchAriaLabel="Search recipients"
options={canAddEmail ? [...options, emailOption] : options}
onInputValueChange={setInputValue}
value={selected}
includeMissingSelectedOptions={false}
isError={isError}
getOptionGroup={(option) => option.groupLabel}
getOptionValue={(option) => `${option.principalType}-${option.principalId}`}
getOptionKeywords={(option) => [`${option.principalType}-${option.principalId}`]}
getOptionLabel={(option) => option.label}
renderOption={formatOptionLabel}
renderValue={formatOptionLabel}
onValueChange={(newValue) =>
onChange(
newValue.map((option) => ({
principalType: option.principalType,
principalId: option.principalId
}))
)
}
/>
);
};
export const canReceiveAlerts = (membership: { isActive: boolean; status: string }) =>
membership.isActive && membership.status !== "invited";
const OrgRecipientSelect = ({ orgId, ...props }: SelectProps & { orgId: string }) => {
const { data: users = [] } = useGetOrgUsers(orgId);
const { data: groups = [] } = useGetOrganizationGroups(orgId);
const eligibleUsers = users.filter(canReceiveAlerts);
return (
<RecipientSelect
options={buildOptions(eligibleUsers, groups)}
labelledOptions={buildOptions(users, groups)}
{...props}
/>
);
};
const ProjectRecipientSelect = ({
projectId,
orgId,
...props
}: SelectProps & { projectId: string; orgId: string }) => {
const { data: users = [] } = useGetWorkspaceUsers(projectId, true);
const { data: orgUsers = [] } = useGetOrgUsers(orgId);
const { data: groups = [] } = useListWorkspaceGroups(projectId);
const blockedUserIds = new Set(
orgUsers
.filter((membership) => !canReceiveAlerts(membership))
.map((membership) => membership.user.id)
);
const eligibleUsers = users.filter(
(membership) => membership.user.isOrgMembershipActive && !blockedUserIds.has(membership.user.id)
);
const groupOptions = groups.map((membership) => membership.group);
const everyone = projectMembersOption(projectId);
return (
<RecipientSelect
options={[everyone, ...buildOptions(eligibleUsers, groupOptions)]}
labelledOptions={[everyone, ...buildOptions(users, groupOptions)]}
{...props}
/>
);
};
type Props = SelectProps & { projectId?: string };
export const ChannelRecipientsField = ({ projectId, ...props }: Props) => {
const { currentOrg } = useOrganization();
return projectId ? (
<ProjectRecipientSelect projectId={projectId} orgId={currentOrg.id} {...props} />
) : (
<OrgRecipientSelect orgId={currentOrg.id} {...props} />
);
};