File
Blob: src/client/lib/emoji/recents.ts
| 1 | import { STORAGE_KEYS } from "@/client/lib/constants"; |
| 2 | import { readVersionedStorageJson, writeVersionedStorageJson } from "@/client/lib/storage"; |
| 3 | |
| 4 | const MAX_RECENTS = 16; |
| 5 | const EMOJI_RECENTS_VERSION = 1; |
| 6 | |
| 7 | function parseEmojiList(value: unknown): string[] | null { |
| 8 | if (!Array.isArray(value)) return null; |
| 9 | return value.filter((item): item is string => typeof item === "string" && item.length > 0).slice(0, MAX_RECENTS); |
| 10 | } |
| 11 | |
| 12 | export function readRecentEmojis(): string[] { |
| 13 | return readVersionedStorageJson(STORAGE_KEYS.EMOJI_RECENTS, EMOJI_RECENTS_VERSION, parseEmojiList) ?? []; |
| 14 | } |
| 15 | |
| 16 | export function writeRecentEmoji(emoji: string): string[] { |
| 17 | if (!emoji) return readRecentEmojis(); |
| 18 | const previous = readRecentEmojis().filter((item) => item !== emoji); |
| 19 | const next = [emoji, ...previous].slice(0, MAX_RECENTS); |
| 20 | writeVersionedStorageJson(STORAGE_KEYS.EMOJI_RECENTS, EMOJI_RECENTS_VERSION, next); |
| 21 | return next; |
| 22 | } |