Skip to content

Commit 2f05d65

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

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

utils/src/participant.ts

Lines changed: 24 additions & 34 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;
@@ -248,26 +239,25 @@ export function setProfile(
248239
config.anonymousProfiles[PROFILE_SET_RANDOM_3_ID] =
249240
generateRandomHashProfile();
250241

251-
// Define public ID (using anonymous animal 1 set)
252-
const mainProfile = profileAnimal1;
242+
// Define public ID (using base animal name without number)
243+
const baseName =
244+
PROFILE_SET_ANIMALS_1[participantNumber % PROFILE_SET_ANIMALS_1.length]
245+
.name;
253246
const color = COLORS[Math.floor(Math.random() * COLORS.length)];
254247

255-
config.publicId =
256-
`${mainProfile.name}-${color}-${randomNumber}`.toLowerCase();
248+
config.publicId = `${baseName}-${color}-${randomNumber}`.toLowerCase();
257249

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

0 commit comments

Comments
 (0)