UserMessage.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <script lang="ts">
  2. import dayjs from 'dayjs';
  3. import { tick, createEventDispatcher } from 'svelte';
  4. import Name from './Name.svelte';
  5. import ProfileImage from './ProfileImage.svelte';
  6. import { modelfiles, settings } from '$lib/stores';
  7. const dispatch = createEventDispatcher();
  8. export let user;
  9. export let message;
  10. export let siblings;
  11. export let isFirstMessage: boolean;
  12. export let confirmEditMessage: Function;
  13. export let showPreviousMessage: Function;
  14. export let showNextMessage: Function;
  15. export let copyToClipboard: Function;
  16. let edit = false;
  17. let editedContent = '';
  18. let messageEditTextArea: HTMLTextAreaElement;
  19. const editMessageHandler = async () => {
  20. edit = true;
  21. editedContent = message.content;
  22. await tick();
  23. messageEditTextArea.style.height = '';
  24. messageEditTextArea.style.height = `${messageEditTextArea.scrollHeight}px`;
  25. messageEditTextArea?.focus();
  26. };
  27. const editMessageConfirmHandler = async () => {
  28. confirmEditMessage(message.id, editedContent);
  29. edit = false;
  30. editedContent = '';
  31. };
  32. const cancelEditMessage = () => {
  33. edit = false;
  34. editedContent = '';
  35. };
  36. const deleteMessageHandler = async () => {
  37. dispatch('delete', message.id);
  38. };
  39. </script>
  40. <div class=" flex w-full">
  41. <ProfileImage
  42. src={message.user
  43. ? $modelfiles.find((modelfile) => modelfile.tagName === message.user)?.imageUrl ?? '/user.png'
  44. : user?.profile_image_url ?? '/user.png'}
  45. />
  46. <div class="w-full overflow-hidden">
  47. <div class="user-message">
  48. <Name>
  49. {#if message.user}
  50. {#if $modelfiles.map((modelfile) => modelfile.tagName).includes(message.user)}
  51. {$modelfiles.find((modelfile) => modelfile.tagName === message.user)?.title}
  52. {:else}
  53. You <span class=" text-gray-500 text-sm font-medium">{message?.user ?? ''}</span>
  54. {/if}
  55. {:else if $settings.showUsername}
  56. {user.name}
  57. {:else}
  58. You
  59. {/if}
  60. {#if message.timestamp}
  61. <span class=" invisible group-hover:visible text-gray-400 text-xs font-medium">
  62. {dayjs(message.timestamp * 1000).format('DD/MM/YYYY HH:mm')}
  63. </span>
  64. {/if}
  65. </Name>
  66. </div>
  67. <div
  68. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 prose-p:my-0 prose-p:-mb-4 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-6 prose-li:-mb-4 whitespace-pre-line"
  69. >
  70. {#if message.files}
  71. <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
  72. {#each message.files as file}
  73. <div>
  74. {#if file.type === 'image'}
  75. <img src={file.url} alt="input" class=" max-h-96 rounded-lg" draggable="false" />
  76. {:else if file.type === 'doc'}
  77. <button
  78. class="h-16 w-[15rem] flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none text-left"
  79. type="button"
  80. on:click={() => {
  81. if (file?.url) {
  82. window.open(file?.url, '_blank').focus();
  83. }
  84. }}
  85. >
  86. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  87. <svg
  88. xmlns="http://www.w3.org/2000/svg"
  89. viewBox="0 0 24 24"
  90. fill="currentColor"
  91. class="w-6 h-6"
  92. >
  93. <path
  94. fill-rule="evenodd"
  95. d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625ZM7.5 15a.75.75 0 0 1 .75-.75h7.5a.75.75 0 0 1 0 1.5h-7.5A.75.75 0 0 1 7.5 15Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H8.25Z"
  96. clip-rule="evenodd"
  97. />
  98. <path
  99. d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"
  100. />
  101. </svg>
  102. </div>
  103. <div class="flex flex-col justify-center -space-y-0.5">
  104. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  105. {file.name}
  106. </div>
  107. <div class=" text-gray-500 text-sm">Document</div>
  108. </div>
  109. </button>
  110. {:else if file.type === 'collection'}
  111. <button
  112. class="h-16 w-[15rem] flex items-center space-x-3 px-2.5 dark:bg-gray-600 rounded-xl border border-gray-200 dark:border-none text-left"
  113. type="button"
  114. >
  115. <div class="p-2.5 bg-red-400 text-white rounded-lg">
  116. <svg
  117. xmlns="http://www.w3.org/2000/svg"
  118. viewBox="0 0 24 24"
  119. fill="currentColor"
  120. class="w-6 h-6"
  121. >
  122. <path
  123. d="M7.5 3.375c0-1.036.84-1.875 1.875-1.875h.375a3.75 3.75 0 0 1 3.75 3.75v1.875C13.5 8.161 14.34 9 15.375 9h1.875A3.75 3.75 0 0 1 21 12.75v3.375C21 17.16 20.16 18 19.125 18h-9.75A1.875 1.875 0 0 1 7.5 16.125V3.375Z"
  124. />
  125. <path
  126. d="M15 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 17.25 7.5h-1.875A.375.375 0 0 1 15 7.125V5.25ZM4.875 6H6v10.125A3.375 3.375 0 0 0 9.375 19.5H16.5v1.125c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 0 1 3 20.625V7.875C3 6.839 3.84 6 4.875 6Z"
  127. />
  128. </svg>
  129. </div>
  130. <div class="flex flex-col justify-center -space-y-0.5">
  131. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1">
  132. {file?.title ?? `#${file.name}`}
  133. </div>
  134. <div class=" text-gray-500 text-sm">Collection</div>
  135. </div>
  136. </button>
  137. {/if}
  138. </div>
  139. {/each}
  140. </div>
  141. {/if}
  142. {#if edit === true}
  143. <div class=" w-full">
  144. <textarea
  145. id="message-edit-{message.id}"
  146. bind:this={messageEditTextArea}
  147. class=" bg-transparent outline-none w-full resize-none"
  148. bind:value={editedContent}
  149. on:input={(e) => {
  150. messageEditTextArea.style.height = `${messageEditTextArea.scrollHeight}px`;
  151. }}
  152. />
  153. <div class=" mt-2 mb-1 flex justify-center space-x-2 text-sm font-medium">
  154. <button
  155. class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded-lg"
  156. on:click={() => {
  157. editMessageConfirmHandler();
  158. }}
  159. >
  160. Save & Submit
  161. </button>
  162. <button
  163. class=" px-4 py-2 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-100 transition outline outline-1 outline-gray-200 dark:outline-gray-600 rounded-lg"
  164. on:click={() => {
  165. cancelEditMessage();
  166. }}
  167. >
  168. Cancel
  169. </button>
  170. </div>
  171. </div>
  172. {:else}
  173. <div class="w-full">
  174. <pre id="user-message">{message.content}</pre>
  175. <div class=" flex justify-start space-x-1 text-gray-700 dark:text-gray-500">
  176. {#if siblings.length > 1}
  177. <div class="flex self-center">
  178. <button
  179. class="self-center dark:hover:text-white hover:text-black transition"
  180. on:click={() => {
  181. showPreviousMessage(message);
  182. }}
  183. >
  184. <svg
  185. xmlns="http://www.w3.org/2000/svg"
  186. viewBox="0 0 20 20"
  187. fill="currentColor"
  188. class="w-4 h-4"
  189. >
  190. <path
  191. fill-rule="evenodd"
  192. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  193. clip-rule="evenodd"
  194. />
  195. </svg>
  196. </button>
  197. <div class="text-xs font-bold self-center dark:text-gray-100">
  198. {siblings.indexOf(message.id) + 1} / {siblings.length}
  199. </div>
  200. <button
  201. class="self-center dark:hover:text-white hover:text-black transition"
  202. on:click={() => {
  203. showNextMessage(message);
  204. }}
  205. >
  206. <svg
  207. xmlns="http://www.w3.org/2000/svg"
  208. viewBox="0 0 20 20"
  209. fill="currentColor"
  210. class="w-4 h-4"
  211. >
  212. <path
  213. fill-rule="evenodd"
  214. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  215. clip-rule="evenodd"
  216. />
  217. </svg>
  218. </button>
  219. </div>
  220. {/if}
  221. <button
  222. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition edit-user-message-button"
  223. on:click={() => {
  224. editMessageHandler();
  225. }}
  226. >
  227. <svg
  228. xmlns="http://www.w3.org/2000/svg"
  229. fill="none"
  230. viewBox="0 0 24 24"
  231. stroke-width="1.5"
  232. stroke="currentColor"
  233. class="w-4 h-4"
  234. >
  235. <path
  236. stroke-linecap="round"
  237. stroke-linejoin="round"
  238. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
  239. />
  240. </svg>
  241. </button>
  242. <button
  243. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  244. on:click={() => {
  245. copyToClipboard(message.content);
  246. }}
  247. >
  248. <svg
  249. xmlns="http://www.w3.org/2000/svg"
  250. fill="none"
  251. viewBox="0 0 24 24"
  252. stroke-width="1.5"
  253. stroke="currentColor"
  254. class="w-4 h-4"
  255. >
  256. <path
  257. stroke-linecap="round"
  258. stroke-linejoin="round"
  259. d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
  260. />
  261. </svg>
  262. </button>
  263. {#if !isFirstMessage}
  264. <button
  265. class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
  266. on:click={() => {
  267. deleteMessageHandler();
  268. }}
  269. >
  270. <svg
  271. xmlns="http://www.w3.org/2000/svg"
  272. fill="none"
  273. viewBox="0 0 24 24"
  274. stroke-width="1.5"
  275. stroke="currentColor"
  276. class="w-4 h-4"
  277. >
  278. <path
  279. stroke-linecap="round"
  280. stroke-linejoin="round"
  281. d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
  282. />
  283. </svg>
  284. </button>
  285. {/if}
  286. </div>
  287. </div>
  288. {/if}
  289. </div>
  290. </div>
  291. </div>