From e5348bffc164132b70c2325f080aacba72e5ebe3 Mon Sep 17 00:00:00 2001 From: SrashtiChauhan Date: Thu, 14 May 2026 15:20:18 +0530 Subject: [PATCH] feat(chat): add message search and filter support --- frontend/app/chat/page.tsx | 118 +++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/frontend/app/chat/page.tsx b/frontend/app/chat/page.tsx index b58f641..75c0714 100644 --- a/frontend/app/chat/page.tsx +++ b/frontend/app/chat/page.tsx @@ -25,7 +25,9 @@ export default function ChatPage() { const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [selectedImage, setSelectedImage] = useState(null); const [isRecording, setIsRecording] = useState(false); -const [audioBlob, setAudioBlob] = useState(null); + const [audioBlob, setAudioBlob] = useState(null); + const [searchQuery, setSearchQuery] = useState(""); + const [filterType, setFilterType] = useState("all"); const mediaRecorderRef = useRef(null); const audioChunksRef = useRef([]); @@ -244,6 +246,24 @@ function stopRecording() { socketRef.current?.emit("react", { messageId, emoji, username,}); } +const filteredMessages = messages.filter((msg) => { + const query = searchQuery.toLowerCase(); + + const matchesSearch = + msg.text?.toLowerCase().includes(query) || + msg.user?.toLowerCase().includes(query) || + (query === "image" && msg.image) || + (query === "voice" && msg.audio); + if (filterType === "image") { + return matchesSearch && msg.image; + } + + if (filterType === "voice") { + return matchesSearch && msg.audio; + } + + return matchesSearch; +}); @@ -279,6 +299,66 @@ return ( {onlineUsers.join(", ")} + {/* SEARCH + FILTER */} +
+ + {/* SEARCH INPUT */} +
+ setSearchQuery(e.target.value)} + placeholder="Search messages or users..." + className="w-full rounded-xl border border-(--line) px-4 py-2 pr-10 text-sm outline-none transition focus:border-slate-400" + /> + + {searchQuery && ( + + )} +
+ + {/* FILTER BUTTONS */} +
+ + + + + +
+
+ {/* MESSAGES */}
- {messages.map((msg, i) => { - const prevMsg = messages[i - 1]; + {filteredMessages.map((msg, i) => { + const prevMsg = filteredMessages[i - 1]; const isSameUser = prevMsg && prevMsg.user === msg.user; const isMe = msg.user === username; @@ -336,7 +416,25 @@ return ( {/* TEXT */} {msg.text && (

- {msg.text} + {searchQuery ? ( + msg.text.split( + new RegExp(`(${searchQuery.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})`,"gi") + ).map((part, index) => + part.toLowerCase() === + searchQuery.toLowerCase() ? ( + + {part} + + ) : ( + part + ) + ) + ) : ( + msg.text + )}

)} @@ -399,6 +497,18 @@ return ( ); })} + {filteredMessages.length === 0 && ( +
+

+ No matching messages found +

+ +

+ Try a different search or filter. +

+
+ )} +