Skip to content

Commit 6cd5704

Browse files
authored
Merge pull request #193 from dozro/sonar-qube/fixes-2026-03-11
[internal] Fix a few maintainability issues raised by SonarQube
2 parents ee5b34c + 14dcb50 commit 6cd5704

3 files changed

Lines changed: 33 additions & 35 deletions

File tree

src/app/components/message/modals/MessageForward.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ type ForwardMeta = {
8787
original_event_private: boolean;
8888
};
8989

90-
export function MessageForwardInternal({ room, mEvent, onClose }: MessageForwardInternalProps) {
90+
export function MessageForwardInternal({
91+
room,
92+
mEvent,
93+
onClose,
94+
}: Readonly<MessageForwardInternalProps>) {
9195
const mx = useMatrixClient();
9296

9397
const [isTargetSelected, setIsTargetSelected] = useState(false);
@@ -191,7 +195,7 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward
191195
const safeHtml =
192196
originalFormattedBody !== undefined
193197
? sanitizeCustomHtml(originalFormattedBody)
194-
: sanitizeCustomHtml(originalBody).replace(/\n/g, '<br>');
198+
: sanitizeCustomHtml(originalBody).replaceAll('\n', '<br>');
195199

196200
newBodyHtml =
197201
`<div data-forward-marker>` +
@@ -243,19 +247,13 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward
243247
};
244248
}
245249

246-
try {
247-
mx.sendEvent(targetRoom.roomId, null, eventType, content as unknown as SendEventContent)
248-
.then(() => setIsForwardSuccess(true))
249-
.catch(() => {
250-
setIsForwarding(false);
251-
setIsForwardSuccess(false);
252-
setIsForwardError(true);
253-
});
254-
} catch {
255-
setIsForwarding(false);
256-
setIsForwardSuccess(false);
257-
setIsForwardError(true);
258-
}
250+
mx.sendEvent(targetRoom.roomId, null, eventType, content as unknown as SendEventContent)
251+
.then(() => setIsForwardSuccess(true))
252+
.catch(() => {
253+
setIsForwarding(false);
254+
setIsForwardSuccess(false);
255+
setIsForwardError(true);
256+
});
259257
};
260258

