index.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { toast } from 'react-toastify';
  2. const format = (a: string) => a.split(' ').join('').trim()
  3. const firstLetter = (word: string) => word.slice(0, 1).toUpperCase()
  4. const slicedWord = (word: string, max: number, from: number = 0) =>
  5. word.length < max ? word.slice(from) : word.slice(from, max)
  6. const timeStamp = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  7. year:'numeric',
  8. month: 'short',
  9. day: 'numeric',
  10. hour: 'numeric',
  11. minute: '2-digit',
  12. })
  13. const timeStampMessage = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB',{
  14. timeZone: 'UTC',
  15. hour: 'numeric',
  16. minute: '2-digit',
  17. })
  18. const timeStampFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  19. year:'numeric',
  20. month: 'short',
  21. day: 'numeric',
  22. })
  23. const playNotification = (url:string) => {
  24. const audio = new Audio(url);
  25. audio.play();
  26. }
  27. const notification = (name: string, onClick: () => void) => {
  28. toast(`🦄new message from ${name}`, {
  29. position: "bottom-right",
  30. autoClose: 5000,
  31. hideProgressBar: false,
  32. closeOnClick: true,
  33. pauseOnHover: true,
  34. draggable: true,
  35. progress: undefined,
  36. onClick
  37. });
  38. }
  39. const playNotificationWithoutPermission = (url: string) => {
  40. const w:any = window
  41. const audioContext: any = new (w.AudioContext || w.webkitAudioContext)();
  42. navigator.mediaDevices
  43. .getUserMedia({ audio: true })
  44. .then(() => {
  45. const source = audioContext.createBufferSource();
  46. source.addEventListener('ended', () => {
  47. source.stop();
  48. audioContext.close();
  49. });
  50. const request = new XMLHttpRequest();
  51. request.open('GET', url, true);
  52. request.responseType = 'arraybuffer';
  53. request.onload = () => {
  54. audioContext.decodeAudioData(
  55. request.response,
  56. (buffer:any) => {
  57. source.buffer = buffer;
  58. source.connect(audioContext.destination);
  59. source.start();
  60. },
  61. (e:any) => {
  62. console.log('Error with decoding audio data' + e.message);
  63. });
  64. }
  65. request.send();
  66. })
  67. .catch(reason => console.error(`Audio permissions denied: ${reason}`));
  68. }
  69. export {
  70. format,
  71. firstLetter,
  72. slicedWord,
  73. timeStamp,
  74. timeStampMessage,
  75. timeStampFilter,
  76. playNotification,
  77. notification,
  78. playNotificationWithoutPermission
  79. }