File size: 10,861 Bytes
22b1735
 
1904e4c
22b1735
 
 
 
 
1904e4c
 
 
 
 
 
 
 
 
 
 
22b1735
 
 
 
 
 
 
 
 
1904e4c
 
22b1735
 
 
 
 
 
1904e4c
 
 
22b1735
 
1904e4c
 
 
 
 
 
 
 
 
 
 
22b1735
1904e4c
 
 
 
 
 
 
 
22b1735
1904e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22b1735
 
1904e4c
 
 
 
 
 
 
 
 
 
22b1735
1904e4c
 
22b1735
1904e4c
 
22b1735
1904e4c
 
 
 
22b1735
1904e4c
22b1735
1904e4c
 
 
 
 
22b1735
1904e4c
 
 
 
 
 
22b1735
 
1904e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22b1735
 
 
1904e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22b1735
 
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

import { useState } from "react";
import { ArrowRight, RefreshCcw, Copy, Check, Trash2, RotateCcw, ListFilter, ChevronLeft, ChevronRight } from "lucide-react";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { format } from "date-fns";
import { Avatar, AvatarFallback } from "../ui/avatar";
import { ChatMessage } from "./ChatMessage";
import { ThinkingAnimation } from "./ThinkingAnimation";
import { toast } from '../ui/sonner';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";

interface MessageVariation {
  id: string;
  content: string;
  timestamp: Date;
}

interface Message {
  id: string;
  content: string;
  sender: "user" | "system";
  timestamp: Date;
  isLoading?: boolean;
  error?: boolean;
  result?: any;
  variations?: MessageVariation[];
  activeVariation?: string;
}

interface ChatBubbleProps {
  message: Message;
  onViewSearchResults?: (messageId: string) => void;
  onRetry?: (messageId: string) => void;
  onRegenerate?: (messageId: string) => void;
  onDelete?: (messageId: string) => void;
  onSelectVariation?: (messageId: string, variationId: string) => void;
}

