@fayz-ai/plugin-conversations 0.27.0 → 0.27.1

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.
@@ -2,10 +2,10 @@ import { ConversationsContextProvider, useConversationsStore, assinarChegada, us
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,53 @@ 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
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: selected.contactHandle })
974
+ ] })
975
+ ] })
976
+ ] }),
977
+ /* @__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: [
978
+ /* @__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" }) }) }),
979
+ /* @__PURE__ */ jsx(DropdownContent, { align: "end", children: selected.status === "closed" ? /* @__PURE__ */ jsxs(DropdownItem, { onSelect: () => setStatus("open"), children: [
980
+ /* @__PURE__ */ jsx(ArchiveRestore, { className: "mr-2 h-3.5 w-3.5" }),
981
+ " ",
982
+ t("conversations.thread.unarchive")
983
+ ] }) : /* @__PURE__ */ jsxs(DropdownItem, { onSelect: () => setStatus("closed"), children: [
984
+ /* @__PURE__ */ jsx(Archive, { className: "mr-2 h-3.5 w-3.5" }),
985
+ " ",
986
+ t("conversations.thread.archive")
987
+ ] }) })
988
+ ] }) })
989
+ ]
990
+ }
991
+ ),
949
992
  /* @__PURE__ */ jsxs("div", { ref: threadRef, className: "min-h-0 flex-1 overflow-y-auto px-5 py-4", children: [
950
993
  rows.map((row) => {
951
994
  if (row.kind === "day") {
@@ -1028,156 +1071,260 @@ function MessageThread({ selected, onTogglePanel, panelOpen, onBack, backAlways,
1028
1071
  ] })
1029
1072
  ] });
1030
1073
  }
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);
1074
+ var str = (v) => typeof v === "string" && v.trim() ? v.trim() : null;
1075
+ function declaredCompany(meta) {
1076
+ const f = meta.fields ?? {};
1077
+ const name = str(f.trade_name_declared) ?? str(meta.credential_company) ?? str(meta.company) ?? str(f.legal_name_declared);
1078
+ if (!name) return null;
1079
+ return {
1080
+ id: "",
1081
+ name,
1082
+ legalName: str(f.legal_name_declared),
1083
+ document: str(f.cnpj_declared)?.replace(/\D/g, "") ?? null,
1084
+ role: str(f.role),
1085
+ phone: null,
1086
+ email: null,
1087
+ website: null,
1088
+ address: null,
1089
+ city: null,
1090
+ state: null,
1091
+ size: null,
1092
+ openedAt: null,
1093
+ declaredOnly: true
1094
+ };
1095
+ }
1096
+ function useContactProfile(personId, handle) {
1097
+ const [profile, setProfile] = React9__default.useState(void 0);
1038
1098
  React9__default.useEffect(() => {
1039
1099
  let alive = true;
1100
+ setProfile(void 0);
1040
1101
  const sb = getSupabaseClientOptional();
1041
1102
  const tenantId = getActiveTenantId();
1042
1103
  if (!sb || !tenantId || !personId && !handle) {
1043
- setLead(null);
1104
+ setProfile(null);
1044
1105
  return;
1045
1106
  }
1046
1107
  (async () => {
1047
1108
  try {
1048
- let q = sb.from("people").select("id, name, metadata, phone").eq("tenant_id", tenantId).eq("kind", "lead").limit(1);
1109
+ let q = sb.from("people").select("id, kind, name, email, phone, document_number, city, state, tags, metadata").eq("tenant_id", tenantId).limit(1);
1049
1110
  q = personId ? q.eq("id", personId) : q.like("phone", `%${String(handle).replace(/\D/g, "").slice(-8)}`);
1050
- const { data } = await q.maybeSingle();
1111
+ const { data: p } = await q.maybeSingle();
1051
1112
  if (!alive) return;
1052
- if (!data) {
1053
- setLead(null);
1113
+ if (!p) {
1114
+ setProfile(null);
1054
1115
  return;
1055
1116
  }
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,
1117
+ const meta = p.metadata ?? {};
1118
+ const [score, link] = await Promise.all([
1119
+ sb.from("v_crm_lead_score").select("temperature, interest_score, reasons").eq("lead_id", p.id).maybeSingle().then((r) => r.data).catch(() => null),
1120
+ 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)
1121
+ ]);
1122
+ if (!alive) return;
1123
+ const c = link?.company;
1124
+ const receita = c?.metadata?.receita ?? {};
1125
+ const company = c ? {
1126
+ id: c.id,
1127
+ name: c.name,
1128
+ legalName: str(c.legal_name),
1129
+ document: str(c.document_number),
1130
+ role: str(link.role),
1131
+ phone: str(c.phone),
1132
+ email: str(c.email),
1133
+ website: str(c.website),
1134
+ address: str(c.address),
1135
+ city: str(c.city),
1136
+ state: str(c.state),
1137
+ size: str(receita.porte),
1138
+ openedAt: str(receita.abertura)
1139
+ } : declaredCompany(meta);
1140
+ setProfile({
1141
+ id: p.id,
1142
+ kind: p.kind,
1143
+ name: p.name,
1144
+ phone: str(p.phone),
1145
+ email: str(p.email),
1146
+ document: str(p.document_number),
1147
+ city: str(p.city),
1148
+ state: str(p.state),
1149
+ tags: Array.isArray(p.tags) ? p.tags : [],
1150
+ status: str(meta.status),
1151
+ sourceName: str(meta.sourceName),
1069
1152
  temperature: score?.temperature ?? null,
1070
- fitScore: score?.fit_score ?? null,
1071
1153
  interestScore: score?.interest_score ?? null,
1072
- reasons: Array.isArray(score?.reasons) ? score.reasons : []
1154
+ reasons: Array.isArray(score?.reasons) ? score.reasons : [],
1155
+ fields: meta.fields ?? {},
1156
+ 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) })) : [],
1157
+ company
1073
1158
  });
1074
1159
  } catch {
1075
- if (alive) setLead(null);
1160
+ if (alive) setProfile(null);
1076
1161
  }
1077
1162
  })();
