@chayns-components/emoji-input
Version:
Input field that supports HTML elements and emojis
89 lines (88 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useEmojiHistory = void 0;
var _react = require("react");
var _get = require("../api/item-storage/get");
var _put = require("../api/item-storage/put");
const useEmojiHistory = ({
accessToken,
personId,
selectedCategory
}) => {
const [historyEmojis, setHistoryEmojis] = (0, _react.useState)([]);
const tempHistoryEmojis = (0, _react.useRef)(null);
const addOrUpdateEmojiInHistory = (0, _react.useCallback)(async ({
emoji,
name,
skin_tone_support
}) => {
var _historyItem;
if (typeof accessToken !== 'string' || typeof personId !== 'string') {
return;
}
const newHistoryEmojis = Array.from(tempHistoryEmojis.current ?? historyEmojis);
const historyItemIndex = newHistoryEmojis.findIndex(item => item.emoji === emoji);
let historyItem;
if (historyItemIndex > -1) {
[historyItem] = newHistoryEmojis.splice(historyItemIndex, 1);
}
newHistoryEmojis.push({
count: (((_historyItem = historyItem) === null || _historyItem === void 0 ? void 0 : _historyItem.count) ?? 0) + 1,
emoji,
modifiedTime: new Date().toISOString(),
name,
skin_tone_support
});
newHistoryEmojis.sort((a, b) => {
if (a.count === b.count) {
return new Date(b.modifiedTime).getTime() - new Date(a.modifiedTime).getTime();
}
return b.count - a.count;
});
const {
data
} = await (0, _put.putEmojiHistory)({
accessToken,
personId,
value: newHistoryEmojis.slice(0, 50)
});
if (data) {
if (selectedCategory === 'history') {
tempHistoryEmojis.current = data;
} else {
tempHistoryEmojis.current = null;
setHistoryEmojis(data);
}
}
}, [accessToken, historyEmojis, personId, selectedCategory]);
const loadHistoryEmojis = (0, _react.useCallback)(async () => {
if (typeof accessToken !== 'string' || typeof personId !== 'string') {
return;
}
const {
data
} = await (0, _get.getEmojiHistory)({
accessToken,
personId
});
if (data) {
setHistoryEmojis(data);
}
}, [accessToken, personId]);
(0, _react.useEffect)(() => {
void loadHistoryEmojis();
}, [loadHistoryEmojis]);
(0, _react.useEffect)(() => {
if (selectedCategory !== 'history' && tempHistoryEmojis.current) {
setHistoryEmojis(tempHistoryEmojis.current);
}
}, [selectedCategory]);
return {
addOrUpdateEmojiInHistory,
historyEmojis: historyEmojis.slice(0, 25)
};
};
exports.useEmojiHistory = useEmojiHistory;
//# sourceMappingURL=emojiHistory.js.map