index.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. return audio;
  46. }
  47. const notification = (name: string, onClick: () => void) => {
  48. toast(`🦄new message from ${name}`, {
  49. position: "bottom-right",
  50. autoClose: 5000,
  51. hideProgressBar: false,
  52. closeOnClick: true,
  53. pauseOnHover: true,
  54. draggable: true,
  55. progress: undefined,
  56. onClick
  57. });
  58. }
  59. const copied = (text:string) => {
  60. toast(`${text} Copied`, {
  61. position: "bottom-right",
  62. autoClose: 2000,
  63. hideProgressBar: false,
  64. closeOnClick: true,
  65. pauseOnHover: true,
  66. draggable: true,
  67. progress: undefined,
  68. });
  69. }
  70. const playNotificationWithoutPermission = (url: string) => {
  71. const w:any = window
  72. const audioContext: any = new (w.AudioContext || w.webkitAudioContext)();
  73. navigator.mediaDevices
  74. .getUserMedia({ audio: true })
  75. .then(() => {
  76. const source = audioContext.createBufferSource();
  77. source.addEventListener('ended', () => {
  78. source.stop();
  79. audioContext.close();
  80. });
  81. const request = new XMLHttpRequest();
  82. request.open('GET', url, true);
  83. request.responseType = 'arraybuffer';
  84. request.onload = () => {
  85. audioContext.decodeAudioData(
  86. request.response,
  87. (buffer:any) => {
  88. source.buffer = buffer;
  89. source.connect(audioContext.destination);
  90. source.start();
  91. },
  92. (e:any) => {
  93. console.log('Error with decoding audio data' + e.message);
  94. });
  95. }
  96. request.send();
  97. })
  98. .catch(reason => console.error(`Audio permissions denied: ${reason}`));
  99. return audioContext
  100. }
  101. const handleDownload = async (url: string, type: string) => await FileSaver.saveAs(url, type);
  102. const handleSort = (sortBy: string, data: any,sort:boolean): any => {
  103. return [...data].sort(function (a: any, b: any) {
  104. if (sort?a[sortBy] > b[sortBy]:a[sortBy] < b[sortBy]) {
  105. return -1;
  106. } else if (sort?a[sortBy] < b[sortBy]:a[sortBy] > b[sortBy]) {
  107. return 1;
  108. }
  109. return 0;
  110. });
  111. }
  112. const sortByRecent = (chats:TChats,sort:boolean) => [...chats].sort((a, b) => {
  113. const aCreatedAt = a.lastMessageCreatedAt ? a.lastMessageCreatedAt : a.createdAt
  114. const bCreatedAt = b.lastMessageCreatedAt ? b.lastMessageCreatedAt : b.createdAt
  115. const aTime = timeStampEUFilter(aCreatedAt)
  116. const bTime = timeStampEUFilter(bCreatedAt)
  117. const direction = sort ? aTime < bTime: aTime > bTime
  118. return direction ? 1 : -1
  119. })
  120. const filteredMessages = (arr: TAllMessages,date:any,value:string) => arr.filter((el) => {
  121. if (!date) {
  122. if (!el.fullType && el.message.toLowerCase().includes(value.toLowerCase())) {
  123. return el
  124. }
  125. if (el.fullType && el.fullType.toLowerCase().includes(value.toLowerCase())) {
  126. return el
  127. }
  128. } else {
  129. if (!el.fullType && el.message.toLowerCase().includes(value.toLowerCase())
  130. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  131. return el
  132. }
  133. if (el.fullType && el.fullType.toLowerCase().includes(value.toLowerCase())
  134. && timeStampFilter(date) === timeStampFilter(el.createdAt)) {
  135. return el
  136. }
  137. }
  138. })
  139. const filterBy = ['pdf/docx', 'image', 'text', 'audio', 'video']
  140. const emojisArr = ['💘','😀','😍','😲','😡']
  141. let prodBaseURL = 'https://w-telegram.herokuapp.com'
  142. let prodSocketURL = 'https://w-telegram-socket.herokuapp.com'
  143. prodSocketURL = 'http://localhost:3001'
  144. prodBaseURL = 'http://localhost:3000'
  145. const prodAwsS3 = 'https://my-telegram-bucket.s3.eu-west-1.amazonaws.com'
  146. const refreshAppTime = 3000
  147. export {
  148. format,
  149. firstLetter,
  150. slicedWord,
  151. timeStamp,
  152. timeStampEU,
  153. timeStampEUFilter,
  154. timeStampMessage,
  155. timeStampFilter,
  156. playNotification,
  157. getTimeBySeconds,
  158. notification,
  159. copied,
  160. playNotificationWithoutPermission,
  161. handleDownload,
  162. handleSort,
  163. sortByRecent,
  164. filteredMessages,
  165. filterBy,
  166. emojisArr,
  167. prodSocketURL,
  168. prodBaseURL,
  169. prodAwsS3,
  170. refreshAppTime,
  171. }