@fayz-ai/plugin-conversations 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
- import { ConversationsContextProvider, useConversationsStore, assinarChegada, useConversationsConfig, CHANNEL_LABELS, isWaitingOnUs, MessagePreview, mediaPreviewKind } from './chunk-VZLFELO6.js';
1
+ import { ConversationsContextProvider, useConversationsStore, assinarChegada, useConversationsConfig, CHANNEL_LABELS, isWaitingOnUs, MessagePreview, mediaPreviewKind } from './chunk-NXYQFOT7.js';
2
2
  import { providersApi } from './chunk-HMWUU5O7.js';
3
3
  import * as React9 from 'react';
4
4
  import React9__default from 'react';
5
- import { SquarePen, MessageSquare, Globe, Mail, Instagram, Phone, Search, User, Inbox, CheckCheck, ChevronLeft, Archive, PanelRight, Send, X, MapPin, Tag, StickyNote, Link2, CheckCircle2, MoreVertical, Loader2, Forward, AlertTriangle, Check, Clock, ExternalLink, Reply, Snowflake, Sun, Flame, Building2, Ban, BarChart3, UserRound, Copy, FileText, Download } from 'lucide-react';
6
- import { PageHeaderActions, cn, Button, Modal, ModalContent, ModalHeader, ModalTitle, ModalBody, Textarea, ModalFooter, Input, FilterChip, Skeleton, toast } from '@fayz-ai/ui';
5
+ import { SquarePen, MessageSquare, Globe, Mail, Instagram, Phone, Search, User, Archive, Inbox, CheckCheck, ChevronLeft, MoreHorizontal, ArchiveRestore, Send, X, Building2, CheckCircle2, MoreVertical, Loader2, Forward, AlertTriangle, Check, Clock, ExternalLink, Reply, Snowflake, Sun, Flame, Ban, BarChart3, MapPin, UserRound, Copy, FileText, Download } from 'lucide-react';
6
+ import { PageHeaderActions, cn, Button, Modal, ModalContent, ModalHeader, ModalTitle, ModalBody, Textarea, ModalFooter, Input, FilterChip, Skeleton, Dropdown, DropdownTrigger, DropdownContent, DropdownItem, toast } from '@fayz-ai/ui';
7
7
  import { useRightRailStore, PageActions, useLimitGuard, ContactPicker, invalidateLimit, useConnectors, adminNavigateTo } from '@fayz-ai/admin';
8
- import { currentAppPath, appHref, useTranslation, useActiveTenantId, errorMessage, tl, getSupabaseClientOptional, getActiveTenantId } from '@fayz-ai/core';
8
+ import { currentAppPath, appHref, onAppPathChange, replaceApp, useTranslation, useActiveTenantId, errorMessage, tl, getSupabaseClientOptional, getActiveTenantId } from '@fayz-ai/core';
9
9
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
10
10
 
11
11
  var CHANNEL_ICON = {
@@ -178,6 +178,11 @@ function ConversationList({ className }) {
178
178
  setLeadOwnerFilter,
179
179
  setSearch
180
180
  } = useConversationsStore((s) => s);
181
+ const [archived, setArchived] = React9__default.useState(false);
182
+ const shown = React9__default.useMemo(
183
+ () => conversations.filter((c) => archived ? c.status === "closed" : c.status !== "closed"),
184
+ [conversations, archived]
185
+ );
181
186
  const donos = React9__default.useMemo(() => {
182
187
  const vistos = /* @__PURE__ */ new Map();
183
188
  for (const c of conversations) {
@@ -217,19 +222,40 @@ function ConversationList({ className }) {
217
222
  id
218
223
  );
219
224
  }) }),
220
- donos.length > 0 && /* @__PURE__ */ jsx("div", { className: "mt-2", children: /* @__PURE__ */ jsx(
221
- FilterChip,
222
- {
223
- label: t("conversations.contact.leadOwner"),
224
- icon: User,
225
- allLabel: t("conversations.filter.anyOwner"),
226
- options: [{ value: "none", label: t("conversations.filter.noOwner") }, ...donos],
227
- value: leadOwnerFilter,
228
- onChange: (v) => {
229
- void setLeadOwnerFilter(v);
225
+ /* @__PURE__ */ jsxs("div", { className: "mt-2 flex flex-wrap items-center gap-1", children: [
226
+ donos.length > 0 && /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(
227
+ FilterChip,
228
+ {
229
+ label: t("conversations.contact.leadOwner"),
230
+ icon: User,
231
+ allLabel: t("conversations.filter.anyOwner"),
232
+ options: [{ value: "none", label: t("conversations.filter.noOwner") }, ...donos],
233
+ value: leadOwnerFilter,
234
+ onChange: (v) => {
235
+ void setLeadOwnerFilter(v);
236
+ }
230
237
  }
231
- }
232
- ) })
238
+ ) }),
239
+ /* @__PURE__ */ jsxs(
240
+ Button,
241
+ {
242
+ variant: "bare",
243
+ size: "bare",
244
+ type: "button",
245
+ "aria-pressed": archived,
246
+ onClick: () => setArchived((v) => !v),
247
+ className: cn(
248
+ "inline-flex items-center gap-1 rounded-full border px-2.5 py-1 text-xs font-medium transition-colors",
249
+ archived ? "border-transparent bg-primary text-primary-foreground" : "border-transparent bg-muted text-muted-foreground hover:bg-muted/70"
250
+ ),
251
+ children: [
252
+ /* @__PURE__ */ jsx(Archive, { className: "h-3 w-3" }),
253
+ " ",
254
+ t("conversations.filter.archived")
255
+ ]
256
+ }
257
+ )
258
+ ] })
233
259
  ] }),
