Skip to content

Commit a47ab1e

Browse files
committed
feat: add input to generate contacts button
1 parent ad00e1c commit a47ab1e

File tree

2 files changed

+106
-58
lines changed

2 files changed

+106
-58
lines changed

ts/components/dialog/debug/DebugMenuModal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import useUpdate from 'react-use/lib/useUpdate';
55
import { Flex } from '../../basic/Flex';
66
import { SpacerMD, SpacerSM } from '../../basic/Text';
77
import { updateDebugMenuModal } from '../../../state/ducks/modalDialog';
8-
import { AboutInfo, DebugActions, OtherInfo } from './components';
8+
import { AboutInfo, DataGenerationActions, DebugActions, OtherInfo } from './components';
99
import { SessionWrapperModal2 } from '../../SessionWrapperModal2';
1010
import { FeatureFlags } from './FeatureFlags';
1111
import { ReleaseChannel } from './ReleaseChannel';
@@ -62,6 +62,8 @@ export function DebugMenuModal() {
6262
>
6363
<DebugActions />
6464
<SpacerSM />
65+
<DataGenerationActions />
66+
<SpacerSM />
6567
<FeatureFlags flags={window.sessionFeatureFlags} forceUpdate={forceUpdate} />
6668
<SpacerSM />
6769
<ReleaseChannel />

ts/components/dialog/debug/components.tsx

Lines changed: 103 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import useInterval from 'react-use/lib/useInterval';
77
import { filesize } from 'filesize';
88

99
import type { PubkeyType } from 'libsession_util_nodejs';
10-
import { chunk } from 'lodash';
10+
import { chunk, toNumber } from 'lodash';
1111
import { Flex } from '../../basic/Flex';
1212
import { SpacerXS } from '../../basic/Text';
1313
import { localize } from '../../../localization/localeTools';
@@ -30,6 +30,7 @@ import { ConvoHub } from '../../../session/conversations';
3030
import { ConversationTypeEnum } from '../../../models/types';
3131
import { ContactsWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface';
3232
import { usePolling } from '../../../hooks/usePolling';
33+
import { SessionInput } from '../../inputs';
3334

3435
const hexRef = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
3536

@@ -215,61 +216,6 @@ const ClearOldLogsButton = () => {
215216
);
216217
};
217218

218-
const dummyContactPerClick = 500;
219-
220-
async function fetchContactsCountAndUpdate() {
221-
const count = (await ContactsWrapperActions.getAll()).length;
222-
if (count && Number.isFinite(count)) {
223-
return count;
224-
}
225-
return 0;
226-
}
227-
228-
function AddDummyContactButton() {
229-
const [loading, setLoading] = useState(false);
230-
const [addedCount, setAddedCount] = useState(0);
231-
232-
const { data: contactsCount } = usePolling(
233-
fetchContactsCountAndUpdate,
234-
1000,
235-
'AddDummyContactButton'
236-
);
237-
238-
return (
239-
<SessionButton
240-
onClick={async () => {
241-
if (loading) {
242-
return;
243-
}
244-
try {
245-
setLoading(true);
246-
setAddedCount(0);
247-
const chunkSize = 10;
248-
const allIndexes = Array.from({ length: dummyContactPerClick }).map((_unused, i) => i);
249-
const chunks = chunk(allIndexes, chunkSize);
250-
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
251-
// eslint-disable-next-line no-await-in-loop
252-
await Promise.all(chunks[chunkIndex].map(() => generateOneRandomContact()));
253-
setAddedCount(Math.min(chunkIndex * chunkSize, dummyContactPerClick));
254-
}
255-
} finally {
256-
setLoading(false);
257-
setAddedCount(0);
258-
}
259-
}}
260-
disabled={loading}
261-
>
262-
{loading ? (
263-
<>
264-
{addedCount}/{dummyContactPerClick}...
265-
</>
266-
) : (
267-
`Add ${dummyContactPerClick} contacts (${contactsCount})`
268-
)}
269-
</SessionButton>
270-
);
271-
}
272-
273219
export const DebugActions = () => {
274220
const dispatch = useDispatch();
275221

@@ -338,12 +284,112 @@ export const DebugActions = () => {
338284
>
339285
Open storage profile
340286
</SessionButton>
341-
<AddDummyContactButton />
342287
</Flex>
343288
</>
344289
);
345290
};
346291

292+
async function fetchContactsCountAndUpdate() {
293+
const count = (await ContactsWrapperActions.getAll()).length;
294+
if (count && Number.isFinite(count)) {
295+
return count;
296+
}
297+
return 0;
298+
}
299+
300+
function AddDummyContactButton() {
301+
const [loading, setLoading] = useState(false);
302+
const [addedCount, setAddedCount] = useState(0);
303+
const [countToAdd, setCountToAdd] = useState(500);
304+
305+
const { data: contactsCount } = usePolling(
306+
fetchContactsCountAndUpdate,
307+
500,
308+
'AddDummyContactButton'
309+
);
310+
311+
return (
312+
<div style={{ display: 'flex', alignItems: 'center' }}>
313+
<SessionInput
314+
autoFocus={false}
315+
disableOnBlurEvent={true}
316+
type="text"
317+
value={`${countToAdd}`}
318+
onValueChanged={(value: string) => {
319+
const asNumber = toNumber(value);
320+
if (Number.isFinite(asNumber)) {
321+
setCountToAdd(asNumber);
322+
}
323+
}}
324+
loading={loading}
325+
maxLength={10}
326+
ctaButton={
327+
<SessionButton
328+
onClick={async () => {
329+
if (loading) {
330+
return;
331+
}
332+
try {
333+
setLoading(true);
334+
setAddedCount(0);
335+
const chunkSize = 10;
336+
const allIndexes = Array.from({ length: countToAdd }).map((_unused, i) => i);
337+
const chunks = chunk(allIndexes, chunkSize);
338+
for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
339+
// eslint-disable-next-line no-await-in-loop
340+
await Promise.all(chunks[chunkIndex].map(() => generateOneRandomContact()));
341+
setAddedCount(Math.min(chunkIndex * chunkSize, countToAdd));
342+
}
343+
} finally {
344+
setLoading(false);
345+
setAddedCount(0);
346+
}
347+
}}
348+
disabled={loading}
349+
>
350+
{loading ? (
351+
<>
352+
{addedCount}/{countToAdd}...
353+
</>
354+
) : (
355+
`Add ${countToAdd} contacts (current: ${contactsCount})`
356+
)}
357+
</SessionButton>
358+
}
359+
/>
360+
</div>
361+
);
362+
}
363+
364+
export const DataGenerationActions = () => {
365+
return (
366+
<Flex
367+
$container={true}
368+
width={'100%'}
369+
$flexDirection="column"
370+
$justifyContent="flex-start"
371+
$alignItems="flex-start"
372+
$flexWrap="wrap"
373+
>
374+
<SpacerXS />
375+
<Flex $container={true} width="100%" $alignItems="center" $flexGap="var(--margins-xs)">
376+
<h2>Data generation</h2>
377+
</Flex>
378+
<Flex
379+
$container={true}
380+
width="100%"
381+
$flexDirection="column"
382+
$justifyContent="space-between"
383+
$alignItems="flex-start"
384+
$flexGap="var(--margins-xs)"
385+
>
386+
<AddDummyContactButton />
387+
<SpacerXS />
388+
</Flex>
389+
</Flex>
390+
);
391+
};
392+
347393
export const AboutInfo = () => {
348394
const environmentStates = [];
349395

0 commit comments

Comments
 (0)