-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Julian Jelfs <[email protected]>
- Loading branch information
1 parent
756f66a
commit 6dd0063
Showing
5 changed files
with
205 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ summary.md | |
temp.did | ||
website_release.md | ||
frontend/app/src/stores/timeline.ts | ||
/Makefile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { writable } from "svelte/store"; | ||
import { Database as EmojiDatabase } from "emoji-picker-element"; | ||
import type { Emoji } from "emoji-picker-element/shared"; | ||
|
||
let emojiDb = new EmojiDatabase(); | ||
let showQuickReactionCount = 3; | ||
let defaultReactions = ["yes", "tears_of_joy", "pray"]; | ||
|
||
function initQuickReactions() { | ||
|
||
// Filter the reactions by taking into account the appropriate skin tone. | ||
function getUnicodeBySkintone(skintone: number, reactions: Emoji[]): string[] { | ||
return reactions.map((emoji) => { | ||
if ("unicode" in emoji) { | ||
return undefined === emoji.skins || 0 === skintone | ||
? emoji.unicode | ||
: emoji.skins.find((val) => val.tone === skintone ? val.unicode : null)?.unicode | ||
} | ||
}) | ||
.filter((u) => u !== undefined) as string[]; | ||
} | ||
|
||
function loadQuickReactions(skintone: number) { | ||
return emojiDb | ||
.getTopFavoriteEmoji(showQuickReactionCount) | ||
.then((fav) => { | ||
const favUnicode = getUnicodeBySkintone(skintone, fav); | ||
|
||
// If we have less emoji than we want to show, expand with | ||
// a default selection of emoji. | ||
if (fav.length < showQuickReactionCount) { | ||
return Promise.all( | ||
defaultReactions.map((em) => emojiDb.getEmojiByShortcode(em)), | ||
) | ||
.then((def) => getUnicodeBySkintone(skintone, def.filter((v) => v != null) as Emoji[])) | ||
.then((defUnicode) => [...new Set(favUnicode.concat(defUnicode))].slice(0, showQuickReactionCount)) | ||
} | ||
|
||
return favUnicode; | ||
}).catch((e) => { | ||
console.log(e); | ||
return ([] as string[]); | ||
}); | ||
} | ||
|
||
function loadSkintoneAndQuickReactions() { | ||
return emojiDb | ||
.getPreferredSkinTone() | ||
.then(loadQuickReactions); | ||
} | ||
|
||
const { subscribe, set } = writable<string[]>([]); | ||
loadSkintoneAndQuickReactions().then(set); | ||
|
||
return { | ||
subscribe, | ||
|
||
// Increment favourites | ||
incrementFavourite: (unicode: string): void => { | ||
emojiDb.incrementFavoriteEmojiCount(unicode); | ||
}, | ||
|
||
// Reload reactions | ||
reload: (skintone?: number): void => { | ||
(skintone | ||
? loadQuickReactions(skintone) | ||
: loadSkintoneAndQuickReactions() | ||
).then(set); | ||
}, | ||
}; | ||
} | ||
|
||
export const quickReactions = initQuickReactions(); |