import { toast } from 'react-toastify'; const format = (a: string) => a.split(' ').join('').trim() const firstLetter = (word: string) => word.slice(0, 1).toUpperCase() const slicedWord = (word: string, max: number, from: number = 0) => word.length < max ? word.slice(from) : word.slice(from, max) const timeStamp = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", { year:'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', }) const timeStampMessage = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB',{ timeZone: 'UTC', hour: 'numeric', minute: '2-digit', }) const timeStampFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", { year:'numeric', month: 'short', day: 'numeric', }) const playNotification = (url:string) => { const audio = new Audio(url); audio.play(); } const notification = (name: string, onClick: () => void) => { toast(`🦄new message from ${name}`, { position: "bottom-right", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, onClick }); } const playNotificationWithoutPermission = (url: string) => { const w:any = window const audioContext: any = new (w.AudioContext || w.webkitAudioContext)(); navigator.mediaDevices .getUserMedia({ audio: true }) .then(() => { const source = audioContext.createBufferSource(); source.addEventListener('ended', () => { source.stop(); audioContext.close(); }); const request = new XMLHttpRequest(); request.open('GET', url, true); request.responseType = 'arraybuffer'; request.onload = () => { audioContext.decodeAudioData( request.response, (buffer:any) => { source.buffer = buffer; source.connect(audioContext.destination); source.start(); }, (e:any) => { console.log('Error with decoding audio data' + e.message); }); } request.send(); }) .catch(reason => console.error(`Audio permissions denied: ${reason}`)); } export { format, firstLetter, slicedWord, timeStamp, timeStampMessage, timeStampFilter, playNotification, notification, playNotificationWithoutPermission }