234
260
  /* @__PURE__ */ jsxs("div", { className: "min-h-0 flex-1 overflow-y-auto", children: [
235
261
  loading && conversations.length === 0 && Array.from({ length: 6 }, (_, i) => /* @__PURE__ */ jsxs("div", { className: "flex w-full items-start gap-3 border-b border-border/50 px-3 py-3", children: [
@@ -243,11 +269,11 @@ function ConversationList({ className }) {
243
269
  /* @__PURE__ */ jsx(Skeleton, { className: "mt-1.5 h-3 w-3/4" })
244
270
  ] })
245
271
  ] }, i)),
246
- !loading && conversations.length === 0 && /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 p-8 text-center text-muted-foreground", children: [
272
+ !loading && shown.length === 0 && /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 p-8 text-center text-muted-foreground", children: [
247
273
  /* @__PURE__ */ jsx(Inbox, { className: "h-6 w-6" }),
248
- /* @__PURE__ */ jsx("p", { className: "text-sm", children: t("conversations.list.empty") })
274
+ /* @__PURE__ */ jsx("p", { className: "text-sm", children: archived ? t("conversations.list.emptyArchived") : t("conversations.list.empty") })
249
275
  ] }),
250
- conversations.map((c) => {
276
+ shown.map((c) => {
251
277
  const active = c.id === selectedId;
252
278
  const unread = isWaitingOnUs(c);
253
279
  return (
@@ -294,7 +320,7 @@ function ConversationList({ className }) {
294
320
  ] }),
295
321
  /* @__PURE__ */ jsxs("div", { className: "mt-1 flex items-center justify-between gap-2", children: [
296
322
  /* @__PURE__ */ jsx("span", { className: cn("truncate text-xs", unread ? "text-foreground" : "text-muted-foreground"), children: /* @__PURE__ */ jsx(MessagePreview, { text: c.lastMessagePreview }) }),
297
- unread && /* @__PURE__ */ jsx("span", { className: "flex h-[18px] min-w-[18px] shrink-0 items-center justify-center rounded-full bg-destructive px-1.5 text-[10px] font-semibold text-destructive-foreground", children: c.unreadCount > 0 ? c.unreadCount : "\xB7" })
323
+ unread && /* @__PURE__ */ jsx("span", { className: "flex h-[18px] min-w-[18px] shrink-0 items-center justify-center rounded-full bg-destructive px-1.5 text-[10px] font-semibold text-destructive-foreground group-hover:invisible", children: c.unreadCount > 0 ? c.unreadCount : "\xB7" })
298
324
  ] })
299
325
  ] })
300
326
  ]
@@ -312,7 +338,7 @@ function ConversationList({ className }) {
312
338
  e.stopPropagation();
313
339
  void markRead(c.id);
314
340
  },
315
- className: "absolute right-2 top-2 hidden rounded-full p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground group-hover:block",
341
+ className: "absolute bottom-2.5 right-2.5 hidden rounded-full p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground group-hover:block",
316
342
  children: /* @__PURE__ */ jsx(CheckCheck, { className: "h-3.5 w-3.5" })
317
343
  }
318
344
  )