1078
1163
  return () => {
1079
1164
  alive = false;
1080
1165
  };
1081
1166
  }, [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 })
1167
+ return profile;
1168
+ }
1169
+ var PROFILE_FIELDS = [
1170
+ ["role", "conversations.profile.role"],
1171
+ ["buying_role", "conversations.profile.buyingRole"],
1172
+ ["venue_type", "conversations.profile.venueType"],
1173
+ ["revenue", "conversations.profile.revenue"],
1174
+ ["challenges", "conversations.profile.challenges"],
1175
+ ["goals", "conversations.profile.goals"]
1176
+ ];
1177
+ function fieldText(value) {
1178
+ if (value === null || value === void 0 || value === "") return null;
1179
+ if (Array.isArray(value)) return value.length ? value.join(", ") : null;
1180
+ if (typeof value === "boolean") return value ? "Sim" : "N\xE3o";
1181
+ if (typeof value === "object") return null;
1182
+ return String(value);
1183
+ }
1184
+ function formatDocument(doc) {
1185
+ const d = (doc ?? "").replace(/\D/g, "");
1186
+ if (d.length === 14) return `${d.slice(0, 2)}.${d.slice(2, 5)}.${d.slice(5, 8)}/${d.slice(8, 12)}-${d.slice(12)}`;
1187
+ if (d.length === 11) return `${d.slice(0, 3)}.${d.slice(3, 6)}.${d.slice(6, 9)}-${d.slice(9)}`;
1188
+ return doc;
1189
+ }
1190
+ var LEAD_STATUS = {
1191
+ new: { labelKey: "conversations.leadStatus.new", dot: "#3B82F6" },
1192
+ contacted: { labelKey: "conversations.leadStatus.contacted", dot: "#8B5CF6" },
1193
+ qualified: { labelKey: "conversations.leadStatus.qualified", dot: "#10B981" },
1194
+ unqualified: { labelKey: "conversations.leadStatus.unqualified", dot: "#94A3B8" },
1195
+ converted: { labelKey: "conversations.leadStatus.converted", dot: "#F59E0B" },
1196
+ lost: { labelKey: "conversations.leadStatus.lost", dot: "#EF4444" }
1197
+ };
1198
+ var TEMP = {
1199
+ hot: { icon: Flame, cls: "bg-destructive-soft text-destructive-soft-foreground", labelKey: "conversations.temperature.hot" },
1200
+ warm: { icon: Sun, cls: "bg-warning-soft text-warning-soft-foreground", labelKey: "conversations.temperature.warm" },
1201
+ cold: { icon: Snowflake, cls: "bg-info-soft text-info-soft-foreground", labelKey: "conversations.temperature.cold" }
1202
+ };
1203
+ function Section({ title, action, children }) {
1204
+ return /* @__PURE__ */ jsxs("section", { className: "px-4 pt-4", children: [
1205
+ /* @__PURE__ */ jsxs("div", { className: "mb-1 flex items-center justify-between gap-2", children: [
1206
+ /* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold text-foreground", children: title }),
1207
+ action
1119
1208
  ] }),
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)) })
1209
+ /* @__PURE__ */ jsx("div", { className: "rounded-card border bg-card shadow-sm", children })
1124
1210
  ] });
1125
1211
  }
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
1212
+ function Rows({ rows }) {
1213
+ const shown = rows.filter(([, v]) => v !== null && v !== void 0 && v !== "");
1214
+ if (!shown.length) return null;
1215
+ 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: [
1216
+ /* @__PURE__ */ jsx("dt", { className: "text-xs font-medium text-muted-foreground", children: label }),
1217
+ /* @__PURE__ */ jsx("dd", { className: "min-w-0 break-words text-xs text-foreground", children: value })
1218
+ ] }, label)) });
1219
+ }
1220
+ function OpenLink({ href, label }) {
1221
+ return /* @__PURE__ */ jsxs("a", { href: appHref(href), className: "inline-flex items-center gap-1 text-xs font-medium text-primary hover:underline", children: [
1222
+ label,
1223
+ " ",
1224
+ /* @__PURE__ */ jsx(ExternalLink, { className: "h-3 w-3" })
1134
1225
  ] });
1135
1226
  }
