Skip to content

Commit

Permalink
feat: general way to get user name/email/nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Sep 28, 2023
1 parent 8b44fbf commit 1c64ec9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/utils/getUserName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type UserData = {
email?: string | null;
name?: string | null;
nickname?: string | null;
image?: string | null;
[key: string]: unknown;
};

export const getUserName = <T extends UserData>(user?: T | null): string | null | undefined => {
if (user) {
return user.nickname || user.name || user.email;
}
return null;
};

export const prepareUserDataFromActivity = <T extends UserData, V extends { user?: T | null; ghost?: T | null }>(
value: V,
): (UserData & T) | null => {
const target = value.user || value.ghost;

if (!target) {
return null;
}

return {
...target,
email: target.email,
name: target.name,
nickname: target.nickname,
};
};

0 comments on commit 1c64ec9

Please sign in to comment.