@@ -855,15 +881,15 @@ function buildRows(messages) {
855
881
  const rows = [];
856
882
  let lastDay = "";
857
883
  messages.forEach((m, i) => {
858
- const day = new Date(m.at).toDateString();
859
- if (day !== lastDay) {
860
- rows.push({ kind: "day", id: `day-${day}`, label: dayLabel(m.at) });
861
- lastDay = day;
884
+ const day2 = new Date(m.at).toDateString();
885
+ if (day2 !== lastDay) {
886
+ rows.push({ kind: "day", id: `day-${day2}`, label: dayLabel(m.at) });
887
+ lastDay = day2;
862
888
  }
863
889
  const prev = messages[i - 1];
864
890
  const next = messages[i + 1];
865
- const samePrev = prev && prev.direction === m.direction && new Date(prev.at).toDateString() === day;
866
- const sameNext = next && next.direction === m.direction && new Date(next.at).toDateString() === day;
891
+ const samePrev = prev && prev.direction === m.direction && new Date(prev.at).toDateString() === day2;
892
+ const sameNext = next && next.direction === m.direction && new Date(next.at).toDateString() === day2;
867
893
  rows.push({ kind: "msg", id: m.id, message: m, startsRun: !samePrev, endsRun: !sameNext });
868
894
  });
869
895
  return rows;
@@ -916,36 +942,61 @@ function MessageThread({ selected, onTogglePanel, panelOpen, onBack, backAlways,
916
942
  window.setTimeout(() => el.classList.remove("ring-2", "ring-primary"), 1200);
917
943
  }, []);
918
944
  return /* @__PURE__ */ jsxs("section", { className: cn("flex min-w-0 flex-1 flex-col bg-muted/20", className), children: [
919
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2 border-b border-border bg-card px-3 py-2.5 md:px-5", children: [
920
- /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-2 md:gap-3", children: [
921
- onBack && /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", className: cn("-ml-1 shrink-0", !backAlways && "lg:hidden"), onClick: onBack, "aria-label": t("conversations.thread.back"), children: /* @__PURE__ */ jsx(ChevronLeft, { className: "h-5 w-5" }) }),
922
- /* @__PURE__ */ jsx(Avatar, { name: selected.contactName, accent: selected.accent, size: "sm", channel: selected.channel }),
923
- /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
924
- /* @__PURE__ */ jsx("p", { className: "truncate text-sm font-semibold text-foreground", children: selected.contactName }),
925
- !panelOpen && /* @__PURE__ */ jsxs("p", { className: "flex items-center gap-1.5 text-xs text-muted-foreground", children: [
926
- /* @__PURE__ */ jsx(ChannelBadge, { channel: selected.channel }),
927
- /* @__PURE__ */ jsx("span", { className: "truncate", children: selected.contactHandle })
928
- ] })
929
- ] })
930
- ] }),
931
- /* @__PURE__ */ jsxs("div", { className: "flex shrink-0 items-center gap-1.5", children: [
932
- /* @__PURE__ */ jsxs(Button, { variant: "outline", size: "sm", onClick: () => setStatus("closed"), "aria-label": "Arquivar", children: [
933
- /* @__PURE__ */ jsx(Archive, { className: "h-3.5 w-3.5 sm:mr-1" }),
934
- " ",
935
- /* @__PURE__ */ jsx("span", { className: "hidden sm:inline", children: "Arquivar" })
936
- ] }),
937
- /* @__PURE__ */ jsx(
938
- Button,
939
- {
940
- variant: panelOpen ? "secondary" : "ghost",
941
- size: "icon",
942
- onClick: onTogglePanel,
943
- "aria-label": t("conversations.thread.details"),
944
- children: /* @__PURE__ */ jsx(PanelRight, { className: "h-4 w-4" })
945
+ /* @__PURE__ */ jsxs(
946
+ "div",
947
+ {
948
+ role: "button",
949
+ tabIndex: 0,
950
+ "aria-label": t("conversations.thread.details"),
951
+ "aria-expanded": panelOpen,
952
+ onClick: () => {
953
+ if (!panelOpen) onTogglePanel();
954
+ },
955
+ onKeyDown: (e) => {
956
+ if ((e.key === "Enter" || e.key === " ") && e.target === e.currentTarget && !panelOpen) {
957
+ e.preventDefault();
958
+ onTogglePanel();
945
959
  }
946
- )
947
- ] })
948
- ] }),
960
+ },
961
+ className: cn("flex items-center justify-between gap-2 border-b border-border bg-card px-3 py-2.5 md:px-5", !panelOpen && "cursor-pointer hover:bg-muted/40"),
962
+ children: [
963
+ /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-2 md:gap-3", children: [
964
+ onBack && /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", className: cn("-ml-1 shrink-0", !backAlways && "lg:hidden"), onClick: (e) => {
965
+ e.stopPropagation();
966
+ onBack?.();
967
+ }, "aria-label": t("conversations.thread.back"), children: /* @__PURE__ */ jsx(ChevronLeft, { className: "h-5 w-5" }) }),
968
+ /* @__PURE__ */ jsx(Avatar, { name: selected.contactName, accent: selected.accent, size: "sm", channel: selected.channel }),
969
+ /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
970
+ /* @__PURE__ */ jsx("p", { className: "truncate text-sm font-semibold text-foreground", children: selected.contactName }),
971
+ !panelOpen && /* @__PURE__ */ jsxs("p", { className: "flex items-center gap-1.5 text-xs text-muted-foreground", children: [
972
+ /* @__PURE__ */ jsx(ChannelBadge, { channel: selected.channel }),
973
+ selected.channel === "whatsapp" && selected.provider && /* @__PURE__ */ jsx(
974
+ "span",
975
+ {
976
+ title: "As respostas saem por este n\xFAmero, o mesmo em que a pessoa escreveu.",
977
+ className: "shrink-0 rounded-full bg-muted px-1.5 text-[10px] text-muted-foreground",
978
+ children: selected.provider === "tyxter" ? "n\xFAmero oficial" : "n\xFAmero do dia a dia"
979
+ }
980
+ ),
981
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: selected.contactHandle })
982
+ ] })
983
+ ] })
984
+ ] }),
985
+ /* @__PURE__ */ jsx("div", { className: "flex shrink-0 items-center gap-1.5", onClick: (e) => e.stopPropagation(), onKeyDown: (e) => e.stopPropagation(), children: /* @__PURE__ */ jsxs(Dropdown, { children: [
986
+ /* @__PURE__ */ jsx(DropdownTrigger, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", "aria-label": t("conversations.thread.more"), children: /* @__PURE__ */ jsx(MoreHorizontal, { className: "h-4 w-4" }) }) }),
987
+ /* @__PURE__ */ jsx(DropdownContent, { align: "end", children: selected.status === "closed" ? /* @__PURE__ */ jsxs(DropdownItem, { onSelect: () => setStatus("open"), children: [
988
+ /* @__PURE__ */ jsx(ArchiveRestore, { className: "mr-2 h-3.5 w-3.5" }),
989
+ " ",
990
+ t("conversations.thread.unarchive")
991
+ ] }) : /* @__PURE__ */ jsxs(DropdownItem, { onSelect: () => setStatus("closed"), children: [
992
+ /* @__PURE__ */ jsx(Archive, { className: "mr-2 h-3.5 w-3.5" }),
993
+ " ",
994
+ t("conversations.thread.archive")
995
+ ] }) })
996
+ ] }) })
997
+ ]
998
+ }
999
+ ),
949
1000
  /* @__PURE__ */ jsxs("div", { ref: threadRef, className: "min-h-0 flex-1 overflow-y-auto px-5 py-4", children: [
950
1001
  rows.map((row) => {
951
1002
  if (row.kind === "day") {
@@ -1028,156 +1079,260 @@ function MessageThread({ selected, onTogglePanel, panelOpen, onBack, backAlways,
1028
1079
  ] })
1029
1080
  ] });
1030
1081
  }
1031
- var TEMP = {
1032
- hot: { icon: Flame, cls: "bg-destructive-soft text-destructive-soft-foreground", label: "Quente" },
1033
- warm: { icon: Sun, cls: "bg-warning-soft text-warning-soft-foreground", label: "Morno" },
1034
- cold: { icon: Snowflake, cls: "bg-info-soft text-info-soft-foreground", label: "Frio" }
1035
- };
1036
- function LeadCard({ personId, handle }) {
1037
- const [lead, setLead] = React9__default.useState(void 0);
1082
+ var str = (v) => typeof v === "string" && v.trim() ? v.trim() : null;
1083
+ function declaredCompany(meta) {
1084
+ const f = meta.fields ?? {};
1085
+ const name = str(f.trade_name_declared) ?? str(meta.credential_company) ?? str(meta.company) ?? str(f.legal_name_declared);
1086
+ if (!name) return null;
1087
+ return {
1088
+ id: "",
1089
+ name,
1090
+ legalName: str(f.legal_name_declared),
1091
+ document: str(f.cnpj_declared)?.replace(/\D/g, "") ?? null,
1092
+ role: str(f.role),
1093
+ phone: null,
1094
+ email: null,
1095
+ website: null,
1096
+ address: null,
1097
+ city: null,
1098
+ state: null,
1099
+ size: null,
1100
+ openedAt: null,
1101
+ declaredOnly: true
1102
+ };
1103
+ }
1104
+ function useContactProfile(personId, handle) {
1105
+ const [profile, setProfile] = React9__default.useState(void 0);
1038
1106
  React9__default.useEffect(() => {
1039
1107
  let alive = true;
1108
+ setProfile(void 0);
1040
1109
  const sb = getSupabaseClientOptional();
1041
1110
  const tenantId = getActiveTenantId();
1042
1111
  if (!sb || !tenantId || !personId && !handle) {
1043
- setLead(null);
1112
+ setProfile(null);
1044
1113
  return;
1045
1114
  }
1046
1115
  (async () => {
1047
1116
  try {
1048
- let q = sb.from("people").select("id, name, metadata, phone").eq("tenant_id", tenantId).eq("kind", "lead").limit(1);
1117
+ let q = sb.from("people").select("id, kind, name, email, phone, document_number, city, state, tags, metadata").eq("tenant_id", tenantId).limit(1);
1049
1118
  q = personId ? q.eq("id", personId) : q.like("phone", `%${String(handle).replace(/\D/g, "").slice(-8)}`);
1050
- const { data } = await q.maybeSingle();
1119
+ const { data: p } = await q.maybeSingle();
1051
1120
  if (!alive) return;
1052
- if (!data) {
1053
- setLead(null);
1121
+ if (!p) {
1122
+ setProfile(null);
1054
1123
  return;
1055
1124
  }
1056
- const meta = data.metadata ?? {};
1057
- let score = null;
1058
- try {
1059
- const { data: s } = await sb.from("v_crm_lead_score").select("temperature, fit_score, interest_score, reasons").eq("lead_id", data.id).maybeSingle();
1060
- score = s;
1061
- } catch {
1062
- }
1063
- setLead({
1064
- id: data.id,
1065
- name: data.name,
1066
- company: meta.company ?? null,
1067
- status: meta.status ?? null,
1068
- sourceName: meta.sourceName ?? null,
1125
+ const meta = p.metadata ?? {};
1126
+ const [score, link] = await Promise.all([
1127
+ sb.from("v_crm_lead_score").select("temperature, interest_score, reasons").eq("lead_id", p.id).maybeSingle().then((r) => r.data).catch(() => null),
1128
+ sb.from("plg_crm_company_people").select("role, is_primary, company:plg_crm_companies(id, name, legal_name, document_number, phone, email, website, address, city, state, metadata)").eq("person_id", p.id).order("is_primary", { ascending: false }).limit(1).maybeSingle().then((r) => r.data).catch(() => null)
1129
+ ]);
1130
+ if (!alive) return;
1131
+ const c = link?.company;
1132
+ const receita = c?.metadata?.receita ?? {};
1133
+ const company = c ? {
1134
+ id: c.id,
1135
+ name: c.name,
1136
+ legalName: str(c.legal_name),
1137
+ document: str(c.document_number),
1138
+ role: str(link.role),
1139
+ phone: str(c.phone),
1140
+ email: str(c.email),
1141
+ website: str(c.website),
1142
+ address: str(c.address),
1143
+ city: str(c.city),
1144
+ state: str(c.state),
1145
+ size: str(receita.porte),
1146
+ openedAt: str(receita.abertura)
1147
+ } : declaredCompany(meta);
1148
+ setProfile({
1149
+ id: p.id,
1150
+ kind: p.kind,
1151
+ name: p.name,
1152
+ phone: str(p.phone),
1153
+ email: str(p.email),
1154
+ document: str(p.document_number),
1155
+ city: str(p.city),
1156
+ state: str(p.state),
1157
+ tags: Array.isArray(p.tags) ? p.tags : [],
1158
+ status: str(meta.status),
1159
+ sourceName: str(meta.sourceName),
1069
1160
  temperature: score?.temperature ?? null,
1070
- fitScore: score?.fit_score ?? null,
1071
1161
  interestScore: score?.interest_score ?? null,
1072
- reasons: Array.isArray(score?.reasons) ? score.reasons : []
1162
+ reasons: Array.isArray(score?.reasons) ? score.reasons : [],
1163
+ fields: meta.fields ?? {},
1164
+ touchpoints: Array.isArray(meta.touchpoints) ? meta.touchpoints.filter((x) => x && x.detail).map((x) => ({ at: String(x.at ?? ""), kind: String(x.kind ?? ""), detail: String(x.detail) })) : [],
1165
+ company
1073
1166
  });
1074
1167
  } catch {
1075
- if (alive) setLead(null);
1168
+ if (alive) setProfile(null);
1076
1169
  }
1077
1170
  })();
1078
1171
  return () => {
1079
1172
  alive = false;
1080
1173
  };
1081
1174
  }, [personId, handle]);
1082
- if (lead === void 0) return /* @__PURE__ */ jsx("div", { className: "px-4 py-3 text-xs text-muted-foreground", children: "carregando\u2026" });
1083
- if (!lead) {
1084
- return /* @__PURE__ */ jsx("div", { className: "border-t border-border px-4 py-3", children: /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: "Este contato ainda n\xE3o \xE9 um lead cadastrado." }) });
1085
- }
1086
- const t = lead.temperature ? TEMP[lead.temperature] : null;
1087
- const Icon = t?.icon;
1088
- return /* @__PURE__ */ jsxs("div", { className: "border-t border-border px-4 py-3", children: [
1089
- /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between gap-2", children: [
1090
- /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
1091
- /* @__PURE__ */ jsx("p", { className: "truncate text-sm font-semibold", children: lead.name }),
1092
- lead.company && /* @__PURE__ */ jsxs("p", { className: "flex items-center gap-1 truncate text-xs text-muted-foreground", children: [
1093
- /* @__PURE__ */ jsx(Building2, { className: "h-3 w-3 shrink-0" }),
1094
- " ",
1095
- lead.company
1096
- ] })
1097
- ] }),
1098
- /* @__PURE__ */ jsxs(
1099
- "a",
1100
- {
1101
- href: appHref(`/sales/leads/${lead.id}`),
1102
- className: "inline-flex shrink-0 items-center gap-1 rounded-button border bg-card px-2 py-1 text-[11px] font-medium hover:bg-muted",
1103
- children: [
1104
- "Abrir lead ",
1105
- /* @__PURE__ */ jsx(ExternalLink, { className: "h-3 w-3" })
1106
- ]
1107
- }
1108
- )
1109
- ] }),
1110
- /* @__PURE__ */ jsxs("div", { className: "mt-2 flex flex-wrap items-center gap-1.5", children: [
1111
- t && Icon && /* @__PURE__ */ jsxs("span", { className: `inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[10px] font-medium ${t.cls}`, children: [
1112
- /* @__PURE__ */ jsx(Icon, { className: "h-3 w-3" }),
1113
- " ",
1114
- t.label,
1115
- lead.interestScore != null && /* @__PURE__ */ jsx("span", { className: "tabular-nums opacity-70", children: lead.interestScore })
1116
- ] }),
1117
- lead.status && /* @__PURE__ */ jsx("span", { className: "rounded-full bg-muted px-2 py-0.5 text-[10px] capitalize text-muted-foreground", children: lead.status }),
1118
- lead.sourceName && /* @__PURE__ */ jsx("span", { className: "rounded-full bg-muted px-2 py-0.5 text-[10px] text-muted-foreground", children: lead.sourceName })
1175
+ return profile;
1176
+ }
1177
+ var PROFILE_FIELDS = [
1178
+ ["role", "conversations.profile.role"],
1179
+ ["buying_role", "conversations.profile.buyingRole"],
1180
+ ["venue_type", "conversations.profile.venueType"],
1181
+ ["revenue", "conversations.profile.revenue"],
1182
+ ["challenges", "conversations.profile.challenges"],
1183
+ ["goals", "conversations.profile.goals"]
1184
+ ];
1185
+ function fieldText(value) {
1186
+ if (value === null || value === void 0 || value === "") return null;
1187
+ if (Array.isArray(value)) return value.length ? value.join(", ") : null;
1188
+ if (typeof value === "boolean") return value ? "Sim" : "N\xE3o";
1189
+ if (typeof value === "object") return null;
1190
+ return String(value);
1191
+ }
1192
+ function formatDocument(doc) {
1193
+ const d = (doc ?? "").replace(/\D/g, "");
1194
+ if (d.length === 14) return `${d.slice(0, 2)}.${d.slice(2, 5)}.${d.slice(5, 8)}/${d.slice(8, 12)}-${d.slice(12)}`;
1195
+ if (d.length === 11) return `${d.slice(0, 3)}.${d.slice(3, 6)}.${d.slice(6, 9)}-${d.slice(9)}`;
1196
+ return doc;
1197
+ }
1198
+ var LEAD_STATUS = {
1199
+ new: { labelKey: "conversations.leadStatus.new", dot: "#3B82F6" },
1200
+ contacted: { labelKey: "conversations.leadStatus.contacted", dot: "#8B5CF6" },
1201
+ qualified: { labelKey: "conversations.leadStatus.qualified", dot: "#10B981" },
1202
+ unqualified: { labelKey: "conversations.leadStatus.unqualified", dot: "#94A3B8" },
1203
+ converted: { labelKey: "conversations.leadStatus.converted", dot: "#F59E0B" },
1204
+ lost: { labelKey: "conversations.leadStatus.lost", dot: "#EF4444" }
1205
+ };
1206
+ var TEMP = {
1207
+ hot: { icon: Flame, cls: "bg-destructive-soft text-destructive-soft-foreground", labelKey: "conversations.temperature.hot" },
1208
+ warm: { icon: Sun, cls: "bg-warning-soft text-warning-soft-foreground", labelKey: "conversations.temperature.warm" },
1209
+ cold: { icon: Snowflake, cls: "bg-info-soft text-info-soft-foreground", labelKey: "conversations.temperature.cold" }
1210
+ };
1211
+ function Section({ title, action, children }) {
1212
+ return /* @__PURE__ */ jsxs("section", { className: "px-4 pt-4", children: [
1213
+ /* @__PURE__ */ jsxs("div", { className: "mb-1 flex items-center justify-between gap-2", children: [
1214
+ /* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold text-foreground", children: title }),
1215
+ action
1119
1216
  ] }),
1120
- lead.reasons.length > 0 && /* @__PURE__ */ jsx("ul", { className: "mt-2 space-y-0.5", children: lead.reasons.slice(0, 4).map((r, i) => /* @__PURE__ */ jsxs("li", { className: "flex gap-1.5 text-[11px] text-muted-foreground", children: [
1121
- /* @__PURE__ */ jsx("span", { className: "text-muted-foreground/40", children: "\u2022" }),
1122
- /* @__PURE__ */ jsx("span", { children: r })
1123
- ] }, i)) })
1217
+ /* @__PURE__ */ jsx("div", { className: "rounded-card border bg-card shadow-sm", children })
1124
1218
  ] });
1125
1219
  }
1126
- function Section({ icon: Icon, title, children }) {
1127
- return /* @__PURE__ */ jsxs("div", { className: "border-t border-border px-4 py-3", children: [
1128
- /* @__PURE__ */ jsxs("p", { className: "mb-1.5 flex items-center gap-1.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground", children: [
1129
- /* @__PURE__ */ jsx(Icon, { className: "h-3 w-3" }),
1130
- " ",
1131
- title
1132
- ] }),
1133
- children
1220
+ function Rows({ rows }) {
1221
+ const shown = rows.filter(([, v]) => v !== null && v !== void 0 && v !== "");
1222
+ if (!shown.length) return null;
1223
+ return /* @__PURE__ */ jsx("dl", { className: "divide-y", children: shown.map(([label, value]) => /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[6.5rem_minmax(0,1fr)] gap-x-3 px-3 py-2", children: [
1224
+ /* @__PURE__ */ jsx("dt", { className: "text-xs font-medium text-muted-foreground", children: label }),
1225
+ /* @__PURE__ */ jsx("dd", { className: "min-w-0 break-words text-xs text-foreground", children: value })
1226
+ ] }, label)) });
1227
+ }
1228
+ function OpenLink({ href, label }) {
1229
+ return /* @__PURE__ */ jsxs("a", { href: appHref(href), className: "inline-flex items-center gap-1 text-xs font-medium text-primary hover:underline", children: [
1230
+ label,
1231
+ " ",
1232
+ /* @__PURE__ */ jsx(ExternalLink, { className: "h-3 w-3" })
1134
1233
  ] });
1135
1234
  }
1235
+ function place(city, state) {
1236
+ return [city, state].filter(Boolean).join(" / ") || null;
1237
+ }
1238
+ function day(iso) {
1239
+ const d = new Date(iso.length === 10 ? `${iso}T12:00:00` : iso);
1240
+ return Number.isNaN(d.getTime()) ? iso : d.toLocaleDateString("pt-BR");
1241
+ }
1136
1242
  function ContactPanel({ contact, onClose, className }) {
1137
1243
  const t = useTranslation();
1138
- return /* @__PURE__ */ jsxs("aside", { className: cn("flex shrink-0 flex-col overflow-y-auto border-l border-border bg-card", className), children: [
1139
- onClose && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between border-b border-border px-3 py-2 xl:hidden", children: [
1244
+ const profile = useContactProfile(contact.contactPersonId, contact.contactHandle);
1245
+ return /* @__PURE__ */ jsxs("aside", { className: cn("flex shrink-0 flex-col overflow-y-auto border-l border-border bg-muted/20 pb-4", className), children: [
1246
+ /* @__PURE__ */ jsxs("div", { className: "sticky top-0 z-10 flex items-center justify-between border-b border-border bg-card px-3 py-2", children: [
1140
1247
  /* @__PURE__ */ jsx("span", { className: "text-sm font-semibold text-foreground", children: t("conversations.contact.details") }),
1141
- /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", onClick: onClose, "aria-label": t("conversations.contact.closeDetails"), children: /* @__PURE__ */ jsx(X, { className: "h-4 w-4" }) })
1248
+ onClose && /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", onClick: onClose, "aria-label": t("conversations.contact.closeDetails"), children: /* @__PURE__ */ jsx(X, { className: "h-4 w-4" }) })
1142
1249
  ] }),
1143
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 px-4 py-5 text-center", children: [
1250
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 bg-card px-4 py-5 text-center", children: [
1144
1251
  /* @__PURE__ */ jsx(Avatar, { name: contact.contactName, accent: contact.accent, size: "lg", channel: contact.channel }),
1145
- /* @__PURE__ */ jsxs("div", { children: [
1146
- /* @__PURE__ */ jsx("p", { className: "text-sm font-semibold text-foreground", children: contact.contactName }),
1252
+ /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
1253
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-semibold text-foreground", children: profile?.name ?? contact.contactName }),
1254
+ profile?.company && /* @__PURE__ */ jsxs("p", { className: "mt-0.5 flex items-center justify-center gap-1 text-xs text-muted-foreground", children: [
1255
+ /* @__PURE__ */ jsx(Building2, { className: "h-3 w-3 shrink-0" }),
1256
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: [profile.company.role, profile.company.name].filter(Boolean).join(" \xB7 ") })
1257
+ ] }),
1147
1258
  /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: contact.contactHandle })
1148
1259
  ] }),
1149
1260
  /* @__PURE__ */ jsx(ChannelBadge, { channel: contact.channel })
1150
1261
  ] }),
1151
- /* @__PURE__ */ jsx(LeadCard, { personId: contact.contactPersonId, handle: contact.contactHandle }),
1152
- /* @__PURE__ */ jsx(Section, { icon: User, title: t("conversations.contact.details"), children: /* @__PURE__ */ jsxs("dl", { className: "space-y-1.5 text-sm", children: [
1153
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
1154
- /* @__PURE__ */ jsx("dt", { className: "text-muted-foreground", children: t("conversations.contact.channel") }),
1155
- /* @__PURE__ */ jsx("dd", { className: "text-foreground", children: CHANNEL_LABELS[contact.channel] })
1156
- ] }),
1157
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
1158
- /* @__PURE__ */ jsx("dt", { className: "text-muted-foreground", children: t("conversations.contact.status") }),
1159
- /* @__PURE__ */ jsx("dd", { className: "text-foreground", children: t(`conversations.status.${contact.status}`) })
1160
- ] }),
1161
- contact.assignedTo && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
1162
- /* @__PURE__ */ jsx("dt", { className: "text-muted-foreground", children: t("conversations.contact.assignedTo") }),
1163
- /* @__PURE__ */ jsx("dd", { className: "text-foreground", children: contact.assignedTo })
1164
- ] }),
1165
- contact.leadOwnerName && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
1166
- /* @__PURE__ */ jsx("dt", { className: "text-muted-foreground", children: t("conversations.contact.leadOwner") }),
1167
- /* @__PURE__ */ jsx("dd", { className: "font-medium text-foreground", children: contact.leadOwnerName })
1168
- ] }),
1169
- contact.location && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
1170
- /* @__PURE__ */ jsxs("dt", { className: "flex items-center gap-1 text-muted-foreground", children: [
1171
- /* @__PURE__ */ jsx(MapPin, { className: "h-3 w-3" }),
1262
+ profile === void 0 && /* @__PURE__ */ jsx("p", { className: "px-4 pt-4 text-xs text-muted-foreground", children: t("conversations.profile.loading") }),
1263
+ profile === null && /* @__PURE__ */ jsx("p", { className: "px-4 pt-4 text-xs text-muted-foreground", children: t("conversations.profile.notInCrm") }),
1264
+ profile && /* @__PURE__ */ jsx(ProfileSections, { profile }),
1265
+ /* @__PURE__ */ jsx(Section, { title: t("conversations.profile.conversation"), children: /* @__PURE__ */ jsx(Rows, { rows: [
1266
+ [t("conversations.contact.channel"), CHANNEL_LABELS[contact.channel]],
1267
+ [t("conversations.contact.status"), t(`conversations.status.${contact.status}`)],
1268
+ [t("conversations.contact.assignedTo"), contact.assignedTo],
1269
+ // Who answers this conversation may not be who follows the lead up:
1270
+ // side by side on purpose — when they differ, that is information.
1271
+ [t("conversations.contact.leadOwner"), contact.leadOwnerName],
1272
+ [t("conversations.contact.location"), contact.location],
1273
+ [t("conversations.contact.note"), contact.note]
1274
+ ] }) })
1275
+ ] });
1276
+ }
1277
+ function ProfileSections({ profile }) {
1278
+ const t = useTranslation();
1279
+ const status = profile.status ? LEAD_STATUS[profile.status] : null;
1280
+ const temp = profile.temperature ? TEMP[profile.temperature] : null;
1281
+ const TempIcon = temp?.icon;
1282
+ const c = profile.company;
1283
+ const isLead = profile.kind === "lead";
1284
+ const fields = PROFILE_FIELDS.map(([key, labelKey]) => [t(labelKey), fieldText(profile.fields[key])]);
1285
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1286
+ isLead && /* @__PURE__ */ jsxs(Section, { title: t("conversations.profile.lead"), action: /* @__PURE__ */ jsx(OpenLink, { href: `/sales/leads/${profile.id}`, label: t("conversations.profile.openLead") }), children: [
1287
+ /* @__PURE__ */ jsx(Rows, { rows: [
1288
+ [t("conversations.profile.status"), status ? /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5", children: [
1289
+ /* @__PURE__ */ jsx("span", { className: "h-1.5 w-1.5 rounded-full", style: { backgroundColor: status.dot } }),
1290
+ t(status.labelKey)
1291
+ ] }) : profile.status],
1292
+ [t("conversations.profile.temperature"), temp && TempIcon ? /* @__PURE__ */ jsxs("span", { className: cn("inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[11px] font-medium", temp.cls), children: [
1293
+ /* @__PURE__ */ jsx(TempIcon, { className: "h-3 w-3" }),
1172
1294
  " ",
1173
- t("conversations.contact.location")
1174
- ] }),
1175
- /* @__PURE__ */ jsx("dd", { className: "text-foreground", children: contact.location })
1176
- ] })
1295
+ t(temp.labelKey),
1296
+ profile.interestScore != null && /* @__PURE__ */ jsx("span", { className: "tabular-nums opacity-70", children: profile.interestScore })
1297
+ ] }) : null],
1298
+ [t("conversations.profile.source"), profile.sourceName]
1299
+ ] }),
1300
+ profile.reasons.length > 0 && /* @__PURE__ */ jsx("ul", { className: "space-y-0.5 border-t px-3 py-2", children: profile.reasons.slice(0, 5).map((r, i) => /* @__PURE__ */ jsxs("li", { className: "flex gap-1.5 text-[11px] text-muted-foreground", children: [
1301
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground/40", children: "\u2022" }),
1302
+ /* @__PURE__ */ jsx("span", { children: r })
1303
+ ] }, i)) })
1304
+ ] }),
1305
+ c && /* @__PURE__ */ jsx(
1306
+ Section,
1307
+ {
1308
+ title: t("conversations.profile.company"),
1309
+ action: c.declaredOnly ? /* @__PURE__ */ jsx("span", { className: "text-[11px] text-muted-foreground", children: t("conversations.profile.declared") }) : /* @__PURE__ */ jsx(OpenLink, { href: `/sales/companies/${c.id}`, label: t("conversations.profile.openCompany") }),
1310
+ children: /* @__PURE__ */ jsx(Rows, { rows: [
1311
+ [t("conversations.profile.tradeName"), c.name],
1312
+ [t("conversations.profile.legalName"), c.legalName !== c.name ? c.legalName : null],
1313
+ [t("conversations.profile.cnpj"), c.document ? /* @__PURE__ */ jsx("span", { className: "tabular-nums", children: formatDocument(c.document) }) : null],
1314
+ [t("conversations.profile.role"), c.role],
1315
+ [t("conversations.profile.size"), c.size],
1316
+ [t("conversations.profile.since"), c.openedAt ? day(c.openedAt) : null],
1317
+ [t("conversations.profile.phone"), c.phone],
1318
+ [t("conversations.profile.email"), c.email],
1319
+ [t("conversations.profile.website"), c.website],
1320
+ [t("conversations.profile.address"), [c.address, place(c.city, c.state)].filter(Boolean).join(" \u2014 ") || null]
1321
+ ] })
1322
+ }
1323
+ ),
1324
+ /* @__PURE__ */ jsx(Section, { title: t("conversations.profile.person"), children: /* @__PURE__ */ jsx(Rows, { rows: [
1325
+ [t("conversations.profile.phone"), profile.phone],
1326
+ [t("conversations.profile.email"), profile.email],
1327
+ [t("conversations.profile.cpf"), profile.document ? /* @__PURE__ */ jsx("span", { className: "tabular-nums", children: formatDocument(profile.document) }) : null],
1328
+ [t("conversations.profile.city"), place(profile.city, profile.state)],
1329
+ ...fields.filter(([label]) => label !== t("conversations.profile.role") || !c?.role)
1177
1330
  ] }) }),
