'use client'
import { useState } from 'react'
import { CheckIcon, CopyIcon, LinkIcon, MessageCircleIcon } from 'lucide-react'
import type { NucleusShareRecipientsResult } from '@/app/(campaign)/campanha/(app)/nucleos/[slug]/shareRecipientsActions'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert'
import { Avatar, AvatarFallback } from '@/components/ui/Avatar'
import { Button } from '@/components/ui/button'
import {
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Spinner } from '@/components/ui/Spinner'
import type { NucleusShareRecipient } from '@/utilities/nucleusShareRecipients'
import { buildWhatsAppUrl } from '@/utilities/phone'
export const buildNucleusShareMessage = ({
recipientName,
senderName,
nucleusName,
nucleusUrl,
}: {
recipientName: string
senderName: string
nucleusName: string
nucleusUrl: string
}): string =>
`Oi ${recipientName}, aqui é ${senderName} da campanha do Solla. Veja o núcleo ${nucleusName}: ${nucleusUrl}`
const getInitials = (name: string): string => {
const words = name.trim().split(/\s+/).filter(Boolean)
return words
.slice(0, 2)
.map((word) => word[0])
.join('')
.toLocaleUpperCase('pt-BR')
}
const RecipientRow = ({
recipient,
roleLabel,
onOpenWhatsApp,
}: {
recipient: NucleusShareRecipient
roleLabel: string
onOpenWhatsApp: (recipient: NucleusShareRecipient) => void
}) => (
{getInitials(recipient.name)}
{recipient.name}
{roleLabel}
)
const RecipientSection = ({
title,
roleLabel,
recipients,
onOpenWhatsApp,
}: {
title: string
roleLabel: string
recipients: NucleusShareRecipient[]
onOpenWhatsApp: (recipient: NucleusShareRecipient) => void
}) => {
if (recipients.length === 0) return null
return (
{title}
{recipients.map((recipient) => (
))}
)
}
export const ShareNucleusDialog = ({
data,
loadError,
loading,
nucleusName,
onRetry,
senderName,
}: {
data: NucleusShareRecipientsResult | null
loadError: boolean
loading: boolean
nucleusName: string
onRetry: () => void
senderName: string
}) => {
const [copied, setCopied] = useState(false)
const [copyError, setCopyError] = useState(null)
const openWhatsApp = (recipient: NucleusShareRecipient) => {
if (!data) return
const message = buildNucleusShareMessage({
recipientName: recipient.name,
senderName,
nucleusName,
nucleusUrl: data.nucleusUrl,
})
window.open(buildWhatsAppUrl(recipient.phone, message), '_blank', 'noopener,noreferrer')
}
const copyLink = async () => {
if (!data) return
setCopyError(null)
try {
await navigator.clipboard.writeText(data.nucleusUrl)
setCopied(true)
} catch {
setCopyError('Não foi possível copiar o link. Selecione e copie manualmente.')
}
}
return (
Compartilhar núcleo
{nucleusName} · envia apenas o link, não concede acesso
{loading ? (
) : null}
{loadError ? (
Não foi possível carregar
Não foi possível listar os destinatários. Tente novamente.
) : null}
{copyError ? (
Cópia indisponível
{copyError}
) : null}
{data && !loading ? (
Copiar link
{data.nucleusUrl}
) : null}
)
}