CBV01 commited on
Commit
d3be83c
·
1 Parent(s): 0bfcc32

feat(campaigns): real telegram metrics, fix failed card colors, compact card height, fix account avatar, and accurate group permission detection

Browse files
backend/main.py CHANGED
@@ -1095,7 +1095,7 @@ async def get_dialogs(req: FetchDialogsRequest, user_id: str = Depends(get_curre
1095
  is_group = bool(dialog.is_group)
1096
  is_channel = bool(dialog.is_channel)
1097
 
1098
- # Fix for Telethon Megagroups (Supergroups)
1099
  is_supergroup = False
1100
  username = None
1101
  participants_count = 0
@@ -1103,37 +1103,62 @@ async def get_dialogs(req: FetchDialogsRequest, user_id: str = Depends(get_curre
1103
  try:
1104
  if hasattr(dialog, 'entity') and dialog.entity:
1105
  ent = dialog.entity
1106
- username = getattr(ent, 'username', None)
1107
- participants_count = getattr(ent, 'participants_count', 0) or 0
1108
- if is_channel and hasattr(ent, 'megagroup'):
1109
- is_supergroup = bool(ent.megagroup) # type: ignore
1110
-
1111
- is_broadcast = bool(getattr(ent, 'broadcast', False))
1112
- is_creator = bool(getattr(ent, 'creator', False))
1113
- admin_rights = getattr(ent, 'admin_rights', None)
1114
- default_banned = getattr(ent, 'default_banned_rights', None)
1115
- banned = getattr(ent, 'banned_rights', None)
1116
- left = bool(getattr(ent, 'left', False))
1117
- kicked = bool(getattr(ent, 'kicked', False))
1118
- deactivated = bool(getattr(ent, 'deactivated', False))
1119
-
1120
- if left or kicked or deactivated:
1121
  can_send = False
1122
- elif is_creator:
1123
- can_send = True
1124
- elif is_broadcast:
1125
- # Telegram broadcast channel: ONLY admins with post_messages can post
1126
- can_send = bool(admin_rights and getattr(admin_rights, 'post_messages', False))
1127
  else:
1128
- # Group / Supergroup:
1129
- if admin_rights:
1130
- can_send = True
1131
- elif banned and getattr(banned, 'send_messages', False):
1132
- can_send = False
1133
- elif default_banned and getattr(default_banned, 'send_messages', False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1134
  can_send = False
1135
- else:
1136
  can_send = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1137
  except Exception as perm_err:
1138
  log_debug(f"DIALOGS_PERM_CHECK_ERR: {str(perm_err)}")
1139
  can_send = False
 
1095
  is_group = bool(dialog.is_group)
1096
  is_channel = bool(dialog.is_channel)
1097
 
1098
+ # Fix for Telethon Megagroups (Supergroups) vs Broadcast Channels vs Basic Groups
1099
  is_supergroup = False
1100
  username = None
1101
  participants_count = 0
 
1103
  try:
1104
  if hasattr(dialog, 'entity') and dialog.entity:
1105
  ent = dialog.entity
1106
+ ent_class = ent.__class__.__name__
1107
+
1108
+ # If entity is forbidden or empty, user has no access / banned
1109
+ if 'Forbidden' in ent_class or 'Empty' in ent_class:
 
 
 
 
 
 
 
 
 
 
 
1110
  can_send = False
 
 
 
 
 
1111
  else:
1112
+ username = getattr(ent, 'username', None)
1113
+ participants_count = getattr(ent, 'participants_count', 0) or 0
1114
+
1115
+ # In Telegram TL, supergroups have megagroup=True
1116
+ if is_channel and hasattr(ent, 'megagroup'):
1117
+ is_supergroup = bool(ent.megagroup) # type: ignore
1118
+
1119
+ # Any channel that is not a supergroup IS a broadcast channel
1120
+ is_broadcast = bool(getattr(ent, 'broadcast', False)) or (is_channel and not is_supergroup)
1121
+ is_creator = bool(getattr(ent, 'creator', False))
1122
+ admin_rights = getattr(ent, 'admin_rights', None)
1123
+ default_banned = getattr(ent, 'default_banned_rights', None)
1124
+ banned = getattr(ent, 'banned_rights', None)
1125
+ left = bool(getattr(ent, 'left', False))
1126
+ kicked = bool(getattr(ent, 'kicked', False))
1127
+ deactivated = bool(getattr(ent, 'deactivated', False))
1128
+ restricted = bool(getattr(ent, 'restricted', False))
1129
+ migrated_to = getattr(ent, 'migrated_to', None)
1130
+
1131
+ if left or kicked or deactivated or restricted or (migrated_to is not None):
1132
  can_send = False
1133
+ elif is_creator:
1134
  can_send = True
1135
+ elif is_broadcast:
1136
+ # Telegram broadcast channel: ONLY admins with post_messages can broadcast
1137
+ can_send = bool(admin_rights and getattr(admin_rights, 'post_messages', False))
1138
+ else:
1139
+ # Group / Supergroup
1140
+ if admin_rights:
1141
+ can_send = True
1142
+ else:
1143
+ # Check if group has banned regular members from sending messages
1144
+ banned_by_default = False
1145
+ if default_banned:
1146
+ if (getattr(default_banned, 'send_messages', False) or
1147
+ getattr(default_banned, 'send_plain', False)):
1148
+ banned_by_default = True
1149
+
1150
+ # Check if this user specifically has banned rights
1151
+ user_banned = False
1152
+ if banned:
1153
+ if (getattr(banned, 'send_messages', False) or
1154
+ getattr(banned, 'send_plain', False) or
1155
+ getattr(banned, 'view_messages', False)):
1156
+ user_banned = True
1157
+
1158
+ if banned_by_default or user_banned:
1159
+ can_send = False
1160
+ else:
1161
+ can_send = True
1162
  except Exception as perm_err:
1163
  log_debug(f"DIALOGS_PERM_CHECK_ERR: {str(perm_err)}")
1164
  can_send = False
frontend/next.config.js CHANGED
@@ -7,6 +7,10 @@ const nextConfig = {
7
  source: '/api/:path*',
8
  destination: `${normalized}/api/:path*`,
9
  },
 
 
 
 
10
  ];
11
  },
12
  };
 
7
  source: '/api/:path*',
8
  destination: `${normalized}/api/:path*`,
9
  },
10
+ {
11
+ source: '/static/:path*',
12
+ destination: `${normalized}/static/:path*`,
13
+ },
14
  ];
