# All files / src/components/chat TicketChat.tsx

0% Statements 0/61
0% Branches 0/44
0% Functions 0/13
0% Lines 0/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323

     'use client';

    import React, { useState, useRef, useEffect } from 'react';
    import { Button } from '@/components/ui/button';
    import { Textarea } from '@/components/ui/textarea';
    import { Badge } from '@/components/ui/badge';
    import { Avatar, AvatarFallback } from '@/components/ui/avatar';
    import { Card, CardContent } from '@/components/ui/card';
    import {
      Send,
      User,
      Shield,
      CheckCheck,
      Paperclip,
      Smile,
      MoreVertical,
      Reply
    } from 'lucide-react';
    import { cn } from '@/lib/utils';
    import { useTranslation } from 'react-i18next';

    interface Message {
      id: number;
      message: string;
      username: string;
      user_role: string;
      created_at: string;
      is_internal: boolean;
      is_read: boolean;
    }

    interface Ticket {
      id: number;
      title: string;
      description: string;
      status: string;
      priority: string;
      creator_username: string;
      created_at: string;
      messages: Message[];
    }

    interface TicketChatProps {
      ticket: Ticket;
      newMessage: string;
      setNewMessage: (message: string) => void;
      onSendMessage: () => void;
      sendingMessage: boolean;
      currentUserRole?: string;
    }

    export default function TicketChat({
      ticket,
      newMessage,
      setNewMessage,
      onSendMessage,
      sendingMessage,
      currentUserRole = 'reseller'
    }: TicketChatProps) {
      const { t } = useTranslation();
      const messagesEndRef = useRef<HTMLDivElement>(null);
      const textareaRef = useRef<HTMLTextAreaElement>(null);
      const [isTyping, setIsTyping] = useState(false);

      // Auto-scroll to bottom when new messages arrive
      useEffect(() => {
        messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
      }, [ticket.messages]);

      // Auto-resize textarea
      useEffect(() => {
        if (textareaRef.current) {
          textareaRef.current.style.height = 'auto';
          textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
        }
      }, [newMessage]);

      const handleKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === 'Enter' && !e.shiftKey) {
          e.preventDefault();
          if (newMessage.trim()) {
            onSendMessage();
          }
        }
      };

      const getMessageAlignment = (message: Message) => {
        const isCurrentUser = message.user_role === currentUserRole;
        return isCurrentUser ? 'flex-row-reverse' : 'flex-row';
      };

      const getMessageBubbleStyle = (message: Message) => {
        const isCurrentUser = message.user_role === currentUserRole;
        const isAdmin = message.user_role === 'admin';

        if (isCurrentUser) {
          return 'bg-primary text-primary-foreground ml-12';
        } else if (isAdmin) {
          return 'bg-green-500/10 text-green-600 border border-green-500/20 mr-12';
        } else {
          return 'bg-muted text-muted-foreground border border-muted mr-12';
        }
      };

      const getUserInitials = (username: string) => {
        return username.split(' ').map(n => n[0]).join('').toUpperCase().slice(0, 2);
      };

      const formatTime = (dateString: string) => {
        const date = new Date(dateString);
        const now = new Date();
        const diffInHours = (now.getTime() - date.getTime()) / (1000 * 60 * 60);

        if (diffInHours < 24) {
          return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
        } else if (diffInHours < 168) { // 7 days
          return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit', minute: '2-digit' });
        } else {
          return date.toLocaleDateString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
        }
      };

      const getRoleIcon = (role: string) => {
        switch (role) {
          case 'admin':
            return <Shield className="h-3 w-3 text-green-600" />;
          case 'reseller':
            return <User className="h-3 w-3 text-primary" />;
          default:
            return <User className="h-3 w-3 text-muted-foreground" />;
        }
      };

      const getRoleBadgeColor = (role: string) => {
        switch (role) {
          case 'admin':
            return 'bg-green-500/10 text-green-600 border-green-500/20';
          case 'reseller':
            return 'bg-primary/10 text-primary border-primary/20';
          default:
            return 'bg-muted text-muted-foreground border-muted';
        }
      };

      return (
        <div className="flex flex-col h-full">
          {/* Chat Header */}
          <div className="flex items-center justify-between p-4 border-b border-border bg-muted/30">
            <div className="flex items-center gap-3">
              <Avatar className="h-8 w-8">
                <AvatarFallback className="bg-primary/10 text-primary text-xs">
                  {getUserInitials(ticket.creator_username)}
                </AvatarFallback>
              </Avatar>
              <div>
                <h3 className="font-semibold text-sm text-foreground">{ticket.title}</h3>
                <p className="text-xs text-muted-foreground">
                  {t('chat.ticket.createdBy', { username: ticket.creator_username })} • {formatTime(ticket.created_at)}
                </p>
              </div>
            </div>
            <div className="flex items-center gap-2">
              <Badge variant="outline" className="text-xs">
                {ticket.status.replace('_', ' ')}
              </Badge>
              <Badge variant="outline" className="text-xs">
                {ticket.priority}
              </Badge>
            </div>
          </div>

          {/* Messages Container */}
          <div className="flex-1 overflow-y-auto p-4 space-y-4 bg-background/50">
            {/* Initial Ticket Description */}
            <Card className="border-l-4 border-l-primary bg-primary/5">
              <CardContent className="p-4">
                <div className="flex items-center gap-2 mb-2">
                  <Avatar className="h-6 w-6">
                    <AvatarFallback className="bg-primary/10 text-primary text-xs">
                      {getUserInitials(ticket.creator_username)}
                    </AvatarFallback>
                  </Avatar>
                  <span className="font-medium text-primary text-sm">{ticket.creator_username}</span>
                  <Badge className="bg-primary/10 text-primary text-xs">{t('chat.ticket.initialRequest')}</Badge>
                  <span className="text-xs text-primary ml-auto">
                    {formatTime(ticket.created_at)}
                  </span>
                </div>
                <p className="text-primary text-sm leading-relaxed">{ticket.description}</p>
              </CardContent>
            </Card>

            {/* Chat Messages */}
            {ticket.messages.map((message) => {
              // Don't show internal messages to non-admin users
              if (message.is_internal && currentUserRole !== 'admin') return null;

              const isCurrentUser = message.user_role === currentUserRole;
              const isAdmin = message.user_role === 'admin';

              return (
                <div
                  key={message.id}
                  className={cn('flex gap-3 group', getMessageAlignment(message))}
                >
                  {!isCurrentUser && (
                    <Avatar className="h-8 w-8 mt-1">
                      <AvatarFallback className={cn(
                        'text-xs',
                        isAdmin ? 'bg-green-500/10 text-green-600' : 'bg-muted text-muted-foreground'
                      )}>
                        {getUserInitials(message.username)}
                      </AvatarFallback>
                    </Avatar>
                  )}

                  <div className={cn('flex flex-col', isCurrentUser ? 'items-end' : 'items-start')}>
                    {!isCurrentUser && (
                      <div className="flex items-center gap-2 mb-1">
                        <span className="text-xs font-medium text-foreground">{message.username}</span>
                        {getRoleIcon(message.user_role)}
                        <Badge variant="outline" className={cn('text-xs', getRoleBadgeColor(message.user_role))}>
                          {message.user_role === 'admin' ? t('chat.ticket.supportRole') : t('chat.ticket.customerRole')}
                        </Badge>
                        {message.is_internal && (
                          <Badge variant="outline" className="text-xs bg-orange-500/10 text-orange-600">
                            {t('chat.ticket.internal')}
                          </Badge>
                        )}
                      </div>
                    )}

                    <div className={cn(
                      'rounded-2xl px-4 py-2 max-w-[80%] shadow-sm',
                      getMessageBubbleStyle(message)
                    )}>
                      <p className="text-sm leading-relaxed whitespace-pre-wrap">{message.message}</p>
                    </div>

                    <div className={cn(
                      'flex items-center gap-1 mt-1 opacity-0 group-hover:opacity-100 transition-opacity',
                      isCurrentUser ? 'flex-row-reverse' : 'flex-row'
                    )}>
                      <span className="text-xs text-muted-foreground">{formatTime(message.created_at)}</span>
                      {isCurrentUser && message.is_read && (
                        <CheckCheck className="h-3 w-3 text-primary" />
                      )}
                      <Button variant="ghost" size="sm" className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100">
                        <MoreVertical className="h-3 w-3" />
                      </Button>
                    </div>
                  </div>

                  {isCurrentUser && (
                    <Avatar className="h-8 w-8 mt-1">
                      <AvatarFallback className="bg-primary/10 text-primary text-xs">
                        {getUserInitials(message.username)}
                      </AvatarFallback>
                    </Avatar>
                  )}
                </div>
              );
            })}

            {/* Typing Indicator */}
            {isTyping && (
              <div className="flex items-center gap-2 text-muted-foreground">
                <div className="flex gap-1">
                  <div className="w-2 h-2 bg-muted-foreground rounded-full animate-bounce"></div>
                  <div className="w-2 h-2 bg-muted-foreground rounded-full animate-bounce" style={{ animationDelay: '0.1s' }}></div>
                  <div className="w-2 h-2 bg-muted-foreground rounded-full animate-bounce" style={{ animationDelay: '0.2s' }}></div>
                </div>
                <span className="text-xs">{t('chat.ticket.supportTyping')}</span>
              </div>
            )}

            <div ref={messagesEndRef} />
          </div>

          {/* Message Input */}
          <div className="border-t border-border bg-card p-4">
            <div className="flex items-end gap-3">
              <div className="flex-1 relative">
                <Textarea
                  ref={textareaRef}
                  placeholder={t('chat.ticket.messagePlaceholder')}
                  value={newMessage}
                  onChange={(e) => setNewMessage(e.target.value)}
                  onKeyDown={handleKeyDown}
                  className="min-h-[44px] max-h-32 resize-none pr-12 rounded-2xl border-border focus:border-primary focus:ring-primary/20"
                  rows={1}
                />
                <div className="absolute right-2 bottom-2 flex items-center gap-1">
                  <Button variant="ghost" size="sm" className="h-8 w-8 p-0">
                    <Paperclip className="h-4 w-4 text-muted-foreground" />
                  </Button>
                  <Button variant="ghost" size="sm" className="h-8 w-8 p-0">
                    <Smile className="h-4 w-4 text-muted-foreground" />
                  </Button>
                </div>
              </div>
              <Button
                onClick={onSendMessage}
                disabled={!newMessage.trim() || sendingMessage}
                className="h-11 w-11 rounded-full bg-primary hover:bg-primary/90 disabled:opacity-50"
                size="sm"
              >
                {sendingMessage ? (
                  <div className="animate-spin rounded-full h-4 w-4 border-2 border-primary-foreground border-t-transparent"></div>
                ) : (
                  <Send className="h-4 w-4" />
                )}
              </Button>
            </div>
            <div className="flex items-center justify-between mt-2 text-xs text-muted-foreground">
              <span>{t('chat.ticket.sendHint')}</span>
              <span>{newMessage.length}/1000</span>
            </div>
          </div>
        </div>
      );
    }