File size: 11,205 Bytes
68c2137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import React, { useState } from 'react';
import { 
  Scale, 
  User, 
  Copy, 
  Check, 
  ThumbsUp, 
  ThumbsDown, 
  RotateCw, 
  FileText, 
  Clock, 
  Cpu,
  Share2,
  Edit3,
  Trash2
} from 'lucide-react';
import type { Message } from '../types/chat';

interface MessageCardProps {
  message: Message;
  onRegenerate?: () => void;
  onOpenPDFPreview?: (pdf: NonNullable<Message['pdfPreview']>) => void;
  onEditMessage?: (id: string, text: string) => void;
  onDeleteMessage?: (id: string) => void;
}

export const MessageCard: React.FC<MessageCardProps> = ({
  message,
  onRegenerate,
  onOpenPDFPreview,
  onEditMessage,
  onDeleteMessage
}) => {
  const [copied, setCopied] = useState(false);
  const [liked, setLiked] = useState<boolean | null>(null);
  const [isEditing, setIsEditing] = useState(false);
  const [editText, setEditText] = useState(message.content);

  const isUser = message.role === 'user';

  const handleCopy = () => {
    navigator.clipboard.writeText(message.content);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  const handleSaveEdit = () => {
    if (onEditMessage && editText.trim()) {
      onEditMessage(message.id, editText.trim());
      setIsEditing(false);
    }
  };

  // Simple Markdown & Table Parser
  const renderFormattedContent = (content: string) => {
    const codeBlockRegex = /```(\w*)\n([\s\S]*?)```/g;
    const parts = [];
    let lastIndex = 0;
    let match;

    while ((match = codeBlockRegex.exec(content)) !== null) {
      if (match.index > lastIndex) {
        parts.push(content.substring(lastIndex, match.index));
      }
      const lang = match[1] || 'code';
      const code = match[2].trim();
      parts.push(
        <div key={match.index} className="my-4 rounded-2xl overflow-hidden border border-slate-700 bg-slate-950 text-slate-100 font-mono text-xs shadow-xl">
          <div className="flex items-center justify-between px-4 py-2 bg-slate-900 border-b border-slate-700 text-slate-400 font-semibold">
            <span>{lang}</span>
            <button
              onClick={() => {
                navigator.clipboard.writeText(code);
                setCopied(true);
                setTimeout(() => setCopied(false), 2000);
              }}
              className="flex items-center space-x-1.5 hover:text-white transition-colors"
            >
              {copied ? <Check className="w-3.5 h-3.5 text-emerald-400" /> : <Copy className="w-3.5 h-3.5" />}
              <span>Copy Code</span>
            </button>
          </div>
          <pre className="p-4 overflow-x-auto">
            <code>{code}</code>
          </pre>
        </div>
      );
      lastIndex = match.index + match[0].length;
    }

    if (lastIndex < content.length) {
      parts.push(content.substring(lastIndex));
    }

    return parts.map((p, idx) => {
      if (typeof p === 'string') {
        // Simple Markdown Table parsing
        if (p.includes('|')) {
          const lines = p.split('\n');
          const tableRows = lines.filter(l => l.trim().startsWith('|'));
          if (tableRows.length > 1) {
            return (
              <div key={idx} className="my-3 overflow-x-auto rounded-xl border border-slate-700 bg-slate-900/60 p-2">
                <table className="w-full text-xs text-left text-slate-200">
                  <tbody>
                    {tableRows.map((row, rIdx) => {
                      if (row.includes(':---') || row.includes('---')) return null;
                      const cols = row.split('|').filter(c => c.trim() !== '');
                      return (
                        <tr key={rIdx} className={rIdx === 0 ? 'bg-slate-800 font-bold text-white border-b border-slate-700' : 'border-b border-slate-800'}>
                          {cols.map((cell, cIdx) => (
                            <td key={cIdx} className="px-3 py-2">{cell.trim().replace(/\*\*/g, '')}</td>
                          ))}
                        </tr>
                      );
                    })}
                  </tbody>
                </table>
              </div>
            );
          }
        }
        return <p key={idx} className="whitespace-pre-wrap leading-relaxed">{p}</p>;
      }
      return p;
    });
  };

  return (
    <div className={`py-4 px-4 sm:px-6 w-full flex ${isUser ? 'justify-end' : 'justify-start'}`}>
      <div className={`max-w-[85%] sm:max-w-[80%] rounded-2xl p-5 shadow-lg transition-all ${
        isUser 
          ? 'glass-card-user rounded-tr-none' 
          : 'glass-card-ai rounded-tl-none w-full'
      }`}>
        {/* Header Bar */}
        <div className="flex items-center justify-between mb-3 border-b border-slate-700/50 pb-2">
          <div className="flex items-center space-x-2.5">
            <div className={`w-7 h-7 rounded-xl flex items-center justify-center text-white shadow-sm ${
              isUser 
                ? 'bg-white/20' 
                : 'bg-gradient-to-br from-blue-600 to-indigo-600'
            }`}>
              {isUser ? <User className="w-4 h-4" /> : <Scale className="w-4 h-4" />}
            </div>
            <span className="text-xs font-bold text-white">
              {isUser ? 'You' : 'LawSLM Assistant'}
            </span>
          </div>

          <div className="flex items-center space-x-2 text-[11px] text-slate-400">
            <Clock className="w-3 h-3" />
            <span>{message.timestamp}</span>
          </div>
        </div>

        {/* Content Body */}
        {isEditing ? (
          <div className="space-y-2">
            <textarea
              value={editText}
              onChange={e => setEditText(e.target.value)}
              className="w-full p-3 text-sm rounded-xl bg-slate-900 border border-blue-500 text-white outline-none"
              rows={3}
            />
            <div className="flex justify-end space-x-2">
              <button
                onClick={() => setIsEditing(false)}
                className="px-3 py-1 rounded-lg bg-slate-700 text-xs text-slate-200"
              >
                Cancel
              </button>
              <button
                onClick={handleSaveEdit}
                className="px-3 py-1 rounded-lg bg-blue-600 text-xs text-white font-semibold"
              >
                Save & Resend
              </button>
            </div>
          </div>
        ) : (
          <div className="text-sm text-slate-100 leading-relaxed font-normal">
            {renderFormattedContent(message.content)}
            {message.isStreaming && (
              <span className="streaming-cursor" />
            )}
          </div>
        )}

        {/* PDF Document Card if generated */}
        {message.pdfPreview && (
          <div className="mt-4 p-3.5 rounded-xl bg-blue-500/10 border border-blue-500/30 flex items-center justify-between">
            <div className="flex items-center space-x-3">
              <FileText className="w-6 h-6 text-blue-400" />
              <div>
                <h4 className="text-xs font-bold text-blue-300">{message.pdfPreview.title}</h4>
                <p className="text-[11px] text-slate-400">Formal Legal & Analytical PDF Report</p>
              </div>
            </div>
            <button
              onClick={() => onOpenPDFPreview && onOpenPDFPreview(message.pdfPreview!)}
              className="px-3.5 py-2 rounded-xl bg-blue-600 hover:bg-blue-700 text-white text-xs font-semibold shadow-md transition-all"
            >
              Preview & Export PDF
            </button>
          </div>
        )}

        {/* Bottom Toolbar */}
        <div className="mt-3 pt-2.5 border-t border-slate-700/40 flex items-center justify-between text-xs text-slate-400">
          <div className="flex items-center space-x-2">
            <button
              onClick={handleCopy}
              className="flex items-center space-x-1 px-2.5 py-1 rounded-lg bg-slate-800/80 hover:bg-slate-700 border border-slate-700 text-slate-300 transition-colors"
              title="Copy text"
            >
              {copied ? <Check className="w-3.5 h-3.5 text-emerald-400" /> : <Copy className="w-3.5 h-3.5" />}
              <span>{copied ? 'Copied' : 'Copy'}</span>
            </button>

            {!isUser && (
              <>
                <button
                  onClick={() => setLiked(liked === true ? null : true)}
                  className={`p-1.5 rounded-lg bg-slate-800/80 hover:bg-slate-700 border border-slate-700 transition-colors ${liked === true ? 'text-emerald-400 border-emerald-500/50' : 'text-slate-400'}`}
                  title="Good response"
                >
                  <ThumbsUp className="w-3.5 h-3.5" />
                </button>

                <button
                  onClick={() => setLiked(liked === false ? null : false)}
                  className={`p-1.5 rounded-lg bg-slate-800/80 hover:bg-slate-700 border border-slate-700 transition-colors ${liked === false ? 'text-rose-400 border-rose-500/50' : 'text-slate-400'}`}
                  title="Bad response"
                >
                  <ThumbsDown className="w-3.5 h-3.5" />
                </button>

                {onRegenerate && (
                  <button
                    onClick={onRegenerate}
                    className="flex items-center space-x-1 px-2.5 py-1 rounded-lg bg-slate-800/80 hover:bg-slate-700 border border-slate-700 text-slate-300 transition-colors"
                    title="Regenerate answer"
                  >
                    <RotateCw className="w-3.5 h-3.5" />
                    <span>Retry</span>
                  </button>
                )}

                <button
                  onClick={handleCopy}
                  className="p-1.5 rounded-lg bg-slate-800/80 hover:bg-slate-700 border border-slate-700 text-slate-400 transition-colors"
                  title="Share response"
                >
                  <Share2 className="w-3.5 h-3.5" />
                </button>
              </>
            )}

            {isUser && (
              <>
                <button
                  onClick={() => setIsEditing(true)}
                  className="p-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-white transition-colors"
                  title="Edit prompt"
                >
                  <Edit3 className="w-3.5 h-3.5" />
                </button>
                {onDeleteMessage && (
                  <button
                    onClick={() => onDeleteMessage(message.id)}
                    className="p-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-rose-300 transition-colors"
                    title="Delete message"
                  >
                    <Trash2 className="w-3.5 h-3.5" />
                  </button>
                )}
              </>
            )}
          </div>

          {!isUser && message.responseTimeMs && (
            <div className="flex items-center space-x-1.5 text-[10px] font-semibold text-slate-400">
              <Cpu className="w-3.5 h-3.5 text-blue-400" />
              <span>{(message.responseTimeMs / 1000).toFixed(2)}s</span>
            </div>
          )}
        </div>
      </div>
    </div>
  );
};