Page MenuHomePhabricator

D7053.id23848.diff
No OneTemporary

D7053.id23848.diff

diff --git a/lib/shared/avatar-utils.js b/lib/shared/avatar-utils.js
new file mode 100644
--- /dev/null
+++ b/lib/shared/avatar-utils.js
@@ -0,0 +1,52 @@
+// @flow
+
+import stringHash from 'string-hash';
+
+import { selectedThreadColors } from './thread-utils.js';
+import type { EmojiAvatar } from '../types/avatar-types.js';
+
+const defaultAnonymousUserEmojiAvatar: EmojiAvatar = {
+ color: selectedThreadColors[4],
+ emoji: '👤',
+ type: 'emoji',
+};
+
+const defaultEmojiAvatars: $ReadOnlyArray<EmojiAvatar> = [
+ { color: selectedThreadColors[7], emoji: '😀', type: 'emoji' },
+ { color: selectedThreadColors[0], emoji: '😆', type: 'emoji' },
+ { color: selectedThreadColors[1], emoji: '🤩', type: 'emoji' },
+ { color: selectedThreadColors[7], emoji: '🏈', type: 'emoji' },
+ { color: selectedThreadColors[2], emoji: '👻', type: 'emoji' },
+ { color: selectedThreadColors[3], emoji: '🧑‍🚀', type: 'emoji' },
+ { color: selectedThreadColors[4], emoji: '🎩', type: 'emoji' },
+ { color: selectedThreadColors[5], emoji: '🍦', type: 'emoji' },
+ { color: selectedThreadColors[6], emoji: '🚀', type: 'emoji' },
+ { color: selectedThreadColors[8], emoji: '🥰', type: 'emoji' },
+ { color: selectedThreadColors[3], emoji: '🐬', type: 'emoji' },
+ { color: selectedThreadColors[4], emoji: '🍀', type: 'emoji' },
+ { color: selectedThreadColors[9], emoji: '🍕', type: 'emoji' },
+ { color: selectedThreadColors[0], emoji: '🙄', type: 'emoji' },
+ { color: selectedThreadColors[1], emoji: '🥳', type: 'emoji' },
+ { color: selectedThreadColors[7], emoji: '🥸', type: 'emoji' },
+ { color: selectedThreadColors[2], emoji: '🦋', type: 'emoji' },
+ { color: selectedThreadColors[9], emoji: '🏆', type: 'emoji' },
+ { color: selectedThreadColors[5], emoji: '🎲', type: 'emoji' },
+ { color: selectedThreadColors[8], emoji: '🏀', type: 'emoji' },
+];
+
+function getAvatarForUser(
+ user: ?{ +avatar?: EmojiAvatar, +username?: ?string, ... },
+): EmojiAvatar {
+ if (user?.avatar) {
+ return user.avatar;
+ }
+
+ if (!user?.username) {
+ return defaultAnonymousUserEmojiAvatar;
+ }
+
+ const avatarIndex = stringHash(user.username) % defaultEmojiAvatars.length;
+ return defaultEmojiAvatars[avatarIndex];
+}
+
+export { getAvatarForUser };
diff --git a/lib/types/avatar-types.js b/lib/types/avatar-types.js
new file mode 100644
--- /dev/null
+++ b/lib/types/avatar-types.js
@@ -0,0 +1,7 @@
+// @flow
+
+export type EmojiAvatar = {
+ +type: 'emoji',
+ +emoji: string,
+ +color: string,
+};

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 25, 12:27 PM (21 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2579692
Default Alt Text
D7053.id23848.diff (2 KB)

Event Timeline