index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { toast } from 'react-toastify';
  2. import FileSaver from 'file-saver';
  3. import { TChats } from '../typescript/redux/chats/types';
  4. const format = (a: string) => a.split(' ').join('').trim()
  5. const firstLetter = (word: string) => word.slice(0, 1).toUpperCase()
  6. const slicedWord = (word: string, max: number, from: number = 0) =>
  7. word.length < max ? word.slice(from) : word.slice(from, max)
  8. const timeStamp = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  9. year:'numeric',
  10. month: 'short',
  11. day: 'numeric',
  12. hour: 'numeric',
  13. minute: '2-digit',
  14. })
  15. const timeStampEU = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB', {
  16. year:'numeric',
  17. month: 'short',
  18. day: 'numeric',
  19. hour: 'numeric',
  20. minute: '2-digit',
  21. })
  22. const timeStampEUFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB', {
  23. year:'numeric',
  24. month: 'short',
  25. day: 'numeric',
  26. hour: 'numeric',
  27. minute: '2-digit',
  28. second: '2-digit',
  29. })
  30. const timeStampMessage = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB',{
  31. hour: 'numeric',
  32. minute: '2-digit',
  33. })
  34. const timeStampFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  35. year:'numeric',
  36. month: 'short',
  37. day: 'numeric',
  38. })
  39. const playNotification = (url:string) => {
  40. const audio = new Audio(url);
  41. audio.play();
  42. }
  43. const notification = (name: string, onClick: () => void) => {
  44. toast(`🦄new message from ${name}`, {
  45. position: "bottom-right",
  46. autoClose: 5000,
  47. hideProgressBar: false,
  48. closeOnClick: true,
  49. pauseOnHover: true,
  50. draggable: true,
  51. progress: undefined,
  52. onClick
  53. });
  54. }
  55. const copied = (text:string) => {
  56. toast(`${text} Copied`, {
  57. position: "bottom-right",
  58. autoClose: 2000,
  59. hideProgressBar: false,
  60. closeOnClick: true,
  61. pauseOnHover: true,
  62. draggable: true,
  63. progress: undefined,
  64. });
  65. }
  66. const playNotificationWithoutPermission = (url: string) => {
  67. const w:any = window
  68. const audioContext: any = new (w.AudioContext || w.webkitAudioContext)();
  69. navigator.mediaDevices
  70. .getUserMedia({ audio: true })
  71. .then(() => {
  72. const source = audioContext.createBufferSource();
  73. source.addEventListener('ended', () => {
  74. source.stop();
  75. audioContext.close();
  76. });
  77. const request = new XMLHttpRequest();
  78. request.open('GET', url, true);
  79. request.responseType = 'arraybuffer';
  80. request.onload = () => {
  81. audioContext.decodeAudioData(
  82. request.response,
  83. (buffer:any) => {
  84. source.buffer = buffer;
  85. source.connect(audioContext.destination);
  86. source.start();
  87. },
  88. (e:any) => {
  89. console.log('Error with decoding audio data' + e.message);
  90. });
  91. }
  92. request.send();
  93. })
  94. .catch(reason => console.error(`Audio permissions denied: ${reason}`));
  95. }
  96. const handleDownload = async (url: string, type: string) => await FileSaver.saveAs(url, type);
  97. const handleSort = (sortBy: string, data: any,sort:boolean): any => {
  98. return [...data].sort(function (a: any, b: any) {
  99. if (sort?a[sortBy] > b[sortBy]:a[sortBy] < b[sortBy]) {
  100. return -1;
  101. } else if (sort?a[sortBy] < b[sortBy]:a[sortBy] > b[sortBy]) {
  102. return 1;
  103. }
  104. return 0;
  105. });
  106. };
  107. const sortByRecent = (chats:TChats,sort:boolean) => [...chats].sort((a, b) => {
  108. const aCreatedAt = a.lastMessageCreatedAt ? a.lastMessageCreatedAt : a.createdAt
  109. const bCreatedAt = b.lastMessageCreatedAt ? b.lastMessageCreatedAt : b.createdAt
  110. const aTime = timeStampEUFilter(aCreatedAt)
  111. const bTime = timeStampEUFilter(bCreatedAt)
  112. const direction = sort ? aTime < bTime: aTime > bTime
  113. return direction ? 1 : -1
  114. })
  115. const prodBaseURL = 'https://w-telegram.herokuapp.com'
  116. export {
  117. format,
  118. firstLetter,
  119. slicedWord,
  120. timeStamp,
  121. timeStampEU,
  122. timeStampEUFilter,
  123. timeStampMessage,
  124. timeStampFilter,
  125. playNotification,
  126. notification,
  127. copied,
  128. playNotificationWithoutPermission,
  129. handleDownload,
  130. handleSort,
  131. sortByRecent,
  132. prodBaseURL,
  133. }