docbrown/app/javascript/chat/util.js
Nick Taylor 24621b03f2 Restructure pack files part I (#377)
* Renamed pack file from chat.jsx to Chat.jsx.

* refactor

* Missed a js pack where chat.js -> Chat.jsx

* 👷Refactor

* 👷Refactor

* 👷Refactor

* 👷Refactor

* Tests

* Fixed some wording in a test description.
2018-08-29 12:59:20 -04:00

121 lines
3.2 KiB
JavaScript

import 'intersection-observer';
import { sendKeys } from './actions';
export function getCsrfToken() {
const element = document.querySelector(`meta[name='csrf-token']`);
return element !== null ? element.content : undefined;
}
const getWaitOnUserDataHandler = ({ resolve, reject, waitTime = 20 }) => {
let totalTimeWaiting = 0;
return function waitingOnUserData() {
if (totalTimeWaiting === 3000) {
reject(new Error("Couldn't find user data on page."));
return;
}
const csrfToken = getCsrfToken(document);
const { user } = document.body.dataset;
if (user && csrfToken !== undefined) {
const currentUser = JSON.parse(user);
resolve({ currentUser, csrfToken });
return;
}
totalTimeWaiting += waitTime;
setTimeout(waitingOnUserData, waitTime);
};
};
export function getUserDataAndCsrfToken() {
return new Promise((resolve, reject) => {
getWaitOnUserDataHandler({ resolve, reject })();
});
}
export function scrollToBottom() {
const element = document.getElementById('messagelist');
element.scrollTop = element.scrollHeight;
}
export function setupObserver(callback) {
const sentinel = document.querySelector('#messagelist__sentinel');
const somethingObserver = new IntersectionObserver(callback, {
threshold: [0, 1],
});
somethingObserver.observe(sentinel);
}
export function hideMessages(messages, userId) {
const cleanedMessages = Object.keys(messages).reduce(
(accumulator, channelId) => {
const newMessages = messages[channelId].map(message => {
if (message.user_id === userId) {
const messageClone = Object.assign({ type: 'hidden' }, message);
messageClone.message = '<message removed>';
messageClone.messageColor = 'lightgray';
return messageClone;
}
return message;
});
return { ...accumulator, [channelId]: newMessages };
},
{},
);
return cleanedMessages;
}
export function adjustTimestamp(timestamp) {
let time = new Date(timestamp);
const options = {
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
};
time = new Intl.DateTimeFormat('en-US', options).format(time);
return time;
}
export function setupNotifications() {
navigator.serviceWorker.ready.then(serviceWorkerRegistration => {
serviceWorkerRegistration.pushManager
.getSubscription()
.then(subscription => {
if (subscription) {
return subscription;
}
return serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: window.vapidPublicKey,
});
})
.then(subscription => {
sendKeys(subscription.toJSON(), null, null);
});
});
}
export function getNotificationState() {
// Not yet ready
if (!window.location.href.includes('ask-for-notifications')) {
return 'dont-ask';
}
// Let's check if the browser supports notifications
if (!('Notification' in window)) {
return 'not-supported';
}
const { permission } = Notification;
if (permission === 'granted') {
setupNotifications();
}
return permission === 'default' ? 'waiting-permission' : permission;
}