index.ts 5.4 KB

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