From d53f65a16c18ab9890fdb0fb8f37c022a390af89 Mon Sep 17 00:00:00 2001 From: abhishek-01k Date: Thu, 3 Oct 2024 13:42:39 +0530 Subject: [PATCH] fix: modified some parts in helper func --- .../lib/components/chat/helpers/twitter.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/uiweb/src/lib/components/chat/helpers/twitter.ts b/packages/uiweb/src/lib/components/chat/helpers/twitter.ts index 8414bd317..1e8a98435 100644 --- a/packages/uiweb/src/lib/components/chat/helpers/twitter.ts +++ b/packages/uiweb/src/lib/components/chat/helpers/twitter.ts @@ -7,23 +7,25 @@ export const checkTwitterUrl = (message: string): TwitterFeedReturnType => { const URL_REGEX = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/)?([\w#!:.?+=&%@!-]+)/; const messageContent = typeof message === 'string' ? message.split(' ') : []; - for (let i = 0; i < messageContent?.length; i++) { - if ( - (URL_REGEX.test(messageContent[i]) && messageContent[i].toLowerCase().includes('twitter')) || - messageContent[i].toLowerCase().includes('x') - ) { - // Extracting tweetId - const wordArray = messageContent[i].split('?')[0].split('/'); // split url at '?' and take first element and split at '/' - if (wordArray?.length >= 6) { - tweetId = wordArray[wordArray?.length - 1]; + messageContent?.forEach((message) => { + if (isTweet) return; // Exit the iteration if the tweet was already found + + const lowerCaseMessage = message.toLowerCase(); + + // Check if the message contains a Twitter URL or the letter 'x' + if (URL_REGEX.test(message) && (lowerCaseMessage.includes('twitter') || lowerCaseMessage.includes('x'))) { + // Extract tweetId by splitting the URL before the '?' and then splitting by '/' + const urlParts = message.split('?')[0].split('/'); + + // Ensure the URL has at least 6 parts to extract the tweetId + if (urlParts.length >= 6) { + tweetId = urlParts[urlParts.length - 1]; isTweet = true; - break; } else { isTweet = false; - break; } } - } + }); return { tweetId, isTweet }; };