diff options
| author | Tulir Asokan <tulir@maunium.net> | 2020-09-06 18:25:28 +0300 |
|---|---|---|
| committer | Tulir Asokan <tulir@maunium.net> | 2020-09-06 18:25:28 +0300 |
| commit | aad04ba9b6e62aa0f900e6fece894dfaa78c0d75 (patch) | |
| tree | 369538f148d896605634d178a8fbbd09b381e01c /web/frequently-used.js | |
| parent | 4ce90892f0df459361ffceaf59ff5a1624954b87 (diff) | |
Add frequently used stickers section at top. Fixes #4
Diffstat (limited to 'web/frequently-used.js')
| -rw-r--r-- | web/frequently-used.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/web/frequently-used.js b/web/frequently-used.js new file mode 100644 index 0000000..1601e03 --- /dev/null +++ b/web/frequently-used.js @@ -0,0 +1,24 @@ +// Copyright (c) 2020 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +const FREQUENTLY_USED = JSON.parse(window.localStorage.mauFrequentlyUsedStickerIDs || "{}") +let FREQUENTLY_USED_SORTED = null + +export const add = id => { + const [count] = FREQUENTLY_USED[id] || [0] + FREQUENTLY_USED[id] = [count + 1, Date.now()] + window.localStorage.mauFrequentlyUsedStickerIDs = JSON.stringify(FREQUENTLY_USED) + FREQUENTLY_USED_SORTED = null +} + +export const get = (limit = 16) => { + if (FREQUENTLY_USED_SORTED === null) { + FREQUENTLY_USED_SORTED = Object.entries(FREQUENTLY_USED) + .sort(([, [count1, date1]], [, [count2, date2]]) => + count2 === count1 ? date2 - date1 : count2 - count1) + .map(([emoji]) => emoji) + } + return FREQUENTLY_USED_SORTED.slice(0, limit) +} |