1178
- contact.tags.length > 0 && /* @__PURE__ */ jsx(Section, { icon: Tag, title: t("conversations.contact.tags"), children: /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1.5", children: contact.tags.map((tag) => /* @__PURE__ */ jsx("span", { className: "rounded-full bg-muted px-2 py-0.5 text-xs font-medium text-foreground", children: tag }, tag)) }) }),
1179
- contact.note && /* @__PURE__ */ jsx(Section, { icon: StickyNote, title: t("conversations.contact.note"), children: /* @__PURE__ */ jsx("p", { className: "text-sm text-foreground", children: contact.note }) }),
1180
- /* @__PURE__ */ jsx(Section, { icon: Link2, title: t("conversations.contact.linkedRecords"), children: /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t("conversations.contact.noLinkedRecords") }) })
1331
+ profile.tags.length > 0 && /* @__PURE__ */ jsx(Section, { title: t("conversations.contact.tags"), children: /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1.5 px-3 py-2", children: profile.tags.map((tag) => /* @__PURE__ */ jsx("span", { className: "rounded-full border border-border bg-card px-2 py-0.5 text-[11px] text-foreground/80", children: tag }, tag)) }) }),
1332
+ profile.touchpoints.length > 0 && /* @__PURE__ */ jsx(Section, { title: t("conversations.profile.history"), children: /* @__PURE__ */ jsx("ol", { className: "divide-y", children: profile.touchpoints.map((tp, i) => /* @__PURE__ */ jsxs("li", { className: "grid grid-cols-[6.5rem_minmax(0,1fr)] gap-x-3 px-3 py-2 text-xs", children: [
1333
+ /* @__PURE__ */ jsx("span", { className: "tabular-nums text-muted-foreground", children: tp.at ? day(tp.at) : "\u2014" }),
1334
+ /* @__PURE__ */ jsx("span", { className: "min-w-0 break-words text-foreground", children: tp.detail })
1335
+ ] }, i)) }) })
1181
1336
  ] });
