From 94d2222d8d030f0aba9cb6ad0f84f2464181634b Mon Sep 17 00:00:00 2001 From: Jeffrey Yu <35394596+JeffreytheCoder@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:24:03 -0800 Subject: [PATCH] Make search tab consistent with threads & RC (#497) * Make search tab consistent with threads & RC * Fix linting errors --- .../react/src/components/Message/Message.js | 3 +- .../components/SearchMessage/SearchMessage.js | 188 +++++++++++------- .../SearchMessage/SearchMessage.module.css | 6 + 3 files changed, 125 insertions(+), 72 deletions(-) diff --git a/packages/react/src/components/Message/Message.js b/packages/react/src/components/Message/Message.js index f07cc9727..2bbb640a9 100644 --- a/packages/react/src/components/Message/Message.js +++ b/packages/react/src/components/Message/Message.js @@ -53,6 +53,7 @@ const Message = ({ showAvatar = false, className = '', style = {}, + showToolbox = true, }) => { const { classNames, styleOverrides } = useComponentOverrides( 'Message', @@ -215,7 +216,7 @@ const Message = ({ handleOpenThread={handleOpenThread} /> ) : null} - {!message.t ? ( + {!message.t && showToolbox ? ( { const { RCInstance } = useContext(RCContext); const setShowSearch = useSearchMessageStore((state) => state.setShowSearch); - const authenticatedUserUsername = useUserStore((state) => state.username); const toggleShowSearch = () => { setShowSearch(false); @@ -27,83 +22,134 @@ const Search = () => { const [text, setText] = useState(''); const [messageList, setMessageList] = useState([]); + const handleInputChange = (e) => { + setText(e.target.value); + }; + const searchMessages = async () => { const { messages } = await RCInstance.getSearchMessages(text); setMessageList(messages); }; - const handleKeyPress = (e) => { - if (e.key === 'Enter') { - searchMessages(); + const debouncedSearch = debounce(async () => { + await searchMessages(); + }, 500); // 500ms delay + + useEffect(() => { + if (!text.trim()) { + setMessageList([]); + } else { + debouncedSearch(); } - }; + + // Cleanup function to cancel the debounce on component unmount + return () => { + debouncedSearch.cancel(); + }; + }, [text, debouncedSearch]); const isMessageNewDay = (current, previous) => !previous || !isSameDay(new Date(current.ts), new Date(previous.ts)); return ( - -

- - Search Messages - - - -

-
- - setText(e.target.value)} - onKeyDown={handleKeyPress} - className={classes.textInput} - /> + + +

+ + + Search Messages + + + + +

+
+ + - - - {messageList && - messageList.map((msg, index, arr) => { - const prev = arr[index + 1]; - const newDay = isMessageNewDay(msg, prev); - return ( - - {newDay && ( - - {format(new Date(msg.ts), 'MMMM d, yyyy')} - - )} - - - - - - - +
+ + {messageList.length === 0 ? ( + + + + No results found + + + ) : ( + messageList.map((msg, index, arr) => { + const prev = arr[index + 1]; + const newDay = isMessageNewDay(msg, prev); + const sequential = isMessageSequential(msg, prev, 300); + return ( + + {newDay && ( + + + {format(new Date(msg.ts), 'MMMM d, yyyy')} + + + )} + - - - - ); - })} + + ); + }) + )} + +
); }; diff --git a/packages/react/src/components/SearchMessage/SearchMessage.module.css b/packages/react/src/components/SearchMessage/SearchMessage.module.css index 8369e2381..a90e7f4c6 100644 --- a/packages/react/src/components/SearchMessage/SearchMessage.module.css +++ b/packages/react/src/components/SearchMessage/SearchMessage.module.css @@ -11,6 +11,12 @@ z-index: 100; } +.wrapContainer { + height: 100%; + display: flex; + flex-direction: column; +} + .container { display: flex; align-items: center;