File size: 10,166 Bytes
cc276cc | 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 | "use client";
import { useState, useRef, useEffect } from 'react';
import { Button } from './ui/button';
import { Paperclip, Send, Smile, Mic, X } from 'lucide-react';
import { CSSTransition } from 'react-transition-group';
import { VoiceRecorder } from './voice-recorder';
import type { ReplyTo, Message, Group } from '@/lib/types';
import { useSettings } from '@/contexts/settings-context';
import { useAuth } from '@/contexts/auth-context';
import { useChatUtils } from '@/contexts/chat-utils-context';
import { useAppContext } from '@/contexts/app-context';
import { Skeleton } from './ui/skeleton';
// Helper function to extract plain text and emoji data from the contentEditable div
const getMessageFromDiv = (div: HTMLDivElement): string => {
let message = '';
div.childNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
message += node.textContent;
} else if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === 'IMG') {
const imgElement = node as HTMLImageElement;
// Use a specific character or sequence to represent the emoji,
// which can be parsed on the receiving end.
// Here, we use the 'alt' attribute which should contain the native emoji character.
message += imgElement.alt;
}
});
return message;
};
interface MessageInputProps {
chatId: string | null;
disabled?: boolean;
lastMessage: Message | null;
isGroupChat?: boolean;
currentGroup?: Group;
onGetSuggestedReplies: (message: Message) => Promise<string[]>;
sendTextMessage: (text: string, replyTo?: ReplyTo | null) => void;
onSelectMedia: (file: File) => void;
onSendAudio: (data: { file: File, duration: number }) => void;
onToggleEmojiPicker: () => void;
editorRef: React.RefObject<HTMLDivElement>;
}
const QuotedMessagePreview = ({ replyTo, onCancel }: { replyTo: ReplyTo | null, onCancel: () => void }) => {
const nodeRef = useRef(null);
return (
<CSSTransition nodeRef={nodeRef} in={!!replyTo} timeout={200} classNames="reply-bar" unmountOnExit>
<div ref={nodeRef} className="bg-muted/70 px-4 pt-2 pb-1 border-b">
<div className="bg-background/50 rounded-lg p-2 flex justify-between items-center border-l-4 border-primary">
{replyTo && (
<>
<div>
<p className="font-bold text-primary text-sm">{replyTo.displayName}</p>
<p className="text-sm text-muted-foreground truncate quoted-message-content">
{replyTo.text || (replyTo.imageKey ? 'Image' : replyTo.videoKey ? 'Video' : 'Voice Message')}
</p>
</div>
<Button variant="ghost" size="icon" className="h-7 w-7" onClick={onCancel}>
<X className="h-4 w-4" />
</Button>
</>
)}
</div>
</div>
</CSSTransition>
);
};
export function MessageInput({
sendTextMessage,
onSelectMedia,
onSendAudio,
chatId,
disabled,
lastMessage,
onGetSuggestedReplies,
isGroupChat,
currentGroup,
onToggleEmojiPicker,
editorRef,
}: MessageInputProps) {
const { currentUser } = useAuth();
const { setUserTyping } = useChatUtils();
const { replyTo, setReplyTo } = useAppContext();
const { playSound, t } = useSettings();
const [isRecording, setIsRecording] = useState(false);
const [suggestedReplies, setSuggestedReplies] = useState<string[]>([]);
const [showSuggestions, setShowSuggestions] = useState(false);
const typingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const handleSend = () => {
if (!editorRef.current) return;
const message = getMessageFromDiv(editorRef.current);
if (message.trim()) {
playSound('send');
sendTextMessage(message, replyTo);
editorRef.current.innerHTML = '';
setReplyTo(null);
}
};
useEffect(() => {
const fetchSuggestions = async () => {
if (lastMessage && lastMessage.sender !== currentUser?.uid && lastMessage.text) {
setShowSuggestions(true);
setSuggestedReplies([]); // Clear old suggestions immediately
const replies = await onGetSuggestedReplies(lastMessage);
setSuggestedReplies(replies);
} else {
setShowSuggestions(false);
setSuggestedReplies([]);
}
};
// Only fetch suggestions if the last message changes
if (lastMessage?.id) {
fetchSuggestions();
}
}, [lastMessage?.id, lastMessage?.sender, lastMessage?.text, currentUser?.uid, onGetSuggestedReplies]);
const handleInputChange = (e: React.FormEvent<HTMLDivElement>) => {
if (chatId) {
if (typingTimeoutRef.current) clearTimeout(typingTimeoutRef.current);
else setUserTyping(chatId, true);
typingTimeoutRef.current = setTimeout(() => {
setUserTyping(chatId, false);
typingTimeoutRef.current = null;
}, 2000);
}
};
const handleSendSuggestion = (reply: string) => {
sendTextMessage(reply, null);
setShowSuggestions(false);
}
const handleKeyPress = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
handleSend();
}
};
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
onSelectMedia(file);
}
// Reset the input value to allow selecting the same file again
e.target.value = '';
};
if (disabled) {
return (
<div className="p-4 border-t text-center text-sm text-muted-foreground">
{t('messagingDisabled')}
</div>
);
}
const sendingMode = currentGroup?.info?.settings?.sendingMode || 'everyone';
const isMuted = isGroupChat && currentGroup?.info.mutedMembers?.[currentUser?.uid || ''];
const canSend = !isGroupChat || (
(sendingMode === 'everyone' && !isMuted) ||
(sendingMode === 'admins' && currentGroup?.admins[currentUser?.uid || '']) ||
(sendingMode === 'owner' && currentGroup?.info.createdBy === currentUser?.uid)
);
if (!canSend) {
return (
<div className="p-4 border-t text-center text-sm text-muted-foreground">
<p>{isMuted ? t('youAreMuted') : t('adminsCanSend')}</p>
</div>
);
}
if (isRecording) {
return <VoiceRecorder onCancel={() => setIsRecording(false)} onSend={onSendAudio} />;
}
return (
<div className="flex flex-col border-t bg-background/80 backdrop-blur-sm">
<QuotedMessagePreview replyTo={replyTo} onCancel={() => setReplyTo(null)} />
{showSuggestions && (
<div className="p-2 border-b">
<div className="flex gap-2 overflow-x-auto pb-2">
{suggestedReplies.length > 0 ? (
suggestedReplies.map((reply, i) => (
<Button key={i} variant="outline" size="sm" className="flex-shrink-0" onClick={() => handleSendSuggestion(reply)}>
{reply}
</Button>
))
) : (
Array.from({ length: 3 }).map((_, i) => <Skeleton key={i} className="h-9 w-24 rounded-md" />)
)}
<Button variant="ghost" size="icon" className="h-9 w-9 flex-shrink-0" onClick={() => setShowSuggestions(false)}><X className="h-4 w-4"/></Button>
</div>
</div>
)}
<div className="flex items-start gap-2 p-2 md:p-4">
<input type="file" id="file-upload" className="hidden" onChange={handleFileSelect} accept="image/*,video/*" />
<Button asChild variant="ghost" size="icon" title="Attach file">
<label htmlFor="file-upload" className="cursor-pointer">
<Paperclip />
<span className="sr-only">Attach file</span>
</label>
</Button>
<Button variant="ghost" size="icon" title="Add emoji" onClick={onToggleEmojiPicker}>
<Smile />
<span className="sr-only">Add emoji</span>
</Button>
<div className="rich-input-container flex-1">
<div
ref={editorRef}
contentEditable="true"
onInput={handleInputChange}
onKeyDown={handleKeyPress}
data-placeholder={t('typeMessage')}
className="rich-input-editor"
/>
</div>
{editorRef.current && editorRef.current.textContent?.trim() ? (
<Button onClick={handleSend} size="icon" className="rounded-full h-10 w-10 flex-shrink-0" title="Send message">
<Send />
<span className="sr-only">Send</span>
</Button>
) : (
<Button onClick={() => { playSound('touch'); setIsRecording(true); }} size="icon" className="rounded-full h-10 w-10 flex-shrink-0" title="Record voice message">
<Mic />
<span className="sr-only">Record</span>
</Button>
)}
</div>
</div>
);
} |