1227
+ function place(city, state) {
1228
+ return [city, state].filter(Boolean).join(" / ") || null;
1229
+ }
1230
+ function day(iso) {
1231
+ const d = new Date(iso.length === 10 ? `${iso}T12:00:00` : iso);
1232
+ return Number.isNaN(d.getTime()) ? iso : d.toLocaleDateString("pt-BR");
1233
+ }
1136
1234
  function ContactPanel({ contact, onClose, className }) {
1137
1235
  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: [
1236
+ const profile = useContactProfile(contact.contactPersonId, contact.contactHandle);
1237
+ 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: [
1238
+ /* @__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
1239
  /* @__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" }) })
1240
+ 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
1241
  ] }),
1143
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 px-4 py-5 text-center", children: [
1242
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2 bg-card px-4 py-5 text-center", children: [
1144
1243
  /* @__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 }),
1244
+ /* @__PURE__ */ jsxs("div", { className: "min-w-0", children: [
1245
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-semibold text-foreground", children: profile?.name ?? contact.contactName }),
1246
+ profile?.company && /* @__PURE__ */ jsxs("p", { className: "mt-0.5 flex items-center justify-center gap-1 text-xs text-muted-foreground", children: [
1247
+ /* @__PURE__ */ jsx(Building2, { className: "h-3 w-3 shrink-0" }),
1248
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: [profile.company.role, profile.company.name].filter(Boolean).join(" \xB7 ") })
1249
+ ] }),
1147
1250
  /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: contact.contactHandle })
1148
1251
  ] }),
1149
1252
  /* @__PURE__ */ jsx(ChannelBadge, { channel: contact.channel })
