mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-10 10:12:45 +00:00
Co-authored-by: Selis <12716288+ItsSelis@users.noreply.github.com> Co-authored-by: Kashargul <144968721+Kashargul@users.noreply.github.com>
107 lines
2.5 KiB
TypeScript
107 lines
2.5 KiB
TypeScript
/**
|
|
* @file
|
|
* @copyright 2020 Aleksej Komarov
|
|
* @license MIT
|
|
*/
|
|
|
|
import { createUuid } from 'tgui-core/uuid';
|
|
|
|
import { MESSAGE_TYPE_INTERNAL, MESSAGE_TYPES } from './constants';
|
|
import type { message, Page } from './types';
|
|
|
|
export const canPageAcceptType = (page: Page, type: string): string | boolean =>
|
|
type.startsWith(MESSAGE_TYPE_INTERNAL) || page.acceptedTypes[type];
|
|
|
|
export const typeIsImportant = (type: string): boolean => {
|
|
let isImportant = false;
|
|
for (let typeDef of MESSAGE_TYPES) {
|
|
if (typeDef.type === type && !!typeDef.important) {
|
|
isImportant = true;
|
|
break;
|
|
}
|
|
}
|
|
return isImportant;
|
|
};
|
|
|
|
export const adminPageOnly = (page: Page): boolean => {
|
|
let adminTab = true;
|
|
let checked = 0;
|
|
for (let typeDef of MESSAGE_TYPES) {
|
|
if (
|
|
page.acceptedTypes[typeDef.type] &&
|
|
!(!!typeDef.important || !!typeDef.admin)
|
|
) {
|
|
adminTab = false;
|
|
break;
|
|
}
|
|
if (page.acceptedTypes[typeDef.type] && !typeDef.important) {
|
|
checked++;
|
|
}
|
|
}
|
|
return checked > 0 && adminTab;
|
|
};
|
|
|
|
export const canStoreType = (
|
|
storedTypes: Record<string, string>,
|
|
type: string,
|
|
) => storedTypes[type];
|
|
|
|
export const createPage = (obj?: Object): Page => {
|
|
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,
|
|
hideUnreadCount: false,
|
|
createdAt: Date.now(),
|
|
...obj,
|
|
};
|
|
};
|
|
|
|
export const createMainPage = (): Page => {
|
|
const acceptedTypes = {};
|
|
for (let typeDef of MESSAGE_TYPES) {
|
|
acceptedTypes[typeDef.type] = true;
|
|
}
|
|
return createPage({
|
|
isMain: true,
|
|
name: 'Main',
|
|
acceptedTypes,
|
|
});
|
|
};
|
|
|
|
export const createMessage = (payload: { type: string }): message => ({
|
|
...payload,
|
|
createdAt: Date.now(),
|
|
roundId: null,
|
|
});
|
|
|
|
export const serializeMessage = (
|
|
message: message,
|
|
archive = false,
|
|
): message => {
|
|
let archiveM = '';
|
|
if (archive && message.node && typeof message.node !== 'string') {
|
|
archiveM = message.node.outerHTML.replace(/(?:\r\n|\r|\n)/g, '<br>');
|
|
}
|
|
return {
|
|
type: message.type,
|
|
text: message.text,
|
|
html: archive ? archiveM : message.html,
|
|
times: message.times,
|
|
createdAt: message.createdAt,
|
|
roundId: message.roundId,
|
|
};
|
|
};
|
|
|
|
export const isSameMessage = (a: message, b: message): boolean =>
|
|
(typeof a.text === 'string' && a.text === b.text) ||
|
|
(typeof a.html === 'string' && a.html === b.html);
|