Skip to content

The open alternative to Salesforce, designed for AI.

3.8 KB
Raw Blame
import { useLingui } from '@lingui/react/macro';
import { useCallback, useState } from 'react';
import { z } from 'zod';

import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath } from 'twenty-shared/utils';

import { useCreateEmailGroupChannel } from '@/settings/accounts/hooks/useCreateEmailGroupChannel';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';
import { SettingsPageLayout } from '@/settings/components/layout/SettingsPageLayout';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
import { Section } from 'twenty-ui/components/layout';

export const SettingsAccountsNewEmailGroupChannel = () => {
  const { t } = useLingui();
  const navigate = useNavigateSettings();
  const { createEmailGroupChannel, loading } = useCreateEmailGroupChannel();

  const [handle, setHandle] = useState('');
  const [displayName, setDisplayName] = useState('');

  const isHandleValidEmail = z.email().safeParse(handle).success;
  const canSave = isHandleValidEmail && !loading;

  const handleSave = useCallback(async () => {
    const trimmedDisplayName = displayName.trim();
    const result = await createEmailGroupChannel(
      handle,
      trimmedDisplayName.length > 0 ? trimmedDisplayName : undefined,
    );
    const messageChannelId =
      result.data?.createEmailGroupChannel.messageChannel.id;

    if (messageChannelId) {
      navigate(SettingsPath.EmailGroupChannelDetail, {
        messageChannelId,
      });
    }
  }, [createEmailGroupChannel, displayName, handle, navigate]);

  return (
    <SettingsPageLayout
      title={t`New Email Channel`}
      links={[
        {
          children: t`Workspace`,
          href: getSettingsPath(SettingsPath.General),
        },
        {
          children: t`Communication`,
          href: getSettingsPath(SettingsPath.WorkspaceCommunications),
        },
        { children: t`New Email Channel` },
      ]}
      actionButton={
        <SaveAndCancelButtons
          isSaveDisabled={!canSave}
          isCancelDisabled={loading}
          isLoading={loading}
          onCancel={() => navigate(SettingsPath.WorkspaceCommunications)}
          onSave={handleSave}
        />
      }
    >
      <SettingsPageContainer>
        <Section.Root>
          <Section.Header
            title={t`Email Address`}
            description={t`The address your workspace will send and receive email from (e.g. [email protected]). Outbound sending requires the domain to be verified in Outbound Domains.`}
          />
          <SettingsTextInput
            instanceId="email-group-source"
            label={t`Source Email Address`}
            placeholder="[email protected]"
            value={handle}
            onChange={setHandle}
            onInputEnter={() => {
              if (canSave) {
                handleSave();
              }
            }}
            disabled={loading}
          />
        </Section.Root>
        <Section.Root>
          <Section.Header
            title={t`Display Name`}
            description={t`The name recipients see next to your address in their inbox, instead of the address alone.`}
          />
          <SettingsTextInput
            instanceId="email-group-display-name"
            label={t`Display Name`}
            placeholder={t`Support Team`}
            value={displayName}
            maxLength={255}
            onChange={setDisplayName}
            onInputEnter={() => {
              if (canSave) {
                handleSave();
              }
            }}
            disabled={loading}
          />
        </Section.Root>
      </SettingsPageContainer>
    </SettingsPageLayout>
  );
};