Files
CHOMPStation2/tgui/packages/tgui-panel/chat/model.js
CHOMPStation2 eebf92d66f [MIRROR] TGUI 5.0 Patch 1 (#7701)
Co-authored-by: Selis <sirlionfur@hotmail.de>
2024-02-08 15:31:06 +01:00

60 lines
1.3 KiB
JavaScript

/**
* @file
* @copyright 2020 Aleksej Komarov
* @license MIT
*/
import { createUuid } from 'common/uuid';
import { MESSAGE_TYPE_INTERNAL, MESSAGE_TYPES } from './constants';
export const canPageAcceptType = (page, type) =>
type.startsWith(MESSAGE_TYPE_INTERNAL) || page.acceptedTypes[type];
export const createPage = (obj) => {
let acceptedTypes = {};
for (let typeDef of MESSAGE_TYPES) {
acceptedTypes[typeDef.type] = !!typeDef.important;
}
return {
isMain: false,
id: createUuid(),
name: 'New Tab',
acceptedTypes: acceptedTypes,
unreadCount: 0,
createdAt: Date.now(),
...obj,
};
};
export const createMainPage = () => {
const acceptedTypes = {};
for (let typeDef of MESSAGE_TYPES) {
acceptedTypes[typeDef.type] = true;
}
return createPage({
isMain: true,
name: 'Main',
acceptedTypes,
});
};
export const createMessage = (payload) => ({
createdAt: Date.now(),
...payload,
});
export const serializeMessage = (message, archive = false) => ({
type: message.type,
text: message.text,
html: archive ? message.node.outerHTML : message.html,
times: message.times,
createdAt: message.createdAt,
});
export const isSameMessage = (a, b) =>
(typeof a.text === 'string' && a.text === b.text) ||
(typeof a.html === 'string' && a.html === b.html);