index.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { toast } from 'react-toastify';
  2. import FileSaver from 'file-saver';
  3. import { TChats } from '../typescript/redux/chats/types';
  4. import { TAllMessages } from '../typescript/redux/allMessages/types';
  5. const format = (a: string) => a.split(' ').join('').trim()
  6. const firstLetter = (word: string) => word.slice(0, 1).toUpperCase()
  7. const slicedWord = (word: string, max: number, from: number = 0) =>
  8. word.length < max ? word.slice(from) : word.slice(from, max)
  9. const timeStamp = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  10. year:'numeric',
  11. month: 'short',
  12. day: 'numeric',
  13. hour: 'numeric',
  14. minute: '2-digit',
  15. })
  16. const timeStampEU = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB', {
  17. year:'numeric',
  18. month: 'short',
  19. day: 'numeric',
  20. hour: 'numeric',
  21. minute: '2-digit',
  22. })
  23. const timeStampEUFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB', {
  24. year:'numeric',
  25. month: 'short',
  26. day: 'numeric',
  27. hour: 'numeric',
  28. minute: '2-digit',
  29. second: '2-digit',
  30. })
  31. const timeStampMessage = (updatedAt: string) => new Date(updatedAt).toLocaleString('en-GB',{
  32. hour: 'numeric',
  33. minute: '2-digit',
  34. })
  35. const timeStampFilter = (updatedAt: string) => new Date(updatedAt).toLocaleString("en-US", {
  36. year:'numeric',
  37. month: 'short',
  38. day: 'numeric',
  39. })
  40. const getTimeBySeconds = (seconds: number) =>
  41. Math.floor(seconds / 60) + ':' + ('0' + Math.floor(seconds % 60)).slice(-2)
  42. const playNotification = (url:string) => {
  43. const audio = new Audio(url);
  44. audio.play();
  45. }
  46. const notification = (name: string, onClick: () => void) => {
  47. toast(`🦄new message from ${name}`, {
  48. position: "bottom-right",
  49. autoClose: 5000,
  50. hideProgressBar: false,
  51. closeOnClick: true,
  52. pauseOnHover: true,
  53. draggable: true,
  54. progress: undefined,
  55. onClick
  56. });
  57. }
  58. const copied = (text:string) => {
  59. toast(`${text} Copied`, {
  60. position: "bottom-right",
  61. autoClose: 2000,
  62. hideProgressBar: false,
  63. closeOnClick: true,
  64. pauseOnHover: true,
  65. draggable: true,
  66. progress: undefined,
  67. });
  68. }
  69. const playNotificationWithoutPermission = (url: string) => {
  70. const w:any = window
  71. const audioContext: any = new (w.AudioContext || w.webkitAudioContext)();
  72. navigator.mediaDevices
  73. .getUserMedia({ audio: true })
  74. .then(() => {
  75. const source = audioContext.createBufferSource();
  76. source.addEventListener('ended', () => {
  77. source.stop();
  78. audioContext.close();
  79. });
  80. const request = new XMLHttpRequest();
  81. request.open('GET', url, true);
  82. request.responseType = 'arraybuffer';
  83. request.onload = () => {
  84. audioContext.decodeAudioData(
  85. request.response,
  86. (buffer:any) => {
  87. source.buffer = buffer;
  88. source.connect(audioContext.destination);
  89. source.start();
  90. },
  91. (e:any) => {
  92. console.log('Error with decoding audio data' + e.message);
  93. });
  94. }
  95. request.send();
  96. })
  97. .catch(reason => console.error(`Audio permissions denied: ${reason}`));
  98. }
  99. const handleDownload = async (url: string, type: string) => await FileSaver.saveAs(url, type);
  100. const handleSort = (sortBy: string, data: any,sort:boolean): any => {
  101. return [...data].sort(function (a: any, b: any) {
  102. if (sort?a[sortBy] > b[sortBy]:a[sortBy] < b[sortBy]) {
  103. return -1;
  104. } else if (sort?a[sortBy] < b[sortBy]:a[sortBy] > b[sortBy]) {
  105. return 1;
  106. }
  107. return 0;
  108. });
  109. }
  110. const sortByRecent = (chats:TChats,sort:boolean) => [...chats].sort((a, b) => {
  111. const aCreatedAt = a.lastMessageCreatedAt ? a.lastMessageCreatedAt : a.createdAt
  112. const bCreatedAt = b.lastMessageCreatedAt ? b.lastMessageCreatedAt : b.createdAt
  113. const aTime = timeStampEUFilter(aCreatedAt)
  114. const bTime = timeStampEUFilter(bCreatedAt)
  115. const direction = sort ? aTime < bTime: aTime > bTime
  116. return direction ? 1 : -1
  117. })
  118. const filteredMessages = (arr: TAllMessages,date:any,value:string) => arr.filter((el) => {
  119. if (!date) {
  120. if (!el.fullType && el.message.toLowerCase().includes(value.toLowerCase())) {
  121. return el
  122. }
  123. if (el.fullType && el.fullType.toLowerCase().includes(value.toLowerCase())) {
  124. return el
  125. }
  126. } else {
  127. if (!el.fullType && el.message.toLowerCase().includes(value.toLowerCase())
  128. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  129. return el
  130. }
  131. if (el.fullType && el.fullType.toLowerCase().includes(value.toLowerCase())
  132. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  133. return el
  134. }
  135. }
  136. })
  137. const filterBy = ['pdf/docx', 'image', 'text', 'audio', 'video']
  138. const emojisArr = ['💘','😀','😍','😲','😡']
  139. let prodBaseURL = 'https://w-telegram.herokuapp.com'
  140. let prodSocketURL = 'https://w-telegram-socket.herokuapp.com'
  141. prodSocketURL = 'http://localhost:3001'
  142. prodBaseURL = 'http://localhost:3000'
  143. const prodAwsS3 = 'https://my-telegram-bucket.s3.eu-west-1.amazonaws.com'
  144. const refreshAppTime = 3000
  145. export {
  146. format,
  147. firstLetter,
  148. slicedWord,
  149. timeStamp,
  150. timeStampEU,
  151. timeStampEUFilter,
  152. timeStampMessage,
  153. timeStampFilter,
  154. playNotification,
  155. getTimeBySeconds,
  156. notification,
  157. copied,
  158. playNotificationWithoutPermission,
  159. handleDownload,
  160. handleSort,
  161. sortByRecent,
  162. filteredMessages,
  163. filterBy,
  164. emojisArr,
  165. prodSocketURL,
  166. prodBaseURL,
  167. prodAwsS3,
  168. refreshAppTime,
  169. }