File size: 9,038 Bytes
3b2aa49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * DuplicateWarningBanner — Animated warning card shown when a potential
 * duplicate ticket is detected during ticket creation.
 *
 * Features:
 *   - Animated slide-in warning with similarity score
 *   - Link to parent ticket
 *   - "Subscribe to existing" vs "Create anyway" actions
 *   - Matches the HELPDESK.AI design system (glass morphism, gradient)
 */

import React, { useState, useEffect } from 'react';
import {
  AlertTriangle,
  Copy,
  Eye,
  Bell,
  X,
  ChevronDown,
  ChevronUp,
  ArrowRight,
  Send,
} from 'lucide-react';

/**
 * Format similarity as percentage with color.
 */
function formatSimilarity(score) {
  if (score == null) return '—';
  const pct = Math.round(score * 100);
  if (pct >= 95) return { value: `${pct}%`, color: '#DC2626' };
  if (pct >= 85) return { value: `${pct}%`, color: '#EA580C' };
  if (pct >= 75) return { value: `${pct}%`, color: '#CA8A04' };
  return { value: `${pct}%`, color: '#16A34A' };
}

/**
 * DuplicateWarningBanner
 *
 * Props:
 *   - duplicate: { is_duplicate, duplicate_ticket_id, parent_subject, similarity, candidates }
 *   - onSubscribe: () => void — User wants to subscribe to existing ticket
 *   - onCreateAnyway: () => void — User wants to create ticket regardless
 *   - onDismiss: () => void — Dismiss warning
 */
