@fayz-ai/plugin-conversations 0.17.0 → 0.17.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.
@@ -1 +1 @@
1
- {"version":3,"file":"InboxWatcher.d.ts","sourceRoot":"","sources":["../../src/components/InboxWatcher.tsx"],"names":[],"mappings":"AAiGA,wBAAgB,YAAY,CAAC,EAAE,KAAK,EAAE,KAAwB,EAAE,OAAgB,EAAE,QAAY,EAAE,EAAE;IAChG,sDAAsD;IACtD,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;qCAEiC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,QAgGA"}
1
+ {"version":3,"file":"InboxWatcher.d.ts","sourceRoot":"","sources":["../../src/components/InboxWatcher.tsx"],"names":[],"mappings":"AAkGA,wBAAgB,YAAY,CAAC,EAAE,KAAK,EAAE,KAAwB,EAAE,OAAgB,EAAE,QAAY,EAAE,EAAE;IAChG,sDAAsD;IACtD,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;qCAEiC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,QAiFA"}
package/dist/index.js CHANGED
@@ -386,6 +386,37 @@ var T = {
386
386
  messages: "plg_conversation_messages",
387
387
  channels: "plg_conversations_channels"
388
388
  };
389
+ var porConta = /* @__PURE__ */ new Map();
390
+ function assinarChegada(tenantId, ouvinte) {
391
+ const supabase = getSupabaseClientOptional();
392
+ if (!supabase?.channel || !tenantId) return () => {
393
+ };
394
+ let assinatura = porConta.get(tenantId);
395
+ if (!assinatura) {
396
+ const ouvintes = /* @__PURE__ */ new Set();
397
+ const canal = supabase.channel(`conversas:${tenantId}`).on("broadcast", { event: "inbound" }, (msg2) => {
398
+ const carga = { conversationId: msg2?.payload?.conversationId };
399
+ for (const f of [...ouvintes]) {
400
+ try {
401
+ f(carga);
402
+ } catch {
403
+ }
404
+ }
405
+ }).subscribe();
406
+ assinatura = { canal, ouvintes };
407
+ porConta.set(tenantId, assinatura);
408
+ }
409
+ assinatura.ouvintes.add(ouvinte);
410
+ return () => {
411
+ const a = porConta.get(tenantId);
412
+ if (!a) return;
413
+ a.ouvintes.delete(ouvinte);
414
+ if (a.ouvintes.size === 0) {
415
+ porConta.delete(tenantId);
416
+ void getSupabaseClientOptional()?.removeChannel?.(a.canal);
417
+ }
418
+ };
419
+ }
389
420
 
390
421
  // src/types.ts
391
422
  var CHANNEL_LABELS = {
@@ -484,26 +515,22 @@ function InboxWatcher({ label, route = "/conversations", section = "main", posit
484
515
  void contar();
485
516
  }, [contar]);
486
517
  React11.useEffect(() => {
487
- const supabase = getSupabaseClientOptional();
488
- if (!supabase?.channel || !tenantId) return;
489
- const canal = supabase.channel(`conversas:${tenantId}`).on("broadcast", { event: "inbound" }, (msg2) => {
490
- const id = msg2?.payload?.conversationId;
518
+ if (!tenantId) return;
519
+ return assinarChegada(tenantId, ({ conversationId }) => {
491
520
  void contar();
492
521
  if (podeTocar.current) tocar();
493
522
  void (async () => {
523
+ const supabase = getSupabaseClientOptional();
494
524
  let titulo = label;
495
525
  let corpo = "";
496
- if (id) {
497
- const { data } = await supabase.from(T.conversations).select("contact_name, last_message_preview").eq("id", id).maybeSingle();
526
+ if (conversationId && supabase) {
527
+ const { data } = await supabase.from(T.conversations).select("contact_name, last_message_preview").eq("id", conversationId).maybeSingle();
498
528
  titulo = data?.contact_name || label;
499
529
  corpo = String(data?.last_message_preview || "").replace(/\s+/g, " ").slice(0, 140);
500
530
  }
501
- avisarNoSistema(titulo, corpo, String(id ?? "inbox"));
531
+ avisarNoSistema(titulo, corpo, String(conversationId ?? "inbox"));
502
532
  })();
503
- }).subscribe();
504
- return () => {
505
- void supabase.removeChannel?.(canal);
506
- };
533
+ });
507
534
  }, [contar, tenantId, label]);
508
535
  useNavContribution("conversations:unread", naoLidas > 0 ? [{ id: "conversations", label, route, badge: naoLidas, badgeTone: "destructive", section, position }] : []);
509
536
  return null;
@@ -1637,17 +1664,13 @@ function useCaixaAoVivo() {
1637
1664
  const abertoRef = React11.useRef(selectedId);
1638
1665
  abertoRef.current = selectedId;
1639
1666
  React11.useEffect(() => {
1640
- const supabase = getSupabaseClientOptional();
1641
- if (!supabase?.channel || !tenantId) return;
1642
- const canal = supabase.channel(`conversas:${tenantId}`).on("broadcast", { event: "inbound" }, (msg2) => {
1667
+ if (!tenantId) return;
1668
+ return assinarChegada(tenantId, ({ conversationId }) => {
1643
1669
  void load();
1644
- if (msg2?.payload?.conversationId && msg2.payload.conversationId === abertoRef.current) {
1670
+ if (conversationId && conversationId === abertoRef.current) {
1645
1671
  void refreshMessages();
1646
1672
  }
1647
- }).subscribe();
1648
- return () => {
1649
- void supabase.removeChannel?.(canal);
1650
- };
1673
+ });
1651
1674
  }, [tenantId, load, refreshMessages]);
1652
1675
  }
1653
1676
  function InboxView() {