Add centralized typed helpers in src/app/utils/accountData.ts: - getAccountData<T>(mx, eventType): T | undefined (returns content) - setAccountData<T>(mx, eventType, content): Promise<void> These wrap the single `as any` cast needed because matrix-js-sdk's typed overloads reject the fork's custom account-data event names. Every call site now stays fully typed on its content shape. Route all account-data reads/writes that previously used `(mx as any).getAccountData/setAccountData` or `mx.getAccountData(... as any)` through the helpers (or, where a MatrixEvent is needed, through the existing utils/room.ts getAccountData whose param is widened to accept string keys). No behavior change: same event types, same content shapes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { MatrixClient } from 'matrix-js-sdk';
|
|
import { getAccountData } from '../utils/room';
|
|
import { setAccountData } from '../utils/accountData';
|
|
import { IEmoji, emojis } from './emoji';
|
|
import { AccountDataEvent } from '../../types/matrix/accountData';
|
|
|
|
type EmojiUnicode = string;
|
|
type EmojiUsageCount = number;
|
|
|
|
export type IRecentEmojiContent = {
|
|
recent_emoji?: [EmojiUnicode, EmojiUsageCount][];
|
|
};
|
|
|
|
export const getRecentEmojis = (mx: MatrixClient, limit?: number): IEmoji[] => {
|
|
const recentEmojiEvent = getAccountData(mx, AccountDataEvent.ElementRecentEmoji);
|
|
const recentEmoji = recentEmojiEvent?.getContent<IRecentEmojiContent>().recent_emoji;
|
|
if (!Array.isArray(recentEmoji)) return [];
|
|
|
|
return recentEmoji.slice(0, limit).reduce<IEmoji[]>((list, [unicode]) => {
|
|
const emoji = emojis.find((e) => e.unicode === unicode);
|
|
if (emoji) list.push(emoji);
|
|
return list;
|
|
}, []);
|
|
};
|
|
|
|
export function addRecentEmoji(mx: MatrixClient, unicode: string) {
|
|
const recentEmojiEvent = getAccountData(mx, AccountDataEvent.ElementRecentEmoji);
|
|
const recentEmojiContent = recentEmojiEvent?.getContent<IRecentEmojiContent>();
|
|
const recentEmoji =
|
|
recentEmojiContent && Array.isArray(recentEmojiContent.recent_emoji)
|
|
? structuredClone(recentEmojiContent.recent_emoji)
|
|
: [];
|
|
|
|
const emojiIndex = recentEmoji.findIndex(([u]) => u === unicode);
|
|
let entry: [EmojiUnicode, EmojiUsageCount];
|
|
if (emojiIndex < 0) {
|
|
entry = [unicode, 1];
|
|
} else {
|
|
[entry] = recentEmoji.splice(emojiIndex, 1);
|
|
entry[1] += 1;
|
|
}
|
|
recentEmoji.unshift(entry);
|
|
setAccountData(mx, AccountDataEvent.ElementRecentEmoji, {
|
|
recent_emoji: recentEmoji.slice(0, 100),
|
|
});
|
|
}
|