use-self-emails.ts1.1 KBView on GitHub
import { useConnections } from '@/hooks/use-connections';
import { useEmailAliases } from '@/hooks/use-email-aliases';
import { useMemo } from 'react';

/**
 * Every email address that belongs to the current user — all connected mailboxes plus
 * the active connection's send-as aliases — lowercased and de-duplicated.
 *
 * Reply-recipient computation must exclude the user's own identities so a user is never
 * added as a recipient of their own reply. A single active-connection email is not enough:
 * users can connect multiple mailboxes (e.g. a primary account plus a separate sending
 * domain), and any identity not in this set leaks into the To/Cc of replies.
 */
export function useSelfEmails(): string[] {
  const { data: connections } = useConnections();
  const { data: aliases } = useEmailAliases();

  return useMemo(() => {
    const emails = new Set<string>();
    for (const conn of connections?.connections ?? []) {
      if (conn.email) emails.add(conn.email.toLowerCase());
    }
    for (const alias of aliases ?? []) {
      if (alias.email) emails.add(alias.email.toLowerCase());
    }
    return [...emails];
  }, [connections, aliases]);
}