Skip to content

Commit 1fc00af

Browse files
committed
Consolidate anonymous profile creation logic.
1 parent 7cea919 commit 1fc00af

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

utils/src/participant.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,16 @@ export function setProfile(
192192
setAnonymousProfile = false,
193193
profileType: ProfileType = ProfileType.ANONYMOUS_ANIMAL,
194194
) {
195+
// Generate random number for unique participant ID
196+
const randomNumber = Math.floor(Math.random() * 10000);
197+
195198
const generateProfileFromSet = (
196199
profileSet: {name: string; avatar: string}[],
197200
): AnonymousProfileMetadata => {
198201
// TODO: Randomly select from set
199202
const {name, avatar} = profileSet[participantNumber % profileSet.length];
200203
return {
201-
name,
204+
name: `${name} ${randomNumber}`,
202205
avatar,
203206
repeat: Math.floor(participantNumber / profileSet.length),
204207
};
@@ -212,27 +215,13 @@ export function setProfile(
212215
};
213216
};
214217

215-
// Generate random number for unique participant ID (used in publicID and anonymous participant profile)
216-
const randomNumber = Math.floor(Math.random() * 10000);
217-
218-
const generateAnonymousParticipantProfile = (): AnonymousProfileMetadata => {
219-
return {
220-
name: `Participant ${randomNumber}`,
221-
avatar: '👤',
222-
repeat: 0,
223-
};
224-
};
225-
226218
// Set anonymous profiles
227219
const profileAnimal1 = generateProfileFromSet(PROFILE_SET_ANIMALS_1);
228220
const profileAnimal2 = generateProfileFromSet(PROFILE_SET_ANIMALS_2);
229221
const profileNature = generateProfileFromSet(PROFILE_SET_NATURE);
230-
const profileAnonymousParticipant = generateAnonymousParticipantProfile();
231-
232-
// Append random number to profile names (e.g., "Bear 1002")
233-
profileAnimal1.name = `${profileAnimal1.name} ${randomNumber}`;
234-
profileAnimal2.name = `${profileAnimal2.name} ${randomNumber}`;
235-
profileNature.name = `${profileNature.name} ${randomNumber}`;
222+
const profileAnonymousParticipant = generateProfileFromSet([
223+
{name: 'Participant', avatar: '👤'},
224+
]);
236225

237226
config.anonymousProfiles[PROFILE_SET_ANIMALS_1_ID] = profileAnimal1;
238227
config.anonymousProfiles[PROFILE_SET_ANIMALS_2_ID] = profileAnimal2;
@@ -256,18 +245,16 @@ export function setProfile(
256245
`${mainProfile.name}-${color}-${randomNumber}`.toLowerCase();
257246

258247
if (setAnonymousProfile) {
259-
if (profileType === ProfileType.ANONYMOUS_PARTICIPANT) {
260-
// Use participant number profile
261-
const participantProfile =
262-
config.anonymousProfiles[PROFILE_SET_ANONYMOUS_PARTICIPANT_ID];
263-
config.name = participantProfile.name;
264-
config.avatar = participantProfile.avatar;
265-
} else if (profileType === ProfileType.ANONYMOUS_ANIMAL) {
266-
// Use animal profile (default)
267-
config.name = mainProfile.name;
268-
config.avatar = mainProfile.avatar;
248+
const profileSetMap: Partial<Record<ProfileType, string>> = {
249+
[ProfileType.ANONYMOUS_ANIMAL]: PROFILE_SET_ANIMALS_1_ID,
250+
[ProfileType.ANONYMOUS_PARTICIPANT]: PROFILE_SET_ANONYMOUS_PARTICIPANT_ID,
251+
};
252+
const profileSetId = profileSetMap[profileType];
253+
if (profileSetId) {
254+
const profile = config.anonymousProfiles[profileSetId];
255+
config.name = profile.name;
256+
config.avatar = profile.avatar;
269257
}
270-
// Note: ProfileType.DEFAULT should not reach here as setAnonymousProfile would be false
271258
config.pronouns = '';
272259
}
273260
}

0 commit comments

Comments
 (0)