15
  },
16
  };
frontend/src/app/campaigns/page.tsx CHANGED
@@ -34,11 +34,56 @@ import {
34
  Filter,
35
  Smartphone
36
  } from 'lucide-react';
37
- import { apiFetch } from '@/lib/auth';
38
  import { Preloader } from '@/components/Preloader';
39
  import { SpintaxModal } from '@/components/SpintaxModal';
40
  import { TemplateAiAssistantModal } from '@/components/TemplateAiAssistantModal';
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  export default function CampaignsPage() {
43
  const [isCreating, setIsCreating] = useState(false);
44
  const [campaigns, setCampaigns] = useState<any[]>([]);
@@ -514,7 +559,7 @@ export default function CampaignsPage() {
514
  accentText: 'text-[#2dd4bf]',
515
  ringColor: '#2dd4bf',
516
  statusText: 'COMPLETED',
517
- actionBtn: 'bg-white/[0.04] text-neutral-300 hover:bg-white/[0.08] border border-white/[0.08]'
518
  };
519
  case 'processing':
520
  case 'active':
@@ -538,7 +583,7 @@ export default function CampaignsPage() {
538
  accentText: 'text-[#fbbf24]',
539
  ringColor: '#fbbf24',
540
  statusText: 'PAUSED',
541
- actionBtn: 'bg-[#12281d] text-[#34d399] hover:bg-[#183526] border border-emerald-500/30'
542
  };
543
  case 'failed':
544
  case 'error':
@@ -550,18 +595,18 @@ export default function CampaignsPage() {
550
  accentText: 'text-[#fb7185]',
551
  ringColor: '#fb7185',
552
  statusText: 'FAILED',
553
- actionBtn: 'bg-rose-950/70 text-rose-400 hover:bg-rose-900/70 border border-rose-500/30'
554
  };
555
  default:
556
  return {
557
- topBeamColor: 'from-transparent via-white/20 to-transparent',
558
- radialGlow: 'none',
559
- iconBg: 'bg-[#1a1e24] text-neutral-400',
560
- dotColor: 'bg-neutral-400',
561
- accentText: 'text-neutral-400',
562
- ringColor: '#64748b',
563
  statusText: status ? status.toUpperCase() : 'DRAFT',
564
- actionBtn: 'bg-[#102a33] text-[#38bdf8] hover:bg-[#153844] border border-cyan-500/30'
565
  };
566
  }
567
  };
