Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(console,phrases): improve invite email input #5661

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { emailRegEx } from '@logto/core-kit';
import { OrganizationInvitationStatus } from '@logto/schemas';
import { conditional, conditionalArray, conditionalString } from '@silverhand/essentials';
import { useCallback, useContext } from 'react';
import { useTranslation } from 'react-i18next';
import useSWR from 'swr';

import { useAuthedCloudApi } from '@/cloud/hooks/use-cloud-api';
import { type TenantMemberResponse } from '@/cloud/types/router';
import { type TenantInvitationResponse, type TenantMemberResponse } from '@/cloud/types/router';
import { TenantsContext } from '@/contexts/TenantsProvider';
import { type RequestError } from '@/hooks/use-api';

Expand All @@ -22,6 +23,12 @@ const useEmailInputUtils = () => {
cloudApi.get('/api/tenants/:tenantId/members', { params: { tenantId: currentTenantId } })
);

const { data: existingInvitations = [] } = useSWR<TenantInvitationResponse[], RequestError>(
'api/tenants/:tenantId/invitations',
async () =>
cloudApi.get('/api/tenants/:tenantId/invitations', { params: { tenantId: currentTenantId } })
);

/**
* Find duplicated and invalid formatted email addresses.
*
Expand All @@ -31,29 +38,44 @@ const useEmailInputUtils = () => {
const findDuplicatedOrInvalidEmails = useCallback(
(emails: string[] = []) => {
const duplicatedEmails = new Set<string>();
const conflictMemberEmails = new Set<string>();
const conflictInvitationEmails = new Set<string>();
const invalidEmails = new Set<string>();
const validEmails = new Set<string>(
const validEmails = new Set<string>();

const existingMemberEmails = new Set<string>(
existingMembers.map(({ primaryEmail }) => primaryEmail ?? '').filter(Boolean)
);
const existingInvitationEmails = new Set<string>(
existingInvitations
.filter(({ status }) => status === OrganizationInvitationStatus.Pending)
.map(({ invitee }) => invitee)
);

for (const email of emails) {
if (!emailRegEx.test(email)) {
invalidEmails.add(email);
}

// Check email collisions
if (validEmails.has(email)) {
duplicatedEmails.add(email);
} else if (existingInvitationEmails.has(email)) {
conflictInvitationEmails.add(email);
} else if (existingMemberEmails.has(email)) {
conflictMemberEmails.add(email);
} else {
validEmails.add(email);
}
}

return {
duplicatedEmails,
conflictMemberEmails,
conflictInvitationEmails,
invalidEmails,
};
},
[existingMembers]
[existingInvitations, existingMembers]
);

const parseEmailOptions = useCallback(
Expand All @@ -63,30 +85,44 @@ const useEmailInputUtils = () => {
values: InviteeEmailItem[];
errorMessage?: string;
} => {
const { duplicatedEmails, invalidEmails } = findDuplicatedOrInvalidEmails(
inputValues.map((email) => email.value)
);
const { duplicatedEmails, conflictInvitationEmails, conflictMemberEmails, invalidEmails } =
findDuplicatedOrInvalidEmails(inputValues.map((email) => email.value));
// Show error message and update the inputs' status for error display.
if (duplicatedEmails.size > 0 || invalidEmails.size > 0) {
if (
duplicatedEmails.size > 0 ||
conflictInvitationEmails.size > 0 ||
conflictMemberEmails.size > 0 ||
invalidEmails.size > 0
) {
return {
values: inputValues.map(({ status, ...rest }) => ({
...rest,
...conditional(
(duplicatedEmails.has(rest.value) || invalidEmails.has(rest.value)) && {
status: 'info',
(duplicatedEmails.has(rest.value) ||
conflictInvitationEmails.has(rest.value) ||
conflictMemberEmails.has(rest.value) ||
invalidEmails.has(rest.value)) && {
status: 'error',
}
),
})),
errorMessage: conditionalArray(
conditionalString(duplicatedEmails.size > 0 && t('tenant_members.errors.user_exists')),
conditionalString(duplicatedEmails.size > 0 && t('tenant_members.errors.email_exists')),
conditionalString(
conflictInvitationEmails.size > 0 &&
t('tenant_members.errors.pending_invitation_exists')
),
conditionalString(
conflictMemberEmails.size > 0 && t('tenant_members.errors.member_exists')
),
conditionalString(invalidEmails.size > 0 && t('tenant_members.errors.invalid_email'))
).join(' '),
).join('\n'),
};
}

return { values: inputValues };
},
[findDuplicatedOrInvalidEmails]
[findDuplicatedOrInvalidEmails, t]
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
background: var(--color-overlay-default-focused);
}

&.info {
&.error {
background: var(--color-error-container);
}
}
Expand Down Expand Up @@ -91,4 +91,5 @@ canvas {
font: var(--font-body-2);
color: var(--color-error);
margin-top: _.unit(1);
white-space: pre-wrap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function InviteEmailsInput({
{
value,
id: generateStandardShortId(),
...conditional(!emailRegEx.test(value) && { status: 'info' }),
...conditional(!emailRegEx.test(value) && { status: 'error' }),
},
];
onChange(newValues);
Expand Down Expand Up @@ -129,7 +129,7 @@ function InviteEmailsInput({
ref={ref}
placeholder={conditional(values.length === 0 && placeholder)}
value={currentValue}
style={{ minWidth: `${minInputWidth}px` }}
style={{ minWidth: `${minInputWidth + 10}px` }}
onKeyDown={(event) => {
if (event.key === 'Backspace' && currentValue === '') {
if (focusedValueId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type InviteeEmailItem = {
* - undefined: valid email
* - 'info': duplicated email or invalid email format.
*/
status?: Extract<TagProps['status'], 'info'>;
status?: Extract<TagProps['status'], 'error'>;
};

export type InviteMemberForm = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ const tenant_members = {
},
errors: {
email_required: 'Invitee email is required.',
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
member_exists: 'This user is already a member of this organization.',
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
max_member_limit: 'You have reached the maximum number of members ({{limit}}) for this tenant.',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ const tenant_members = {
/** UNTRANSLATED */
email_required: 'Invitee email is required.',
/** UNTRANSLATED */
user_exists: 'This user is already invited to this organization.',
email_exists: 'Email address already exists.',
/** UNTRANSLATED */
member_exists: 'This user is already a member of this organization.',
/** UNTRANSLATED */
pending_invitation_exists:
'Pending invitation exists. Delete related email or revoke the invitation.',
/** UNTRANSLATED */
invalid_email: 'Email address is invalid. Please make sure it is in the right format.',
/** UNTRANSLATED */
Expand Down
Loading