You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
930 B

  1. // Copyright (c) 2020 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. const FREQUENTLY_USED = JSON.parse(window.localStorage.mauFrequentlyUsedStickerIDs || "{}")
  7. let FREQUENTLY_USED_SORTED = null
  8. export const add = id => {
  9. const [count] = FREQUENTLY_USED[id] || [0]
  10. FREQUENTLY_USED[id] = [count + 1, Date.now()]
  11. window.localStorage.mauFrequentlyUsedStickerIDs = JSON.stringify(FREQUENTLY_USED)
  12. FREQUENTLY_USED_SORTED = null
  13. }
  14. export const get = (limit = 16) => {
  15. if (FREQUENTLY_USED_SORTED === null) {
  16. FREQUENTLY_USED_SORTED = Object.entries(FREQUENTLY_USED)
  17. .sort(([, [count1, date1]], [, [count2, date2]]) =>
  18. count2 === count1 ? date2 - date1 : count2 - count1)
  19. .map(([emoji]) => emoji)
  20. }
  21. return FREQUENTLY_USED_SORTED.slice(0, limit)
  22. }