@@ -706,12 +751,13 @@ export default function CampaignsPage() {
706
  </div>
707
  ) : viewMode === 'grid' ? (
708
  /* Natural Modern Grid Cards - Two Per Line (Matching Screenshot) */
709
- <div className="grid grid-cols-1 lg:grid-cols-2 gap-5">
710
  {filteredCampaigns.map((camp) => {
711
  const progressPct = Math.min(100, Math.round(((camp.sent_count || 0) / Math.max(1, camp.total_targets || 1)) * 100));
712
  let failedCount = 0;
713
  try {
714
- failedCount = JSON.parse(camp.failed_groups || '[]').length;
 
715
  } catch (e) { }
716
 
717
  let parsedGroups: string[] = [];
@@ -729,7 +775,7 @@ export default function CampaignsPage() {
729
  ? (senderAccount.first_name ? `${senderAccount.first_name} ${senderAccount.last_name || ''}`.trim() : (senderAccount.username ? `@${senderAccount.username}` : senderAccount.phone_number))
730
  : (camp.phone_number || "Telegram Fleet Node");
731
 
732
- const radius = 24;
733
  const circumference = 2 * Math.PI * radius;
734
  const strokeDashoffset = circumference - (progressPct / 100) * circumference;
735
 
@@ -747,7 +793,7 @@ export default function CampaignsPage() {
747
  return (
748
  <div
749
  key={camp.id}
750
- className="relative rounded-2xl bg-[#111513] border border-white/[0.05] p-6 transition-all duration-300 hover:translate-y-[-2px] flex flex-col justify-between group/card shadow-sm overflow-hidden"
751
  style={{ backgroundImage: cardStyle.radialGlow !== 'none' ? cardStyle.radialGlow : undefined }}
752
  >
753
  {/* Angled Glowing Top Beam */}
@@ -757,19 +803,19 @@ export default function CampaignsPage() {
757
 
758
  <div>
759
  {/* Header: Icon, Title, Status & Action Button */}
760
- <div className="flex items-start justify-between gap-3 mb-2.5">
761
- <div className="flex items-center gap-3 min-w-0">
762
  {/* Status Icon with Lightning Badge */}
763
- <div className={`w-11 h-11 rounded-2xl flex items-center justify-center shrink-0 ${cardStyle.iconBg}`}>
764
- <Zap size={20} className="fill-current" />
765
  </div>
766
  <div className="min-w-0">
767
- <h3 className="font-bold text-white text-base tracking-tight truncate leading-snug">
768
  {camp.name || "Broadcast Campaign"}
769
  </h3>
770
- <div className="flex items-center gap-2 mt-0.5">
771
- <span className={`w-2 h-2 rounded-full ${cardStyle.dotColor}`} />
772
- <span className={`text-[11px] font-bold uppercase tracking-wider ${cardStyle.accentText}`}>
773
  {cardStyle.statusText}
774
  </span>
775
  </div>
@@ -778,92 +824,106 @@ export default function CampaignsPage() {
778
 
779
  {/* Action Button & Menu */}
780
  <div className="flex items-center gap-1 shrink-0">
781
- {camp.status !== 'completed' ? (
782
  <button
783
  onClick={() => handleTogglePause(camp.id)}
784
- className={`px-3.5 py-1.5 rounded-xl text-xs font-semibold tracking-wide transition-all flex items-center gap-1.5 cursor-pointer ${cardStyle.actionBtn}`}
785
- title={camp.status === 'paused' ? 'Resume Campaign' : 'Pause Campaign'}
786
  >
787
- {camp.status === 'paused' ? (
788
- <>
789
- <Play size={12} className="fill-current" />
790
- <span>RESUME</span>
791
- </>
792
- ) : (
793
- <>
794
- <Pause size={12} />
795
- <span>PAUSE</span>
796
- </>
797
- )}
798
  </button>
799
- ) : (
800
- <div className="px-3 py-1.5 rounded-xl text-xs font-semibold text-neutral-400 bg-white/[0.04]">
 
 
 
 
 
 
 
 
 
801
  COMPLETED
802
  </div>
 
 
 
 
 
 
 
 
 
803
  )}
804
 
805
  <button
806
  onClick={() => handleEdit(camp)}
807
- className="p-1.5 text-neutral-500 hover:text-white rounded-lg transition-colors cursor-pointer"
808
  title="Edit Campaign"
809
  >
810
- <Edit3 size={14} />
811
  </button>
812
  <button
813
  onClick={() => handleDelete(camp.id)}
814
- className="p-1.5 text-neutral-500 hover:text-rose-400 rounded-lg transition-colors cursor-pointer"
815
  title="Delete Campaign"
816
  >
817
- <Trash2 size={14} />
818
  </button>
819
  </div>
820
  </div>
821
 
822
- {/* Meta Row: Sender Account & Date (Clean, Unboxed - like in screenshot) */}
823
- <div className="flex flex-wrap items-center gap-2 text-xs text-neutral-400 mt-2 mb-4 font-normal">
824
- <div className="flex items-center gap-1.5 truncate max-w-[280px]">
825
- <Smartphone size={13} className="text-neutral-500 shrink-0" />
826
- <span className="text-neutral-300 font-medium truncate">{accountDisplayName}</span>
827
- <span className="text-neutral-500 font-mono text-[11px] shrink-0">({camp.phone_number})</span>
 
 
 
 
 
828
  </div>
829
 
830
- <span className="text-neutral-600">|</span>
831
 
832
- <div className="flex items-center gap-1.5 shrink-0 text-neutral-400">
833
- <Calendar size={13} className="text-neutral-500" />
834
  <span>{formattedDate}</span>
835
  </div>
836
 
837
  {camp.media_url && (
838
  <>
839
- <span className="text-neutral-600">|</span>
840
- <span className="flex items-center gap-1 text-[11px] font-medium text-indigo-400">
841
- <Paperclip size={11} /> Media
842
  </span>
843
  </>
844
  )}
845
  </div>
846
 
847
- {/* Middle Section: Circular Gauge + 4 Metric Stat Boxes (Exact layout from screenshot) */}
848
- <div className="flex items-center gap-4 my-2">
849
  {/* Circular Progress Gauge */}
850
- <div className="relative w-16 h-16 shrink-0 flex items-center justify-center">
851
- <svg className="w-16 h-16 -rotate-90" viewBox="0 0 64 64">
852
  <circle
853
- cx="32"
854
- cy="32"
855
  r={radius}
856
  stroke="currentColor"
857
- strokeWidth="5"
858
  fill="transparent"
859
- className="text-white/[0.08]"
860
  />
861
  <circle
862
- cx="32"
863
- cy="32"
864
  r={radius}
865
  stroke={cardStyle.ringColor}
866
- strokeWidth="5"
867
  strokeDasharray={circumference}
868
  strokeDashoffset={strokeDashoffset}
869
  strokeLinecap="round"
@@ -872,73 +932,77 @@ export default function CampaignsPage() {
872
  />
873
  </svg>
874
  <div className="absolute inset-0 flex items-center justify-center">
875
- <span className="text-xs font-bold font-mono text-white">
876
- {['pending', 'scheduled', 'draft'].includes(camp.status) && progressPct === 0 ? 'DRAFT' : `${progressPct}%`}
877
- </span>
 
 
 
 
878
  </div>
879
  </div>
880
 
881
- {/* 2x2 Metric Grid (Natural Dark Tiles, NO Harsh Borders) */}
882
- <div className="grid grid-cols-2 gap-2 flex-1">
883
- <div className="bg-white/[0.03] rounded-xl px-3.5 py-2.5">
884
- <div className="text-[10px] font-semibold uppercase tracking-wider text-neutral-400">
885
- Recipients
886
  </div>
887
- <div className="text-base font-bold text-white font-mono mt-0.5">
888
- {camp.total_targets || 0}
889
  </div>
890
  </div>
891
 
892
- <div className="bg-white/[0.03] rounded-xl px-3.5 py-2.5">
893
- <div className="text-[10px] font-semibold uppercase tracking-wider text-neutral-400">
894
- Sent
895
  </div>
896
- <div className="text-base font-bold text-white font-mono mt-0.5">
897
  {camp.sent_count || 0}
898
  </div>
899
  </div>
900
 
901
- <div className="bg-white/[0.03] rounded-xl px-3.5 py-2.5">
902
- <div className="text-[10px] font-semibold uppercase tracking-wider text-neutral-400 flex items-center justify-between">
903
- <span>Open Rate</span>
904
  {failedCount > 0 && (
905
- <span className="text-[9px] font-bold text-rose-400">
906
  {failedCount} err
907
  </span>
908
  )}
909
  </div>
910
- <div className="text-base font-bold text-emerald-400 font-mono mt-0.5">
911
  {progressPct}%
912
  </div>
913
  </div>
914
 
915
- <div className="bg-white/[0.03] rounded-xl px-3.5 py-2.5">
916
- <div className="text-[10px] font-semibold uppercase tracking-wider text-neutral-400">
917
- Interval
918
  </div>
919
- <div className="text-base font-bold text-sky-400 font-mono mt-0.5 truncate">
920
  {camp.interval_hours > 0 || camp.interval_minutes > 0
921
- ? `${camp.interval_hours > 0 ? `${camp.interval_hours}h ` : ''}${camp.interval_minutes > 0 ? `${camp.interval_minutes}m` : ''}`
922
- : 'Single'}
923
  </div>
924
  </div>
925
  </div>
926
  </div>
927
 
928
  {/* Message Snippet */}
929
- <div className="mt-3.5 px-3.5 py-2 rounded-xl bg-white/[0.02] text-[11px] text-neutral-400 font-mono line-clamp-2 leading-relaxed">
930
  "{camp.message_text || camp.message || '...'}"
931
  </div>
932
  </div>
933
 
934
  {/* Footer: Target Entities */}
935
- <div className="mt-3 pt-3 border-t border-white/[0.04] flex items-center justify-between text-xs text-neutral-400 font-medium">
936
  <div className="flex items-center gap-1.5">
937
- <Users size={12} className="text-neutral-500" />
938
  <span>{parsedGroups.length} Telegram {parsedGroups.length === 1 ? 'Target' : 'Targets'}</span>
939
  </div>
940
  {camp.batch_number && camp.batch_number > 1 && (
941
- <span className="text-[10px] font-mono text-neutral-500">Batch #{camp.batch_number}</span>
942
  )}
943
  </div>
944
  </div>
@@ -947,7 +1011,7 @@ export default function CampaignsPage() {
947
  </div>
948
  ) : (
949
  /* List View Upgraded */
950
- <div className="space-y-3">
951
  {filteredCampaigns.map((camp) => {
952
  const progressPct = Math.min(100, Math.round(((camp.sent_count || 0) / Math.max(1, camp.total_targets || 1)) * 100));
953
  const cardStyle = getCampaignCardStyle(camp.status);
@@ -959,32 +1023,37 @@ export default function CampaignsPage() {
959
  return (
960
  <div
961
  key={camp.id}
962
- className="relative rounded-2xl bg-[#111513] border border-white/[0.05] p-4 transition-all flex flex-col md:flex-row md:items-center justify-between gap-4 group/card overflow-hidden"
963
  style={{ backgroundImage: cardStyle.radialGlow !== 'none' ? cardStyle.radialGlow : undefined }}
964
  >
965
  {/* Angled Glowing Top Beam */}
966
  <div
967
  className={`absolute -top-px left-[20%] right-[20%] h-[2px] bg-gradient-to-r ${cardStyle.topBeamColor} pointer-events-none`}
968
  />
969
- <div className="flex items-center gap-3.5 min-w-0">
970
- <div className={`w-11 h-11 rounded-2xl flex items-center justify-center shrink-0 ${cardStyle.iconBg}`}>
971
- <Zap size={20} className="fill-current" />
972
  </div>
973
  <div className="min-w-0">
974
  <div className="flex items-center gap-2">
975
  <h4 className="font-bold text-white text-sm tracking-tight truncate">{camp.name || "Broadcast Campaign"}</h4>
976
- <span className={`w-2 h-2 rounded-full ${cardStyle.dotColor}`} />
977
  <span className={`text-[9px] font-bold uppercase tracking-wider ${cardStyle.accentText}`}>{cardStyle.statusText}</span>
978
  </div>
979
- <div className="flex flex-wrap items-center gap-2.5 text-[11px] text-neutral-400 font-medium mt-1">
980
- <span className="flex items-center gap-1 font-semibold text-neutral-300">
981
- <Smartphone size={11} className="text-neutral-500" />
982
- {accountDisplayName}
983
- </span>
 
 
 
 
 
984
  <span>•</span>
985
  <span className="font-mono text-neutral-500">{camp.phone_number}</span>
986
  <span>•</span>
987
- <span>{camp.sent_count || 0} / {camp.total_targets || 0} targets ({progressPct}%)</span>
988
  {camp.media_url && (
989
  <>
990
  <span>•</span>
@@ -997,30 +1066,52 @@ export default function CampaignsPage() {
997
  </div>
998
  </div>
999
 
1000
- <div className="flex items-center gap-2.5 shrink-0 self-end md:self-auto">
1001
- {camp.status !== 'completed' && (
1002
  <button
1003
  onClick={() => handleTogglePause(camp.id)}
1004
- className={`px-3 py-1.5 rounded-xl text-xs font-semibold transition-all flex items-center gap-1.5 cursor-pointer ${cardStyle.actionBtn}`}
1005
- title={camp.status === 'paused' ? 'Resume' : 'Pause'}
1006
  >
1007
- {camp.status === 'paused' ? <Play size={12} className="fill-current" /> : <Pause size={12} />}
1008
- <span>{camp.status === 'paused' ? 'RESUME' : 'PAUSE'}</span>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1009
  </button>
1010
  )}
1011
  <button
1012
  onClick={() => handleEdit(camp)}
1013
- className="p-1.5 text-neutral-500 hover:text-white rounded-lg transition-colors cursor-pointer"
1014
  title="Edit"
1015
  >
1016
- <Edit3 size={14} />
1017
  </button>
1018
  <button
1019
  onClick={() => handleDelete(camp.id)}
1020
- className="p-1.5 text-neutral-500 hover:text-rose-400 rounded-lg transition-colors cursor-pointer"
1021
  title="Delete"
1022
  >
1023
- <Trash2 size={14} />
1024
  </button>
1025
  </div>
1026
  </div>
@@ -1131,14 +1222,12 @@ export default function CampaignsPage() {
1131
  <div className="flex items-center gap-2.5 min-w-0">
1132
  {selectedAcc ? (
1133
  <>
1134
- <div className="relative w-6 h-6 rounded-lg bg-indigo-500/20 text-indigo-400 font-bold text-[10px] flex items-center justify-center shrink-0 border border-indigo-500/30 overflow-hidden">
1135
- {selectedAcc.profile_photo ? (
1136
- <img src={selectedAcc.profile_photo} alt="" className="w-full h-full object-cover" />
1137
- ) : (
1138
- <span>{selectedAcc.first_name?.charAt(0) || selectedAcc.phone_number?.slice(-2) || 'A'}</span>
1139
- )}
1140
- <span className="absolute -bottom-0.5 -right-0.5 w-2 h-2 rounded-full bg-emerald-400 ring-1 ring-card" />
1141
- </div>
1142
  <div className="text-left truncate">
1143
  <span className="font-bold text-foreground block truncate leading-tight">
1144
  {selectedAcc.first_name ? `${selectedAcc.first_name} ${selectedAcc.last_name || ''}` : selectedAcc.phone_number}
@@ -1176,14 +1265,12 @@ export default function CampaignsPage() {
1176
  }`}
1177
  >
1178
  <div className="flex items-center gap-2.5 min-w-0">
1179
- <div className="relative w-7 h-7 rounded-lg bg-indigo-500/15 text-indigo-400 font-bold text-xs flex items-center justify-center shrink-0 border border-indigo-500/25 overflow-hidden">
1180
- {acc.profile_photo ? (
1181
- <img src={acc.profile_photo} alt="" className="w-full h-full object-cover" />
1182
- ) : (
1183
- <span>{acc.first_name?.charAt(0) || acc.phone_number?.slice(-2) || 'A'}</span>
1184
- )}
1185
- <span className="absolute -bottom-0.5 -right-0.5 w-2 h-2 rounded-full bg-emerald-400 ring-1 ring-card" />
1186
- </div>
1187
  <div className="text-left min-w-0">
1188
  <div className="font-bold text-xs truncate text-foreground flex items-center gap-1.5">
1189
  <span>{acc.first_name ? `${acc.first_name} ${acc.last_name || ''}` : acc.phone_number}</span>
 
34
  Filter,
35
  Smartphone
36
  } from 'lucide-react';
37
+ import { apiFetch, getApiBase } from '@/lib/auth';
38
  import { Preloader } from '@/components/Preloader';
39
  import { SpintaxModal } from '@/components/SpintaxModal';
40
  import { TemplateAiAssistantModal } from '@/components/TemplateAiAssistantModal';
41
 
42
+ function TelegramAvatar({
43
+ photo,
44
+ name,
45
+ phone,
46
+ size = "w-7 h-7 text-xs",
47
+ ringClass = "border-indigo-500/30"
48
+ }: {
49
+ photo?: string | null;
50
+ name?: string;
51
+ phone?: string;
52
+ size?: string;
53
+ ringClass?: string;
54
+ }) {
55
+ const [hasError, setHasError] = React.useState(false);
56
+ const initial = (name?.trim()?.charAt(0) || phone?.slice(-2) || 'A').toUpperCase();
57
+
58
+ const resolvedSrc = React.useMemo(() => {
59
+ if (!photo || hasError) return null;
60
+ if (photo.startsWith('http://') || photo.startsWith('https://')) return photo;
61
+ try {
62
+ const apiBase = getApiBase();
63
+ const cleanPath = photo.startsWith('/') ? photo : `/${photo}`;
64
+ return `${apiBase}${cleanPath}`;
65
+ } catch {
66
+ return photo;
67
+ }
68
+ }, [photo, hasError]);
69
+
70
+ return (
71
+ <div className={`relative ${size} rounded-lg bg-gradient-to-br from-indigo-500/20 via-purple-500/15 to-indigo-600/20 text-indigo-300 font-bold flex items-center justify-center shrink-0 border ${ringClass} overflow-hidden shadow-xs select-none`}>
72
+ {resolvedSrc ? (
73
+ <img
74
+ src={resolvedSrc}
75
+ alt={name || 'Account'}
76
+ className="w-full h-full object-cover"
77
+ onError={() => setHasError(true)}
78
+ />
79
+ ) : (
80
+ <span className="leading-none">{initial}</span>
81
+ )}
82
+ <span className="absolute -bottom-0.5 -right-0.5 w-2 h-2 rounded-full bg-emerald-400 ring-1 ring-[#111513]" />
83
+ </div>
84
+ );
85
+ }
86
+
87
  export default function CampaignsPage() {
88
  const [isCreating, setIsCreating] = useState(false);
89
  const [campaigns, setCampaigns] = useState<any[]>([]);
 
559
  accentText: 'text-[#2dd4bf]',
560
  ringColor: '#2dd4bf',
561
  statusText: 'COMPLETED',
562
+ actionBtn: 'bg-teal-500/15 text-teal-300 hover:bg-teal-500/25 border border-teal-500/30'
563
  };
564
  case 'processing':
565
  case 'active':
 
583
  accentText: 'text-[#fbbf24]',
584
  ringColor: '#fbbf24',
585
  statusText: 'PAUSED',
586
+ actionBtn: 'bg-amber-500/15 text-amber-300 hover:bg-amber-500/25 border border-amber-500/30'
587
  };
588
  case 'failed':
589
  case 'error':
 
595
  accentText: 'text-[#fb7185]',
596
  ringColor: '#fb7185',
597
  statusText: 'FAILED',
598
+ actionBtn: 'bg-rose-500/15 text-rose-300 hover:bg-rose-500/25 border border-rose-500/30'
599
  };
600
  default:
601
  return {
602
+ topBeamColor: 'from-transparent via-sky-400/60 to-transparent',
603
+ radialGlow: 'radial-gradient(circle at 50% -20%, rgba(56, 189, 248, 0.08) 0%, transparent 60%)',
604
+ iconBg: 'bg-[#10222a] text-[#38bdf8]',
605
+ dotColor: 'bg-[#38bdf8]',
606
+ accentText: 'text-[#38bdf8]',
607
+ ringColor: '#38bdf8',
608
  statusText: status ? status.toUpperCase() : 'DRAFT',
609
+ actionBtn: 'bg-sky-500/15 text-sky-300 hover:bg-sky-500/25 border border-sky-500/30'
610
  };
611
  }
612
  };
 
751
  </div>
752
  ) : viewMode === 'grid' ? (
753
  /* Natural Modern Grid Cards - Two Per Line (Matching Screenshot) */
754
+ <div className="grid grid-cols-1 lg:grid-cols-2 gap-4.5">
755
  {filteredCampaigns.map((camp) => {
756
  const progressPct = Math.min(100, Math.round(((camp.sent_count || 0) / Math.max(1, camp.total_targets || 1)) * 100));
757
  let failedCount = 0;
758
  try {
759
+ const parsedFailed = JSON.parse(camp.failed_groups || '[]');
760
+ failedCount = Array.isArray(parsedFailed) ? parsedFailed.length : 0;
761
  } catch (e) { }
762
 
763
  let parsedGroups: string[] = [];
 
775
  ? (senderAccount.first_name ? `${senderAccount.first_name} ${senderAccount.last_name || ''}`.trim() : (senderAccount.username ? `@${senderAccount.username}` : senderAccount.phone_number))
776
  : (camp.phone_number || "Telegram Fleet Node");
777
 
778
+ const radius = 22;
779
  const circumference = 2 * Math.PI * radius;
780
  const strokeDashoffset = circumference - (progressPct / 100) * circumference;
781
 
 
793
  return (
794
  <div
795
  key={camp.id}
796
+ className="relative rounded-2xl bg-[#111513] border border-white/[0.05] p-4 sm:p-5 transition-all duration-300 hover:translate-y-[-2px] flex flex-col justify-between group/card shadow-sm overflow-hidden"
797
  style={{ backgroundImage: cardStyle.radialGlow !== 'none' ? cardStyle.radialGlow : undefined }}
798
  >
799
  {/* Angled Glowing Top Beam */}
 
803
 
804
  <div>
805
  {/* Header: Icon, Title, Status & Action Button */}
806
+ <div className="flex items-start justify-between gap-3 mb-2">
807
+ <div className="flex items-center gap-2.5 min-w-0">
808
  {/* Status Icon with Lightning Badge */}
809
+ <div className={`w-9 h-9 rounded-xl flex items-center justify-center shrink-0 ${cardStyle.iconBg}`}>
810
+ <Zap size={16} className="fill-current" />
811
  </div>
812
  <div className="min-w-0">
813
+ <h3 className="font-bold text-white text-sm sm:text-base tracking-tight truncate leading-tight">
814
  {camp.name || "Broadcast Campaign"}
815
  </h3>
816
+ <div className="flex items-center gap-1.5 mt-0.5">
817
+ <span className={`w-1.5 h-1.5 rounded-full ${cardStyle.dotColor}`} />
818
+ <span className={`text-[10px] font-bold uppercase tracking-wider ${cardStyle.accentText}`}>
819
  {cardStyle.statusText}
820
  </span>
821
  </div>
 
824
 
825
  {/* Action Button & Menu */}
826
  <div className="flex items-center gap-1 shrink-0">
827
+ {camp.status === 'failed' || camp.status === 'error' ? (
828
  <button
829
  onClick={() => handleTogglePause(camp.id)}
830
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold tracking-wide transition-all flex items-center gap-1.5 cursor-pointer bg-rose-500/15 text-rose-300 hover:bg-rose-500/25 border border-rose-500/30"
831
+ title="Retry Failed Campaign"
832
  >
833
+ <Play size={11} className="fill-current text-rose-400" />
834
+ <span>RETRY</span>
 
 
 
 
 
 
 
 
 
835
  </button>
836
+ ) : camp.status === 'paused' ? (
837
+ <button
838
+ onClick={() => handleTogglePause(camp.id)}
839
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold tracking-wide transition-all flex items-center gap-1.5 cursor-pointer bg-amber-500/15 text-amber-300 hover:bg-amber-500/25 border border-amber-500/30"
840
+ title="Resume Campaign"
841
+ >
842
+ <Play size={11} className="fill-current text-amber-400" />
843
+ <span>RESUME</span>
844
+ </button>
845
+ ) : camp.status === 'completed' ? (
846
+ <div className="px-2.5 py-1 rounded-lg text-xs font-semibold text-teal-300 bg-teal-500/15 border border-teal-500/30">
847
  COMPLETED
848
  </div>
849
+ ) : (
850
+ <button
851
+ onClick={() => handleTogglePause(camp.id)}
852
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold tracking-wide transition-all flex items-center gap-1.5 cursor-pointer bg-white/[0.04] text-neutral-300 hover:bg-white/[0.08] border border-white/[0.08]"
853
+ title="Pause Campaign"
854
+ >
855
+ <Pause size={11} />
856
+ <span>PAUSE</span>
857
+ </button>
858
  )}
859
 
860
  <button
861
  onClick={() => handleEdit(camp)}
862
+ className="p-1 text-neutral-500 hover:text-white rounded-lg transition-colors cursor-pointer"
863
  title="Edit Campaign"
864
  >
865
+ <Edit3 size={13} />
866
  </button>
867
  <button
868
  onClick={() => handleDelete(camp.id)}
869
+ className="p-1 text-neutral-500 hover:text-rose-400 rounded-lg transition-colors cursor-pointer"
870
  title="Delete Campaign"
871
  >
872
+ <Trash2 size={13} />
873
  </button>
874
  </div>
875
  </div>
876
 
877
+ {/* Meta Row: Sender Account Avatar & Date */}
878
+ <div className="flex flex-wrap items-center gap-2 text-[11px] text-neutral-400 mt-1 mb-2.5 font-normal">
879
+ <div className="flex items-center gap-1.5 min-w-0 max-w-[260px]">
880
+ <TelegramAvatar
881
+ photo={senderAccount?.profile_photo}
882
+ name={senderAccount?.first_name || camp.phone_number}
883
+ phone={camp.phone_number}
884
+ size="w-5 h-5 text-[9px]"
885
+ />
886
+ <span className="text-neutral-200 font-semibold truncate">{accountDisplayName}</span>
887
+ <span className="text-neutral-500 font-mono text-[10px] shrink-0">({camp.phone_number})</span>
888
  </div>
889
 
890
+ <span className="text-neutral-700">|</span>
891
 
892
+ <div className="flex items-center gap-1 shrink-0 text-neutral-400">
893
+ <Calendar size={11} className="text-neutral-500" />
894
  <span>{formattedDate}</span>
895
  </div>
896
 
897
  {camp.media_url && (
898
  <>
899
+ <span className="text-neutral-700">|</span>
900
+ <span className="flex items-center gap-1 text-[10px] font-bold text-indigo-400">
901
+ <Paperclip size={10} /> Media
902
  </span>
903
  </>
904
  )}
905
  </div>
906
 
907
+ {/* Middle Section: Circular Gauge + 4 Real Telegram Metric Tiles */}
908
+ <div className="flex items-center gap-3 my-1">
909
  {/* Circular Progress Gauge */}
910
+ <div className="relative w-14 h-14 shrink-0 flex items-center justify-center">
911
+ <svg className="w-14 h-14 -rotate-90" viewBox="0 0 56 56">
912
  <circle
913
+ cx="28"
914
+ cy="28"
915
  r={radius}
916
  stroke="currentColor"
917
+ strokeWidth="4.5"
918
  fill="transparent"
919
+ className="text-white/[0.06]"
920
  />
921
  <circle
922
+ cx="28"
923
+ cy="28"
924
  r={radius}
925
  stroke={cardStyle.ringColor}
926
+ strokeWidth="4.5"
927
  strokeDasharray={circumference}
928
  strokeDashoffset={strokeDashoffset}
929
  strokeLinecap="round"
 
932
  />
933
  </svg>
934
  <div className="absolute inset-0 flex items-center justify-center">
935
+ {camp.status === 'failed' || camp.status === 'error' ? (
936
+ <span className="text-[10px] font-black font-mono text-rose-400">FAILED</span>
937
+ ) : ['pending', 'scheduled', 'draft'].includes(camp.status) && progressPct === 0 ? (
938
+ <span className="text-[10px] font-bold font-mono text-neutral-400">DRAFT</span>
939
+ ) : (
940
+ <span className="text-[11px] font-bold font-mono text-white">{progressPct}%</span>
941
+ )}
942
  </div>
943
  </div>
944
 
945
+ {/* 2x2 Metric Grid (Real Telegram Broadcast Metrics) */}
946
+ <div className="grid grid-cols-2 gap-1.5 flex-1">
947
+ <div className="bg-white/[0.03] rounded-lg px-2.5 py-1.5">
948
+ <div className="text-[9px] font-bold uppercase tracking-wider text-neutral-400">
949
+ Target Groups
950
  </div>
951
+ <div className="text-sm font-bold text-white font-mono mt-0.5">
952
+ {camp.total_targets || parsedGroups.length || 0}
953
  </div>
954
  </div>
955
 
956
+ <div className="bg-white/[0.03] rounded-lg px-2.5 py-1.5">
957
+ <div className="text-[9px] font-bold uppercase tracking-wider text-neutral-400">
958
+ Delivered
959
  </div>
960
+ <div className="text-sm font-bold text-white font-mono mt-0.5">
961
  {camp.sent_count || 0}
962
  </div>
963
  </div>
964
 
965
+ <div className="bg-white/[0.03] rounded-lg px-2.5 py-1.5">
966
+ <div className="text-[9px] font-bold uppercase tracking-wider text-neutral-400 flex items-center justify-between">
967
+ <span>Delivery Rate</span>
968
  {failedCount > 0 && (
969
+ <span className="text-[8px] font-bold text-rose-400 bg-rose-500/10 px-1 py-0.2 rounded">
970
  {failedCount} err
971
  </span>
972
  )}
973
  </div>
974
+ <div className={`text-sm font-bold font-mono mt-0.5 ${cardStyle.accentText}`}>
975
  {progressPct}%
976
  </div>
977
  </div>
978
 
979
+ <div className="bg-white/[0.03] rounded-lg px-2.5 py-1.5">
980
+ <div className="text-[9px] font-bold uppercase tracking-wider text-neutral-400">
981
+ Cadence
982
  </div>
983
+ <div className="text-sm font-bold text-sky-400 font-mono mt-0.5 truncate">
984
  {camp.interval_hours > 0 || camp.interval_minutes > 0
985
+ ? `${camp.interval_hours > 0 ? `${camp.interval_hours}h ` : ''}${camp.interval_minutes > 0 ? `${camp.interval_minutes}m` : ''}`.trim()
986
+ : 'Single Run'}
987
  </div>
988
  </div>
989
  </div>
990
  </div>
991
 
992
  {/* Message Snippet */}
993
+ <div className="mt-2 px-2.5 py-1 rounded-lg bg-white/[0.02] text-[10px] text-neutral-400 font-mono truncate leading-normal">
994
  "{camp.message_text || camp.message || '...'}"
995
  </div>
996
  </div>
997
 
998
  {/* Footer: Target Entities */}
999
+ <div className="mt-2 pt-2 border-t border-white/[0.04] flex items-center justify-between text-[11px] text-neutral-400 font-medium">
1000
  <div className="flex items-center gap-1.5">
1001
+ <Users size={11} className="text-neutral-500" />
1002
  <span>{parsedGroups.length} Telegram {parsedGroups.length === 1 ? 'Target' : 'Targets'}</span>
1003
  </div>
1004
  {camp.batch_number && camp.batch_number > 1 && (
1005
+ <span className="text-[9px] font-mono text-neutral-500">Batch #{camp.batch_number}</span>
1006
  )}
1007
  </div>
1008
  </div>
 
1011
  </div>
1012
  ) : (
1013
  /* List View Upgraded */
1014
+ <div className="space-y-2.5">
1015
  {filteredCampaigns.map((camp) => {
1016
  const progressPct = Math.min(100, Math.round(((camp.sent_count || 0) / Math.max(1, camp.total_targets || 1)) * 100));
1017
  const cardStyle = getCampaignCardStyle(camp.status);
 
1023
  return (
1024
  <div
1025
  key={camp.id}
1026
+ className="relative rounded-2xl bg-[#111513] border border-white/[0.05] p-3.5 sm:p-4 transition-all flex flex-col md:flex-row md:items-center justify-between gap-3.5 group/card overflow-hidden"
1027
  style={{ backgroundImage: cardStyle.radialGlow !== 'none' ? cardStyle.radialGlow : undefined }}
1028
  >
1029
  {/* Angled Glowing Top Beam */}
1030
  <div
1031
  className={`absolute -top-px left-[20%] right-[20%] h-[2px] bg-gradient-to-r ${cardStyle.topBeamColor} pointer-events-none`}
1032
  />
1033
+ <div className="flex items-center gap-3 min-w-0">
1034
+ <div className={`w-9 h-9 rounded-xl flex items-center justify-center shrink-0 ${cardStyle.iconBg}`}>
1035
+ <Zap size={16} className="fill-current" />
1036
  </div>
1037
  <div className="min-w-0">
1038
  <div className="flex items-center gap-2">
1039
  <h4 className="font-bold text-white text-sm tracking-tight truncate">{camp.name || "Broadcast Campaign"}</h4>
1040
+ <span className={`w-1.5 h-1.5 rounded-full ${cardStyle.dotColor}`} />
1041
  <span className={`text-[9px] font-bold uppercase tracking-wider ${cardStyle.accentText}`}>{cardStyle.statusText}</span>
1042
  </div>
1043
+ <div className="flex flex-wrap items-center gap-2 text-[11px] text-neutral-400 font-medium mt-1">
1044
+ <div className="flex items-center gap-1.5 font-semibold text-neutral-300">
1045
+ <TelegramAvatar
1046
+ photo={senderAccount?.profile_photo}
1047
+ name={senderAccount?.first_name || camp.phone_number}
1048
+ phone={camp.phone_number}
1049
+ size="w-4 h-4 text-[8px]"
1050
+ />
1051
+ <span>{accountDisplayName}</span>
1052
+ </div>
1053
  <span>•</span>
1054
  <span className="font-mono text-neutral-500">{camp.phone_number}</span>
1055
  <span>•</span>
1056
+ <span>{camp.sent_count || 0} / {camp.total_targets || 0} delivered ({progressPct}%)</span>
1057
  {camp.media_url && (
1058
  <>
1059
  <span>•</span>
 
1066
  </div>
1067
  </div>
1068
 
1069
+ <div className="flex items-center gap-2 shrink-0 self-end md:self-auto">
1070
+ {camp.status === 'failed' || camp.status === 'error' ? (
1071
  <button
1072
  onClick={() => handleTogglePause(camp.id)}
1073
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold transition-all flex items-center gap-1.5 cursor-pointer bg-rose-500/15 text-rose-300 hover:bg-rose-500/25 border border-rose-500/30"
1074
+ title="Retry"
1075
  >
1076
+ <Play size={11} className="fill-current text-rose-400" />
1077
+ <span>RETRY</span>
1078
+ </button>
1079
+ ) : camp.status === 'paused' ? (
1080
+ <button
1081
+ onClick={() => handleTogglePause(camp.id)}
1082
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold transition-all flex items-center gap-1.5 cursor-pointer bg-amber-500/15 text-amber-300 hover:bg-amber-500/25 border border-amber-500/30"
1083
+ title="Resume"
1084
+ >
1085
+ <Play size={11} className="fill-current text-amber-400" />
1086
+ <span>RESUME</span>
1087
+ </button>
1088
+ ) : camp.status === 'completed' ? (
1089
+ <div className="px-2.5 py-1 rounded-lg text-xs font-semibold text-teal-300 bg-teal-500/15 border border-teal-500/30">
1090
+ COMPLETED
1091
+ </div>
1092
+ ) : (
1093
+ <button
1094
+ onClick={() => handleTogglePause(camp.id)}
1095
+ className="px-2.5 py-1 rounded-lg text-xs font-semibold transition-all flex items-center gap-1.5 cursor-pointer bg-white/[0.04] text-neutral-300 hover:bg-white/[0.08] border border-white/[0.08]"
1096
+ title="Pause"
1097
+ >
1098
+ <Pause size={11} />
1099
+ <span>PAUSE</span>
1100
  </button>
1101
  )}
1102
  <button
1103
  onClick={() => handleEdit(camp)}
1104
+ className="p-1 text-neutral-500 hover:text-white rounded-lg transition-colors cursor-pointer"
1105
  title="Edit"
1106
  >
1107
+ <Edit3 size={13} />
1108
  </button>
1109
  <button
1110
  onClick={() => handleDelete(camp.id)}
1111
+ className="p-1 text-neutral-500 hover:text-rose-400 rounded-lg transition-colors cursor-pointer"
1112
  title="Delete"
1113
  >
1114
+ <Trash2 size={13} />
1115
  </button>
1116
  </div>
1117
  </div>
 
1222
  <div className="flex items-center gap-2.5 min-w-0">
1223
  {selectedAcc ? (
1224
  <>
1225
+ <TelegramAvatar
1226
+ photo={selectedAcc.profile_photo}
1227
+ name={selectedAcc.first_name}
1228
+ phone={selectedAcc.phone_number}
1229
+ size="w-6 h-6 text-[10px]"
1230
+ />
 
 
1231
  <div className="text-left truncate">
1232
  <span className="font-bold text-foreground block truncate leading-tight">
1233
  {selectedAcc.first_name ? `${selectedAcc.first_name} ${selectedAcc.last_name || ''}` : selectedAcc.phone_number}
 
1265
  }`}
1266
  >
1267
  <div className="flex items-center gap-2.5 min-w-0">
1268
+ <TelegramAvatar
1269
+ photo={acc.profile_photo}
1270
+ name={acc.first_name}
1271
+ phone={acc.phone_number}
1272
+ size="w-7 h-7 text-xs"
1273
+ />
 
 
1274
  <div className="text-left min-w-0">
1275
  <div className="font-bold text-xs truncate text-foreground flex items-center gap-1.5">
1276
  <span>{acc.first_name ? `${acc.first_name} ${acc.last_name || ''}` : acc.phone_number}</span>