use-inbox-channel-connect.ts1.8 KBView on GitHub
import { useLinkedinConnect } from '@/modules/integrations/use-linkedin-connect';
import { useWhatsappConnect } from '@/modules/integrations/use-whatsapp-connect';
import type { InboxChannelFilter } from '@/modules/inbox/types';

export interface InboxChannelConnect {
  channel: 'linkedin' | 'whatsapp';
  /** Number of connected seats/lines for this channel. */
  connectedCount: number;
  /** True once the accounts query has resolved (so we don't flash the prompt). */
  accountsLoaded: boolean;
  isConnecting: boolean;
  /** Kick off the Unipile hosted-auth flow for this channel. */
  connect: () => void;
}

/**
 * For the LinkedIn/WhatsApp inbox filters, expose whether the channel is connected
 * and a one-tap connect action. Returns null for channels that don't gate on a
 * Unipile connection (email/slack/all). Both underlying account queries are gated
 * by `enabled`, so only the active channel is fetched. WhatsApp defaults to a
 * `shared` line (the rep's everyday number) — the settings card is where a
 * dedicated line is chosen.
 */
export function useInboxChannelConnect(channel: InboxChannelFilter): InboxChannelConnect | null {
  const linkedin = useLinkedinConnect({ enabled: channel === 'linkedin' });
  const whatsapp = useWhatsappConnect({ enabled: channel === 'whatsapp' });

  if (channel === 'linkedin') {
    return {
      channel,
      connectedCount: linkedin.connectedCount,
      accountsLoaded: linkedin.accountsLoaded,
      isConnecting: linkedin.isConnecting,
      connect: linkedin.handleConnect,
    };
  }
  if (channel === 'whatsapp') {
    return {
      channel,
      connectedCount: whatsapp.connectedCount,
      accountsLoaded: whatsapp.accountsLoaded,
      isConnecting: whatsapp.isConnecting,
      connect: () => whatsapp.handleConnect('shared'),
    };
  }
  return null;
}