1150
1253
  ] }),
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" }),
1254
+ profile === void 0 && /* @__PURE__ */ jsx("p", { className: "px-4 pt-4 text-xs text-muted-foreground", children: t("conversations.profile.loading") }),
1255
+ profile === null && /* @__PURE__ */ jsx("p", { className: "px-4 pt-4 text-xs text-muted-foreground", children: t("conversations.profile.notInCrm") }),
1256
+ profile && /* @__PURE__ */ jsx(ProfileSections, { profile }),
1257
+ /* @__PURE__ */ jsx(Section, { title: t("conversations.profile.conversation"), children: /* @__PURE__ */ jsx(Rows, { rows: [
1258
+ [t("conversations.contact.channel"), CHANNEL_LABELS[contact.channel]],
1259
+ [t("conversations.contact.status"), t(`conversations.status.${contact.status}`)],
1260
+ [t("conversations.contact.assignedTo"), contact.assignedTo],
1261
+ // Who answers this conversation may not be who follows the lead up:
1262
+ // side by side on purpose — when they differ, that is information.
1263
+ [t("conversations.contact.leadOwner"), contact.leadOwnerName],
1264
+ [t("conversations.contact.location"), contact.location],
1265
+ [t("conversations.contact.note"), contact.note]
1266
+ ] }) })
1267
+ ] });
1268
+ }
1269
+ function ProfileSections({ profile }) {
1270
+ const t = useTranslation();
1271
+ const status = profile.status ? LEAD_STATUS[profile.status] : null;
1272
+ const temp = profile.temperature ? TEMP[profile.temperature] : null;
1273
+ const TempIcon = temp?.icon;
1274
+ const c = profile.company;
1275
+ const isLead = profile.kind === "lead";
1276
+ const fields = PROFILE_FIELDS.map(([key, labelKey]) => [t(labelKey), fieldText(profile.fields[key])]);
1277
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
1278
+ isLead && /* @__PURE__ */ jsxs(Section, { title: t("conversations.profile.lead"), action: /* @__PURE__ */ jsx(OpenLink, { href: `/sales/leads/${profile.id}`, label: t("conversations.profile.openLead") }), children: [
1279
+ /* @__PURE__ */ jsx(Rows, { rows: [
1280
+ [t("conversations.profile.status"), status ? /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5", children: [
1281
+ /* @__PURE__ */ jsx("span", { className: "h-1.5 w-1.5 rounded-full", style: { backgroundColor: status.dot } }),
1282
+ t(status.labelKey)
1283
+ ] }) : profile.status],
1284
+ [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: [
1285
+ /* @__PURE__ */ jsx(TempIcon, { className: "h-3 w-3" }),
1172
1286
  " ",
1173
- t("conversations.contact.location")
1174
- ] }),
1175
- /* @__PURE__ */ jsx("dd", { className: "text-foreground", children: contact.location })
1176
- ] })
1287
+ t(temp.labelKey),
1288
+ profile.interestScore != null && /* @__PURE__ */ jsx("span", { className: "tabular-nums opacity-70", children: profile.interestScore })
1289
+ ] }) : null],
1290
+ [t("conversations.profile.source"), profile.sourceName]
1291
+ ] }),
1292
+ 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: [
1293
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground/40", children: "\u2022" }),
1294
+ /* @__PURE__ */ jsx("span", { children: r })
1295
+ ] }, i)) })
1296
+ ] }),
1297
+ c && /* @__PURE__ */ jsx(
1298
+ Section,
1299
+ {
1300
+ title: t("conversations.profile.company"),
1301
+ 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") }),
1302
+ children: /* @__PURE__ */ jsx(Rows, { rows: [
1303
+ [t("conversations.profile.tradeName"), c.name],
1304
+ [t("conversations.profile.legalName"), c.legalName !== c.name ? c.legalName : null],
1305
+ [t("conversations.profile.cnpj"), c.document ? /* @__PURE__ */ jsx("span", { className: "tabular-nums", children: formatDocument(c.document) }) : null],
1306
+ [t("conversations.profile.role"), c.role],
1307
+ [t("conversations.profile.size"), c.size],
1308
+ [t("conversations.profile.since"), c.openedAt ? day(c.openedAt) : null],
1309
+ [t("conversations.profile.phone"), c.phone],
1310
+ [t("conversations.profile.email"), c.email],
1311
+ [t("conversations.profile.website"), c.website],
1312
+ [t("conversations.profile.address"), [c.address, place(c.city, c.state)].filter(Boolean).join(" \u2014 ") || null]
1313
+ ] })
1314
+ }
1315
+ ),
1316
+ /* @__PURE__ */ jsx(Section, { title: t("conversations.profile.person"), children: /* @__PURE__ */ jsx(Rows, { rows: [
1317
+ [t("conversations.profile.phone"), profile.phone],
1318
+ [t("conversations.profile.email"), profile.email],
1319
+ [t("conversations.profile.cpf"), profile.document ? /* @__PURE__ */ jsx("span", { className: "tabular-nums", children: formatDocument(profile.document) }) : null],
1320
+ [t("conversations.profile.city"), place(profile.city, profile.state)],
1321
+ ...fields.filter(([label]) => label !== t("conversations.profile.role") || !c?.role)
1177
1322
  ] }) }),
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") }) })
1323
+ 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)) }) }),
1324
+ 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: [
1325
+ /* @__PURE__ */ jsx("span", { className: "tabular-nums text-muted-foreground", children: tp.at ? day(tp.at) : "\u2014" }),
1326
+ /* @__PURE__ */ jsx("span", { className: "min-w-0 break-words text-foreground", children: tp.detail })
1327
+ ] }, i)) }) })
1181
1328
  ] });
