Skip to content

Commit 98b288a

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

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

utils/src/participant.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,19 @@ 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}[],
200+
repeat?: number,
197201
): AnonymousProfileMetadata => {
198202
// TODO: Randomly select from set
199203
const {name, avatar} = profileSet[participantNumber % profileSet.length];
200204
return {
201-
name,
205+
name: `${name} ${randomNumber}`,
202206
avatar,
203-
repeat: Math.floor(participantNumber / profileSet.length),
207+
repeat: repeat ?? Math.floor(participantNumber / profileSet.length),
204208
};
205209
};
206210

@@ -212,27 +216,14 @@ export function setProfile(
212216
};
213217
};
214218

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-
226219
// Set anonymous profiles
227220
const profileAnimal1 = generateProfileFromSet(PROFILE_SET_ANIMALS_1);
228221
const profileAnimal2 = generateProfileFromSet(PROFILE_SET_ANIMALS_2);
229222
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}`;
223+
const profileAnonymousParticipant = generateProfileFromSet(
224+
[{name: 'Participant', avatar: '👤'}],
225+
0,
226+
);
236227

237228
config.anonymousProfiles[PROFILE_SET_ANIMALS_1_ID] = profileAnimal1;
238229
config.anonymousProfiles[PROFILE_SET_ANIMALS_2_ID] = profileAnimal2;
@@ -256,18 +247,16 @@ export function setProfile(
256247
`${mainProfile.name}-${color}-${randomNumber}`.toLowerCase();
257248

258249
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;
250+
const profileSetMap: Partial<Record<ProfileType, string>> = {
251+
[ProfileType.ANONYMOUS_ANIMAL]: PROFILE_SET_ANIMALS_1_ID,
252+
[ProfileType.ANONYMOUS_PARTICIPANT]: PROFILE_SET_ANONYMOUS_PARTICIPANT_ID,
253+
};
254+
const profileSetId = profileSetMap[profileType];
255+
if (profileSetId) {
256+
const profile = config.anonymousProfiles[profileSetId];
257+
config.name = profile.name;
258+
config.avatar = profile.avatar;
269259
}
270-
// Note: ProfileType.DEFAULT should not reach here as setAnonymousProfile would be false
271260
config.pronouns = '';
272261
}
273262
}

0 commit comments

Comments
 (0)