export const ChatBubble = ({ 
  message, 
  onViewSearchResults, 
  onRetry, 
  onRegenerate,
  onDelete,
  onSelectVariation
}: ChatBubbleProps) => {
  const [copied, setCopied] = useState(false);
  const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
  const isWelcomeMessage = message.id === "welcomems"
  const isSystem = message.sender === "system";
  const showCopyButton = true;
  const hasVariations = isSystem && message.variations && message.variations.length > 0;
  
  // If the message has variations, display the active one or the first one
  const displayContent = isSystem && hasVariations && message.activeVariation
    ? message.variations.find(v => v.id === message.activeVariation)?.content || message.content
    : message.content;
  
  const copyToClipboard = () => {
    navigator.clipboard.writeText(displayContent);
    setCopied(true);
    toast.success('Copied to clipboard!');
    setTimeout(() => setCopied(false), 2000);
  };

  const handleDelete = () => {
    setDeleteDialogOpen(false);
    if (onDelete) {
      onDelete(message.id);
    }
  };

  const handleSelectVariation = (variationId: string) => {
    if (onSelectVariation) {
      onSelectVariation(message.id, variationId);
    }
  };

  // Function to get the current variation index and navigate through variations
  const navigateVariations = (direction: 'prev' | 'next') => {
    if (!hasVariations || !message.variations || message.variations.length <= 1) return;
    
    const currentIndex = message.activeVariation 
      ? message.variations.findIndex(v => v.id === message.activeVariation)
      : 0;
      
    let newIndex;
    if (direction === 'prev') {
      newIndex = (currentIndex - 1 + message.variations.length) % message.variations.length;
    } else {
      newIndex = (currentIndex + 1) % message.variations.length;
    }
    
    handleSelectVariation(message.variations[newIndex].id);
  };

  // Get current variation index for display
  const getCurrentVariationIndex = () => {
    if (!hasVariations || !message.variations) return 0;
    return message.activeVariation 
      ? message.variations.findIndex(v => v.id === message.activeVariation) + 1
      : 1;
  };

  return (
    <>
      <div
        className={cn(
          "group flex w-full animate-slide-in mb-4",
          isSystem ? "justify-start" : "justify-end"
        )}
      >
        <div className={cn(
          "flex gap-3 max-w-[80%]",
          isSystem ? "flex-row" : "flex-row-reverse"
        )}>
          <Avatar className={cn(
            "h-8 w-8 border",
            isSystem
              ? "bg-financial-accent dark:text-white shadow-lg"
              : "bg-muted"
          )}>
            <AvatarFallback className="text-xs font-semibold">
              {isSystem ? "AI" : "You"}
            </AvatarFallback>
          </Avatar>

          <div className="flex flex-col">
            <div className={cn(
              "rounded-2xl shadow-lg message-bubble backdrop-blur-sm",
              isSystem
                ? "bg-white/90 dark:bg-card/90 border border-border text-foreground message-bubble-ai"
                : "bg-financial-accent/30 border border-financial-accent/30 dark:text-white message-bubble-user",
              message.error && "border-destructive dark:border-red-500"
            )}>
              {message.isLoading ? (
                <ThinkingAnimation />
              ) : (
                <ChatMessage
                  content={displayContent}
                />
              )}
            </div>
            
            {/* Chat bubble footer */}
            <div className="flex flex-row chat-bubble-footer justify-between">
              {/* Time */}
              <div className={cn(
                "text-xs text-muted-foreground mt-1",
                isSystem ? "text-left" : "text-right"
              )}>
                {format(message.timestamp, "h:mm a")}
              </div>
              {/* Controls */}
              {!isWelcomeMessage && <div className="controls flex gap-1">
                {/* Variation Navigation */}
                {hasVariations && message.variations && message.variations.length > 1 && (
                  <div className="flex items-center mt-1 opacity-0 group-hover:opacity-100 transition-opacity bg-background/50 rounded-md border">
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => navigateVariations('prev')}
                      disabled={message.variations.length <= 1}
                      className="h-6 px-1"
                    >
                      <ChevronLeft className="h-3 w-3" />
                    </Button>
                    <span className="text-xs px-1">
                      {getCurrentVariationIndex()}/{message.variations.length}
                    </span>
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => navigateVariations('next')}
                      disabled={message.variations.length <= 1}
                      className="h-6 px-1"
                    >
                      <ChevronRight className="h-3 w-3" />
                    </Button>
                  </div>
                )}
              
                {/* Retry button for failed messages */}
                {message.error && onRetry && (
                  <div className="flex mt-1">
                    <Button
                      variant="secondary"
                      size="sm"
                      onClick={() => onRetry(message.id)}
                      className="text-xs flex items-center gap-1.5 text-muted-foreground hover:border border-financial-accent/30 bg-background/50 backdrop-blur-sm"
                    >
                      <RefreshCcw className="h-3 w-3" />
                      Retry
                    </Button>
                  </div>
                )}
                
                {/* Regenerate button for system messages */}
                {isSystem && !message.isLoading && onRegenerate && (
                  <TooltipProvider>
                    <Tooltip>
                      <TooltipTrigger asChild>
                        <div className="flex mt-1 opacity-0 group-hover:opacity-100 transition-opacity">
                          <Button
                            variant="link"
                            size="sm"
                            onClick={() => onRegenerate(message.id)}
                          >
                            <RotateCcw className="h-3 w-3" />
                          </Button>
                        </div>
                      </TooltipTrigger>
                      <TooltipContent>
                        <p>Generate variation</p>
                      </TooltipContent>
                    </Tooltip>
                  </TooltipProvider>
                )}
                
                {/* Delete button */}
                {onDelete && !message.isLoading && (
                  <TooltipProvider>
                    <Tooltip>
                      <TooltipTrigger asChild>
                        <div className="flex mt-1 opacity-0 group-hover:opacity-100">
                          <Button
                            variant="ghost"
                            size="sm"
                            onClick={() => setDeleteDialogOpen(true)}
                            className="text-xs flex items-center gap-1.5 text-muted-foreground hover:text-destructive hover:bg-background/50 backdrop-blur-sm"
                          >
                            <Trash2 className="h-3 w-3" />
                          </Button>
                        </div>
                      </TooltipTrigger>
                      <TooltipContent>
                        <p>Delete message</p>
                      </TooltipContent>
                    </Tooltip>
                  </TooltipProvider>
                )}
                
                {/* Copy */}
                {showCopyButton && !message.isLoading && (
                  <div className="relative top-1 right-1 opacity-0 group-hover:opacity-100 transition-opacity">
                    <Button variant="link" size="sm" onClick={copyToClipboard}>
                      {copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
                    </Button>
                  </div>
                )}
              </div>}
            </div>
          </div>
        </div>
      </div>
      
      {/* Delete confirmation dialog */}
      <AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Delete Message</AlertDialogTitle>
            <AlertDialogDescription>
              Are you sure you want to delete this message? This action cannot be undone.
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>Cancel</AlertDialogCancel>
            <AlertDialogAction onClick={handleDelete} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
              Delete
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </>
  );
};