1182
1329
  }
1183
1330
  var CHANNELS = ["whatsapp", "sms", "instagram", "email", "webchat"];
@@ -1430,10 +1577,16 @@ function requestedContact() {
1430
1577
  return null;
1431
1578
  }
1432
1579
  }
1580
+ function conversationIdFromPath(path) {
1581
+ const m = /^\/conversations\/([^/?#]+)/.exec(path);
1582
+ return m ? decodeURIComponent(m[1]) : null;
1583
+ }
1433
1584
  function ConversationsPage({ store, config }) {
1434
1585
  React9__default.useEffect(() => {
1435
1586
  const wanted = requestedContact();
1436
1587
  if (!wanted) {
1588
+ const fromPath = conversationIdFromPath(currentAppPath());
1589
+ if (fromPath) store.setState({ selectedId: fromPath });
1437
1590
  void store.getState().load();
1438
1591
  return;
1439
1592
  }
@@ -1447,9 +1600,30 @@ function ConversationsPage({ store, config }) {
1447
1600
  } catch {
1448
1601
  }
1449
1602
  }, []);
1603
+ React9__default.useEffect(() => {
1604
+ const write = (id) => {
1605
+ const path = currentAppPath();
1606
+ if (!path.startsWith("/conversations")) return;
1607
+ if (path.includes("?")) return;
1608
+ if (conversationIdFromPath(path) === id) return;
1609
+ replaceApp(id ? `/conversations/${encodeURIComponent(id)}` : "/conversations");
1610
+ };
1611
+ write(store.getState().selectedId);
1612
+ const unsubscribe = store.subscribe((state, previous) => {
1613
+ if (state.selectedId !== previous.selectedId) write(state.selectedId);
1614
+ });
1615
+ const off = onAppPathChange((path) => {
1616
+ const id = conversationIdFromPath(path || "/");
1617
+ if (id && id !== store.getState().selectedId) void store.getState().select(id);
1618
+ });
1619
+ return () => {
1620
+ unsubscribe();
1621
+ off();
1622
+ };
1623
+ }, [store]);
1450
1624
  return /* @__PURE__ */ jsx(ConversationsContextProvider, { store, config, children: /* @__PURE__ */ jsx(InboxView, {}) });
1451
1625
  }
1452
1626
 
1453
- export { ConversationsPage };
1454
- //# sourceMappingURL=ConversationsPage-6WOZYAD4.js.map
1455
- //# sourceMappingURL=ConversationsPage-6WOZYAD4.js.map
1627
+ export { ConversationsPage, conversationIdFromPath };
1628
+ //# sourceMappingURL=ConversationsPage-RYEND3MG.js.map
1629
+ //# sourceMappingURL=ConversationsPage-RYEND3MG.js.map