export default function DuplicateWarningBanner({
  duplicate,
  onSubscribe,
  onCreateAnyway,
  onDismiss,
}) {
  const [expanded, setExpanded] = useState(false);
  const [visible, setVisible] = useState(false);

  // Animate in on mount
  useEffect(() => {
    const timer = setTimeout(() => setVisible(true), 100);
    return () => clearTimeout(timer);
  }, []);

  if (!duplicate || !duplicate.is_duplicate) return null;

  const { similarity, parent_subject, duplicate_ticket_id, candidates } = duplicate;
  const sim = formatSimilarity(similarity);
  const extraCandidates = (candidates || []).filter(
    c => c.id !== duplicate_ticket_id
  );

  return (
    <div
      className={`transition-all duration-500 ease-out ${
        visible ? 'opacity-100 translate-y-0' : 'opacity-0 translate-y-4'
      }`}
    >
      <div
        className="rounded-2xl border-2 overflow-hidden"
        style={{
          borderColor: '#FECACA',
          background: 'linear-gradient(135deg, #FFF5F5 0%, #FFFBEB 100%)',
          boxShadow: '0 8px 32px rgba(239, 68, 68, 0.1)',
        }}
      >
        {/* Warning Header */}
        <div className="p-5 flex items-start gap-4">
          {/* Icon */}
          <div
            className="w-12 h-12 rounded-xl flex items-center justify-center flex-shrink-0 animate-pulse"
            style={{ background: '#FEF2F2', border: '1px solid #FECACA' }}
          >
            <AlertTriangle size={22} className="text-red-500" />
          </div>

          {/* Content */}
          <div className="flex-1 min-w-0">
            <div className="flex items-start justify-between gap-2">
              <div>
                <h3 className="text-base font-bold text-gray-900 flex items-center gap-2">
                  <Copy size={16} className="text-amber-500" />
                  Potential Duplicate Detected
                </h3>
                <p className="text-sm text-gray-600 mt-1">
                  A similar issue was recently reported by your teammate.
                </p>
              </div>
              {onDismiss && (
                <button
                  onClick={onDismiss}
                  className="p-1.5 rounded-lg text-gray-400 hover:text-gray-600 hover:bg-gray-100 transition-colors flex-shrink-0"
                >
                  <X size={16} />
                </button>
              )}
            </div>

            {/* Match Info Card */}
            <div
              className="mt-4 rounded-xl p-4 border"
              style={{ background: '#FFFFFF', borderColor: '#FEE2E2' }}
            >
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-3 min-w-0 flex-1">
                  {/* Similarity Badge */}
                  <div
                    className="flex flex-col items-center px-3 py-2 rounded-lg"
                    style={{ background: similarity >= 0.85 ? '#FEF2F2' : '#FFFBEB' }}
                  >
                    <span
                      className="text-lg font-extrabold"
                      style={{ color: sim.color }}
                    >
                      {sim.value}
                    </span>
                    <span className="text-[9px] font-semibold text-gray-400 uppercase tracking-wider">
                      Match
                    </span>
                  </div>

                  {/* Parent Ticket Info */}
                  <div className="min-w-0 flex-1">
                    {parent_subject && (
                      <p className="text-sm font-semibold text-gray-800 truncate">
                        {parent_subject}
                      </p>
                    )}
                    <p className="text-xs text-gray-500 mt-0.5">
                      Ticket #{((duplicate_ticket_id || '') + '').slice(0, 8).toUpperCase()}
                    </p>
                  </div>
                </div>

                {/* View Parent Ticket */}
                <a
                  href={`/admin/ticket/${duplicate_ticket_id}`}
                  className="flex items-center gap-1.5 px-3 py-2 text-xs font-bold text-blue-600 bg-blue-50 border border-blue-200 rounded-lg hover:bg-blue-100 transition-colors flex-shrink-0"
                >
                  <Eye size={14} />
                  View
                </a>
              </div>

              {/* Extra candidates */}
              {extraCandidates.length > 0 && (
                <div className="mt-3 pt-3 border-t border-gray-100">
                  <button
                    onClick={() => setExpanded(!expanded)}
                    className="flex items-center gap-1.5 text-xs font-semibold text-gray-500 hover:text-gray-700 transition-colors"
                  >
                    {expanded ? <ChevronUp size={14} /> : <ChevronDown size={14} />}
                    {extraCandidates.length} more similar ticket{extraCandidates.length > 1 ? 's' : ''}
                  </button>

                  {expanded && (
                    <div className="mt-2 space-y-1.5">
                      {extraCandidates.map((c, idx) => (
                        <div
                          key={c.id || idx}
                          className="flex items-center justify-between p-2 rounded-lg bg-gray-50"
                        >
                          <div className="flex items-center gap-2 min-w-0">
                            <span
                              className="text-[10px] font-bold px-1.5 py-0.5 rounded"
                              style={{
                                background: c.similarity >= 0.85 ? '#FEF2F2' : '#FEFCE8',
                                color: c.similarity >= 0.85 ? '#DC2626' : '#CA8A04',
                              }}
                            >
                              {Math.round(c.similarity * 100)}%
                            </span>
                            <span className="text-xs text-gray-700 truncate">
                              {c.subject || `#${((c.id || '') + '').slice(0, 8)}`}
                            </span>
                          </div>
                          <span className="text-[10px] text-gray-400 flex-shrink-0">
                            {c.assigned_team || ''}
                          </span>
                        </div>
                      ))}
                    </div>
                  )}
                </div>
              )}
            </div>

            {/* Action Buttons */}
            <div className="flex flex-wrap items-center gap-3 mt-4">
              {onSubscribe && (
                <button
                  onClick={onSubscribe}
                  className="flex items-center gap-2 px-5 py-2.5 text-sm font-bold text-white rounded-xl transition-all hover:shadow-lg active:scale-[0.98]"
                  style={{ background: 'linear-gradient(135deg, #16A34A, #15803D)' }}
                >
                  <Bell size={16} />
                  Subscribe to Existing Ticket
                  <ArrowRight size={16} />
                </button>
              )}
              {onCreateAnyway && (
                <button
                  onClick={onCreateAnyway}
                  className="flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-gray-600 bg-white border border-gray-200 rounded-xl hover:bg-gray-50 hover:border-gray-300 transition-all"
                >
                  <Send size={14} />
                  Create Anyway
                </button>
              )}
              <p className="text-[10px] text-gray-400 ml-auto">
                AI-powered semantic match · all-MiniLM-L6-v2
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}