From fe21ec8715913fe6f2a6445bd96e28542b46feec Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 24 May 2022 09:05:11 +0100 Subject: [PATCH] Consolidate random string generation to make Sonar happier (#8675) * Consolidate random string generation to make Sonar happier * Update snapshots --- src/Analytics.tsx | 5 +++-- src/components/views/messages/MLocationBody.tsx | 4 ++-- src/rageshake/rageshake.ts | 3 ++- .../__snapshots__/MLocationBody-test.tsx.snap | 12 ++++++------ 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Analytics.tsx b/src/Analytics.tsx index 639950574d..0249c6ad1a 100644 --- a/src/Analytics.tsx +++ b/src/Analytics.tsx @@ -18,6 +18,7 @@ limitations under the License. import React from 'react'; import { logger } from "matrix-js-sdk/src/logger"; import { Optional } from "matrix-events-sdk"; +import { randomString } from 'matrix-js-sdk/src/randomstring'; import { getCurrentLanguage, _t, _td, IVariables } from './languageHandler'; import PlatformPeg from './PlatformPeg'; @@ -155,9 +156,9 @@ const LAST_VISIT_TS_KEY = "mx_Riot_Analytics_lvts"; function getUid(): string { try { - let data = localStorage && localStorage.getItem(UID_KEY); + let data = localStorage?.getItem(UID_KEY); if (!data && localStorage) { - localStorage.setItem(UID_KEY, data = [...Array(16)].map(() => Math.random().toString(16)[2]).join('')); + localStorage.setItem(UID_KEY, data = randomString(16)); } return data; } catch (e) { diff --git a/src/components/views/messages/MLocationBody.tsx b/src/components/views/messages/MLocationBody.tsx index ff87af1dc3..f5cdf3a969 100644 --- a/src/components/views/messages/MLocationBody.tsx +++ b/src/components/views/messages/MLocationBody.tsx @@ -16,6 +16,7 @@ limitations under the License. import React from 'react'; import { MatrixEvent } from 'matrix-js-sdk/src/models/event'; +import { randomString } from 'matrix-js-sdk/src/randomstring'; import { _t } from '../../../languageHandler'; import Modal from '../../../Modal'; @@ -45,10 +46,9 @@ export default class MLocationBody extends React.Component { constructor(props: IBodyProps) { super(props); - const randomString = Math.random().toString(16).slice(2, 10); // multiple instances of same map might be in document // eg thread and main timeline, reply - const idSuffix = `${props.mxEvent.getId()}_${randomString}`; + const idSuffix = `${props.mxEvent.getId()}_${randomString(8)}`; this.mapId = `mx_MLocationBody_${idSuffix}`; this.state = { diff --git a/src/rageshake/rageshake.ts b/src/rageshake/rageshake.ts index 2f513b6e02..e8461eef76 100644 --- a/src/rageshake/rageshake.ts +++ b/src/rageshake/rageshake.ts @@ -39,6 +39,7 @@ limitations under the License. // the frequency with which we flush to indexeddb import { logger } from "matrix-js-sdk/src/logger"; +import { randomString } from "matrix-js-sdk/src/randomstring"; import { getCircularReplacer } from "../utils/JSON"; @@ -140,7 +141,7 @@ export class IndexedDBLogStore { private indexedDB: IDBFactory, private logger: ConsoleLogger, ) { - this.id = "instance-" + Math.random() + Date.now(); + this.id = "instance-" + randomString(16); } /** diff --git a/test/components/views/messages/__snapshots__/MLocationBody-test.tsx.snap b/test/components/views/messages/__snapshots__/MLocationBody-test.tsx.snap index 97ab9e0ac1..6123b3b7b4 100644 --- a/test/components/views/messages/__snapshots__/MLocationBody-test.tsx.snap +++ b/test/components/views/messages/__snapshots__/MLocationBody-test.tsx.snap @@ -58,7 +58,7 @@ exports[`MLocationBody without error renders map correctly 1`] = permalinkCreator={Object {}} > without error renders map correctly 1`] =
without error renders map correctly 1`] = >