1182
1337
  }
1183
1338
  var CHANNELS = ["whatsapp", "sms", "instagram", "email", "webchat"];
@@ -1430,10 +1585,16 @@ function requestedContact() {
1430
1585
  return null;
1431
1586
  }
1432
1587
  }
1588
+ function conversationIdFromPath(path) {
1589
+ const m = /^\/conversations\/([^/?#]+)/.exec(path);
1590
+ return m ? decodeURIComponent(m[1]) : null;
1591
+ }
1433
1592
  function ConversationsPage({ store, config }) {
1434
1593
  React9__default.useEffect(() => {
1435
1594
  const wanted = requestedContact();
1436
1595
  if (!wanted) {
1596
+ const fromPath = conversationIdFromPath(currentAppPath());
1597
+ if (fromPath) store.setState({ selectedId: fromPath });
1437
1598
  void store.getState().load();
1438
1599
  return;
1439
1600
  }
@@ -1447,9 +1608,30 @@ function ConversationsPage({ store, config }) {
1447
1608
  } catch {
1448
1609
  }
1449
1610
  }, []);
1611
+ React9__default.useEffect(() => {
1612
+ const write = (id) => {
1613
+ const path = currentAppPath();
1614
+ if (!path.startsWith("/conversations")) return;
1615
+ if (path.includes("?")) return;
1616
+ if (conversationIdFromPath(path) === id) return;
1617
+ replaceApp(id ? `/conversations/${encodeURIComponent(id)}` : "/conversations");
1618
+ };
1619
+ write(store.getState().selectedId);
1620
+ const unsubscribe = store.subscribe((state, previous) => {
1621
+ if (state.selectedId !== previous.selectedId) write(state.selectedId);
1622
+ });
1623
+ const off = onAppPathChange((path) => {
1624
+ const id = conversationIdFromPath(path || "/");
1625
+ if (id && id !== store.getState().selectedId) void store.getState().select(id);
1626
+ });
1627
+ return () => {
1628
+ unsubscribe();
1629
+ off();
1630
+ };
1631
+ }, [store]);
1450
1632
  return /* @__PURE__ */ jsx(ConversationsContextProvider, { store, config, children: /* @__PURE__ */ jsx(InboxView, {}) });
1451
1633
  }
1452
1634
 
1453
- export { ConversationsPage };
1454
- //# sourceMappingURL=ConversationsPage-6WOZYAD4.js.map
1455
- //# sourceMappingURL=ConversationsPage-6WOZYAD4.js.map
1635
+ export { ConversationsPage, conversationIdFromPath };
1636
+ //# sourceMappingURL=ConversationsPage-2D343Q5P.js.map
1637
+ //# sourceMappingURL=ConversationsPage-2D343Q5P.js.map