261259
return (

src/app/features/room/RoomInput.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,19 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
312312

313313
if (htmlBody) {
314314
const strippedHtml = trimReplyFromFormattedBody(htmlBody)
315-
.replace(/<br\s*\/?>/gi, ' ')
316-
.replace(/<\/p>\s*<p[^>]*>/gi, ' ')
317-
.replace(/<\/?p[^>]*>/gi, '')
318-
.replace(/(?:\r\n|\r|\n)/g, ' ');
315+
.replaceAll(/<br\s*\/?>/gi, ' ')
316+
.replaceAll(/<\/p>\s*<p[^>]*>/gi, ' ')
317+
.replaceAll(/<\/?p[^>]*>/gi, '')
318+
.replaceAll(/(?:\r\n|\r|\n)/g, ' ')
319+
.trim();
319320
const parserOpts = getReactCustomHtmlParser(mx, roomId, {
320321
linkifyOpts: LINKIFY_OPTS,
321322
useAuthentication,
322323
nicknames,
323324
});
324325
replyBodyJSX = parse(strippedHtml, parserOpts);
325326
} else if (plainBody) {
326-
const strippedBody = trimReplyFromBody(plainBody).replace(/(?:\r\n|\r|\n)/g, ' ');
327+
const strippedBody = trimReplyFromBody(plainBody).replaceAll(/(?:\r\n|\r|\n)/g, ' ');
327328
replyBodyJSX = scaleSystemEmoji(strippedBody);
328329
}
329330

@@ -333,11 +334,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
333334

334335
useEffect(
335336
() => () => {
336-
if (!isEmptyEditor(editor)) {
337-
const parsedDraft = JSON.parse(JSON.stringify(editor.children));
338-
setMsgDraft(parsedDraft);
339-
} else {
337+
if (isEmptyEditor(editor)) {
340338
setMsgDraft([]);
339+
} else {
340+
const parsedDraft = structuredClone(editor.children);
341+
setMsgDraft(parsedDraft);
341342
}
342343
resetEditor(editor);
343344
resetEditorHistory(editor);

src/app/features/room/RoomTimeline.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export const getEventIdAbsoluteIndex = (
240240
eventTimeline: EventTimeline,
241241
eventId: string
242242
): number | undefined => {
243-
const timelineIndex = timelines.findIndex((t) => t === eventTimeline);
243+
const timelineIndex = timelines.indexOf(eventTimeline);
244244
if (timelineIndex === -1) return undefined;
245245

246246
const currentEvents = eventTimeline.getEvents();
@@ -285,17 +285,17 @@ const useEventTimelineLoader = (
285285
async (eventId: string) => {
286286
const withTimeout = async <T,>(promise: Promise<T>, timeoutMs: number): Promise<T> =>
287287
new Promise<T>((resolve, reject) => {
288-
const timeoutId = window.setTimeout(() => {
288+
const timeoutId = globalThis.setTimeout(() => {
289289
reject(new Error('Timed out loading event timeline'));
290290
}, timeoutMs);
291291

292292
promise
293293
.then((value) => {
294-
window.clearTimeout(timeoutId);
294+
globalThis.clearTimeout(timeoutId);
295295
resolve(value);
296296
})
297297
.catch((error) => {
298-
window.clearTimeout(timeoutId);
298+
globalThis.clearTimeout(timeoutId);
299299
reject(error);
300300
});
301301
});
@@ -381,7 +381,7 @@ const useTimelinePagination = (
381381
const { linkedTimelines: lTimelines } = timelineRef.current;
382382
const timelinesEventsCount = lTimelines.map(timelineToEventsCount);
383383

384-
const timelineToPaginate = backwards ? lTimelines[0] : lTimelines[lTimelines.length - 1];
384+
const timelineToPaginate = backwards ? lTimelines[0] : lTimelines.at(-1);
385385
if (!timelineToPaginate) return;
386386

387387
const paginationToken = timelineToPaginate.getPaginationToken(
@@ -688,8 +688,7 @@ export function RoomTimeline({
688688
eventId ? getEmptyTimeline() : getInitialTimeline(room)
689689
);
690690
const eventsLength = getTimelinesEventsCount(timeline.linkedTimelines);
691-
const liveTimelineLinked =
692-
timeline.linkedTimelines[timeline.linkedTimelines.length - 1] === getLiveTimeline(room);
691+
const liveTimelineLinked = timeline.linkedTimelines.at(-1) === getLiveTimeline(room);
693692
const canPaginateBack =
694693
typeof timeline.linkedTimelines[0]?.getPaginationToken(Direction.Backward) === 'string';
695694
const rangeAtStart = timeline.range.start === 0;
@@ -1091,7 +1090,7 @@ export function RoomTimeline({
10911090

10921091
// scroll to focused message
10931092
useLayoutEffect(() => {
1094-
if (focusItem && focusItem.scrollTo) {
1093+
if (focusItem?.scrollTo) {
10951094
scrollToItem(focusItem.index, {
10961095
behavior: 'instant',
10971096
align: 'center',
@@ -1357,7 +1356,7 @@ export function RoomTimeline({
13571356
{
13581357
[MessageEvent.RoomMessage]: (mEventId, mEvent, item, timelineSet, collapse) => {
13591358
const reactionRelations = getEventReactions(timelineSet, mEventId);
1360-
const reactions = reactionRelations && reactionRelations.getSortedAnnotationsByKey();
1359+
const reactions = reactionRelations?.getSortedAnnotationsByKey();
13611360
const hasReactions = reactions && reactions.length > 0;
13621361
const { replyEventId, threadRootId } = mEvent;
13631362
const highlighted = focusItem?.index === item && focusItem.highlight;
@@ -1486,7 +1485,7 @@ export function RoomTimeline({
14861485
},
14871486
[MessageEvent.RoomMessageEncrypted]: (mEventId, mEvent, item, timelineSet, collapse) => {
14881487
const reactionRelations = getEventReactions(timelineSet, mEventId);
1489-
const reactions = reactionRelations && reactionRelations.getSortedAnnotationsByKey();
1488+
const reactions = reactionRelations?.getSortedAnnotationsByKey();
14901489
const hasReactions = reactions && reactions.length > 0;
14911490
const { replyEventId, threadRootId } = mEvent;
14921491
const highlighted = focusItem?.index === item && focusItem.highlight;
@@ -1626,7 +1625,7 @@ export function RoomTimeline({
16261625
},
16271626
[MessageEvent.Sticker]: (mEventId, mEvent, item, timelineSet, collapse) => {
16281627
const reactionRelations = getEventReactions(timelineSet, mEventId);
1629-
const reactions = reactionRelations && reactionRelations.getSortedAnnotationsByKey();
1628+
const reactions = reactionRelations?.getSortedAnnotationsByKey();
16301629
const hasReactions = reactions && reactions.length > 0;
16311630
const { replyEventId, threadRootId } = mEvent;
16321631
const highlighted = focusItem?.index === item && focusItem.highlight;

0 commit comments

Comments
 (0)