Skip to content

Commit

Permalink
fix(chat-message): update extractMentionsFromBody with optional chain…
Browse files Browse the repository at this point in the history
…ing for matches to prevent unwanted error (#2432)

* fix(chat-message): update extractMentionsFromBody with optional chaining for matches to prevent unwanted error

* refactor: fix issue
  • Loading branch information
domw30 authored Nov 15, 2024
1 parent 000bbd1 commit e6d862f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/components/notifications-feed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class Container extends React.Component<Properties> {
}

renderError() {
return <div className={styles.Error}>{'this.props.error'}</div>;
const { error } = this.props;
return error ? <div className={styles.Error}>{error}</div> : null;
}

render() {
Expand All @@ -103,9 +104,9 @@ export class Container extends React.Component<Properties> {
<div className={styles.Body}>
<ol className={styles.Notifications}>{notifications.length > 0 && this.renderNotifications()}</ol>

{notifications.length === 0 && !loading && this.renderNoNotifications()}
{notifications.length === 0 && !loading && !error && this.renderNoNotifications()}
{loading && this.renderLoading()}
{error && !loading && this.renderError()}
{error && this.renderError()}
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/chat/matrix/chat-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export async function mapEventToNotification(event) {
},
};

if (type === 'm.room.message') {
const mentions = extractMentionsFromBody(content.body);
if (type === 'm.room.message' && content?.body) {
const mentions = content.body ? extractMentionsFromBody(content.body) : [];
if (mentions.length > 0) {
return {
...baseNotification,
Expand Down Expand Up @@ -210,7 +210,7 @@ export async function mapEventToNotification(event) {

function extractMentionsFromBody(body: string) {
const mentionRegex = /@\[.*?\]\(user:(.*?)\)/g;
const matches = [...body.matchAll(mentionRegex)];
const matches = [...body?.matchAll(mentionRegex)];
return matches.map((match) => match[1]);
}

Expand Down
1 change: 1 addition & 0 deletions src/store/notifications/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function* fetchNotifications() {

yield put(setNotifications(notificationsWithSenders));
} catch (error: any) {
console.error('Error fetching notifications:', error);
yield put(setError(error.message));
} finally {
yield put(setLoading(false));
Expand Down

0 comments on commit e6d862f

Please sign in to comment.