-
Notifications
You must be signed in to change notification settings - Fork 0
feat(crm): Fase 7 — criar/editar/excluir contato, sincronização com W… #55
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
Merged
sejaanomalo-web
merged 1 commit into
main
from
claude/crm-fase7-contatos-crud-whatsapp-sync
Jul 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| "use client" | ||
|
|
||
| import { useState, useTransition } from "react" | ||
| import { useRouter } from "next/navigation" | ||
| import { criarLeadManualAction } from "@/lib/crm-leads-actions" | ||
| import type { CrmInstanciaRow } from "@/lib/crm-instancias-actions" | ||
|
|
||
| /** Espelha normalizarTelefone de lib/crm-leads-actions.ts — só pra pré-visualização | ||
| * no form (a normalização de verdade acontece no server). */ | ||
| function previaTelefone(bruto: string): string { | ||
| const digitos = bruto.replace(/\D/g, "") | ||
| if (digitos.length < 8) return "número muito curto" | ||
| if (digitos.length <= 11) return `55${digitos}` | ||
| return digitos | ||
| } | ||
|
|
||
| /** | ||
| * Botão "+ Novo contato" da sidebar de Conversas — cria um lead manual | ||
| * (fora do fluxo de mensagem recebida). O contato passa a existir no CRM na | ||
| * hora; só vira uma conversa de verdade no WhatsApp do celular quando a | ||
| * PRIMEIRA mensagem é mandada (por isso já leva direto pra thread dele ao | ||
| * salvar — é só digitar e enviar). | ||
| */ | ||
| export default function NovoContato({ | ||
| instancias, | ||
| }: { | ||
| instancias: CrmInstanciaRow[] | ||
| }) { | ||
| const ativas = instancias.filter((i) => i.ativo) | ||
| const [aberto, setAberto] = useState(false) | ||
| const [instanciaId, setInstanciaId] = useState(ativas[0]?.id ?? "") | ||
| const [nome, setNome] = useState("") | ||
| const [telefone, setTelefone] = useState("") | ||
| const [email, setEmail] = useState("") | ||
| const [erro, setErro] = useState<string | null>(null) | ||
| const [pending, startTransition] = useTransition() | ||
| const router = useRouter() | ||
|
|
||
| function fechar() { | ||
| setAberto(false) | ||
| setNome("") | ||
| setTelefone("") | ||
| setEmail("") | ||
| setErro(null) | ||
| } | ||
|
|
||
| function salvar() { | ||
| if (!instanciaId) { | ||
| setErro("Conecte um número em Conexões antes de criar um contato.") | ||
| return | ||
| } | ||
| if (!nome.trim() || !telefone.trim()) { | ||
| setErro("Preencha nome e telefone.") | ||
| return | ||
| } | ||
| startTransition(async () => { | ||
| const fd = new FormData() | ||
| fd.set("instancia_id", instanciaId) | ||
| fd.set("nome", nome.trim()) | ||
| fd.set("telefone", telefone.trim()) | ||
| fd.set("email", email.trim()) | ||
| const r = await criarLeadManualAction(fd) | ||
| if (!r.ok) { | ||
| setErro(r.erro ?? "Erro ao criar contato") | ||
| return | ||
| } | ||
| fechar() | ||
| if (r.id) router.push(`/dashboard/crm?view=conversas&lead=${r.id}`) | ||
| router.refresh() | ||
| }) | ||
| } | ||
|
|
||
| if (ativas.length === 0) return null | ||
|
|
||
| return ( | ||
| <div style={{ position: "relative" }}> | ||
| <button | ||
| type="button" | ||
| onClick={() => setAberto((v) => !v)} | ||
| style={{ | ||
| fontSize: 11, | ||
| fontWeight: 600, | ||
| color: "var(--gold, #C9953A)", | ||
| padding: "4px 8px", | ||
| }} | ||
| > | ||
| + Novo contato | ||
| </button> | ||
|
|
||
| {aberto && ( | ||
| <> | ||
| <button | ||
| type="button" | ||
| aria-label="Fechar" | ||
| onClick={fechar} | ||
| style={{ position: "fixed", inset: 0, zIndex: 15, cursor: "default" }} | ||
| /> | ||
| <div | ||
| className="glass" | ||
| style={{ | ||
| position: "absolute", | ||
| top: "100%", | ||
| left: 0, | ||
| marginTop: 6, | ||
| padding: 12, | ||
| width: 260, | ||
| zIndex: 20, | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| gap: 8, | ||
| }} | ||
| > | ||
| <p | ||
| style={{ | ||
| fontSize: 9, | ||
| letterSpacing: "1.5px", | ||
| textTransform: "uppercase", | ||
| color: "var(--text-4)", | ||
| fontWeight: 500, | ||
| }} | ||
| > | ||
| Novo contato | ||
| </p> | ||
|
|
||
| {ativas.length > 1 && ( | ||
| <select | ||
| value={instanciaId} | ||
| onChange={(e) => setInstanciaId(e.target.value)} | ||
| className="glass-input" | ||
| style={{ fontSize: 12, padding: "6px 8px" }} | ||
| > | ||
| {ativas.map((i) => ( | ||
| <option key={i.id} value={i.id}> | ||
| {i.display_nome ?? i.empresa_slug} | ||
| {i.numero_e164 ? ` · ${i.numero_e164}` : ""} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| )} | ||
|
|
||
| <input | ||
| value={nome} | ||
| onChange={(e) => setNome(e.target.value)} | ||
| placeholder="Nome" | ||
| maxLength={80} | ||
| autoFocus | ||
| className="glass-input" | ||
| style={{ fontSize: 12, padding: "6px 8px" }} | ||
| /> | ||
| <input | ||
| value={telefone} | ||
| onChange={(e) => setTelefone(e.target.value)} | ||
| placeholder="Telefone (com DDD)" | ||
| inputMode="tel" | ||
| maxLength={20} | ||
| className="glass-input" | ||
| style={{ fontSize: 12, padding: "6px 8px" }} | ||
| /> | ||
| {/* Sem DDI (10/11 dígitos) ganha o 55 na frente — mostra o | ||
| resultado pra dar chance de corrigir um número que já veio | ||
| com código de país diferente (ex: número dos EUA também tem | ||
| 11 dígitos e seria confundido com um BR sem DDI). */} | ||
| {telefone.trim() && ( | ||
| <p style={{ fontSize: 10, color: "var(--text-4)", marginTop: -4 }}> | ||
| Vai salvar como: {previaTelefone(telefone)} | ||
| </p> | ||
| )} | ||
| <input | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| placeholder="E-mail (opcional)" | ||
| type="email" | ||
| maxLength={120} | ||
| className="glass-input" | ||
| style={{ fontSize: 12, padding: "6px 8px" }} | ||
| /> | ||
|
|
||
| {erro && <p style={{ fontSize: 11, color: "var(--danger)" }}>{erro}</p>} | ||
|
|
||
| <div className="flex items-center gap-2"> | ||
| <button | ||
| type="button" | ||
| onClick={salvar} | ||
| disabled={pending} | ||
| className="btn-gold-filled" | ||
| style={{ fontSize: 11, padding: "5px 10px", opacity: pending ? 0.5 : 1 }} | ||
| > | ||
| {pending ? "Criando..." : "Criar"} | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={fechar} | ||
| style={{ fontSize: 11, color: "var(--text-3)" }} | ||
| > | ||
| Cancelar | ||
| </button> | ||
| </div> | ||
|
|
||
| <p style={{ fontSize: 10, color: "var(--text-4)" }}> | ||
| Só vira conversa de verdade no WhatsApp do celular depois que você | ||
| mandar a primeira mensagem. | ||
| </p> | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
r.avisois never surfaced to the user — the WhatsApp-existence warning is silently dropped.criarLeadManualActionreturns anavisofield ("Esse número não parece estar no WhatsApp — confira antes de enviar.") specifically to warn the user best-effort, butsalvar()only checksr.ok/r.idand immediately closes the popover and navigates away. The entire verification feature described in the PR (warn before sending to a non-WhatsApp number) has no visible effect for the user.🐛 Proposed fix
avisowould then need to be rendered somewhere persistent (e.g. a toast, or held in the thread view via a query param) since the popover closes right afterfechar().🤖 Prompt for AI Agents