Chats.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { chats, user, settings, scrollPaginationEnabled, currentChatPage } from '$lib/stores';
  5. import {
  6. archiveAllChats,
  7. createNewChat,
  8. deleteAllChats,
  9. getAllChats,
  10. getAllUserChats,
  11. getChatList
  12. } from '$lib/apis/chats';
  13. import { getImportOrigin, convertOpenAIChats } from '$lib/utils';
  14. import { onMount, getContext } from 'svelte';
  15. import { goto } from '$app/navigation';
  16. import { toast } from 'svelte-sonner';
  17. import ArchivedChatsModal from '$lib/components/layout/ArchivedChatsModal.svelte';
  18. const i18n = getContext('i18n');
  19. export let saveSettings: Function;
  20. // Chats
  21. let importFiles;
  22. let showArchiveConfirm = false;
  23. let showDeleteConfirm = false;
  24. let showArchivedChatsModal = false;
  25. let chatImportInputElement: HTMLInputElement;
  26. $: if (importFiles) {
  27. console.log(importFiles);
  28. let reader = new FileReader();
  29. reader.onload = (event) => {
  30. let chats = JSON.parse(event.target.result);
  31. console.log(chats);
  32. if (getImportOrigin(chats) == 'openai') {
  33. try {
  34. chats = convertOpenAIChats(chats);
  35. } catch (error) {
  36. console.log('Unable to import chats:', error);
  37. }
  38. }
  39. importChats(chats);
  40. };
  41. if (importFiles.length > 0) {
  42. reader.readAsText(importFiles[0]);
  43. }
  44. }
  45. const importChats = async (_chats) => {
  46. for (const chat of _chats) {
  47. console.log(chat);
  48. if (chat.chat) {
  49. await createNewChat(localStorage.token, chat.chat);
  50. } else {
  51. await createNewChat(localStorage.token, chat);
  52. }
  53. }
  54. currentChatPage.set(1);
  55. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  56. scrollPaginationEnabled.set(true);
  57. };
  58. const exportChats = async () => {
  59. let blob = new Blob([JSON.stringify(await getAllChats(localStorage.token))], {
  60. type: 'application/json'
  61. });
  62. saveAs(blob, `chat-export-${Date.now()}.json`);
  63. };
  64. const archiveAllChatsHandler = async () => {
  65. await goto('/');
  66. await archiveAllChats(localStorage.token).catch((error) => {
  67. toast.error(`${error}`);
  68. });
  69. currentChatPage.set(1);
  70. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  71. scrollPaginationEnabled.set(true);
  72. };
  73. const deleteAllChatsHandler = async () => {
  74. await goto('/');
  75. await deleteAllChats(localStorage.token).catch((error) => {
  76. toast.error(`${error}`);
  77. });
  78. currentChatPage.set(1);
  79. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  80. scrollPaginationEnabled.set(true);
  81. };
  82. const handleArchivedChatsChange = async () => {
  83. currentChatPage.set(1);
  84. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  85. scrollPaginationEnabled.set(true);
  86. };
  87. </script>
  88. <ArchivedChatsModal bind:show={showArchivedChatsModal} onUpdate={handleArchivedChatsChange} />
  89. <div id="tab-chats" class="flex flex-col h-full justify-between space-y-3 text-sm">
  90. <div class=" space-y-2 overflow-y-scroll max-h-[28rem] lg:max-h-full">
  91. <div class="flex flex-col">
  92. <input
  93. id="chat-import-input"
  94. bind:this={chatImportInputElement}
  95. bind:files={importFiles}
  96. type="file"
  97. accept=".json"
  98. hidden
  99. />
  100. <button
  101. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  102. on:click={() => {
  103. chatImportInputElement.click();
  104. }}
  105. >
  106. <div class=" self-center mr-3">
  107. <svg
  108. xmlns="http://www.w3.org/2000/svg"
  109. viewBox="0 0 16 16"
  110. fill="currentColor"
  111. class="w-4 h-4"
  112. >
  113. <path
  114. fill-rule="evenodd"
  115. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  116. clip-rule="evenodd"
  117. />
  118. </svg>
  119. </div>
  120. <div class=" self-center text-sm font-medium">{$i18n.t('Import Chats')}</div>
  121. </button>
  122. {#if $user?.role === 'admin' || ($user.permissions?.chat?.export ?? true)}
  123. <button
  124. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  125. on:click={() => {
  126. exportChats();
  127. }}
  128. >
  129. <div class=" self-center mr-3">
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. viewBox="0 0 16 16"
  133. fill="currentColor"
  134. class="w-4 h-4"
  135. >
  136. <path
  137. fill-rule="evenodd"
  138. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  139. clip-rule="evenodd"
  140. />
  141. </svg>
  142. </div>
  143. <div class=" self-center text-sm font-medium">{$i18n.t('Export Chats')}</div>
  144. </button>
  145. {/if}
  146. </div>
  147. <hr class=" border-gray-100 dark:border-gray-850" />
  148. <div class="flex flex-col">
  149. <button
  150. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  151. on:click={() => {
  152. showArchivedChatsModal = true;
  153. }}
  154. >
  155. <div class=" self-center mr-3">
  156. <svg
  157. xmlns="http://www.w3.org/2000/svg"
  158. viewBox="0 0 24 24"
  159. fill="currentColor"
  160. class="size-4"
  161. >
  162. <path
  163. d="M3.375 3C2.339 3 1.5 3.84 1.5 4.875v.75c0 1.036.84 1.875 1.875 1.875h17.25c1.035 0 1.875-.84 1.875-1.875v-.75C22.5 3.839 21.66 3 20.625 3H3.375Z"
  164. />
  165. <path
  166. fill-rule="evenodd"
  167. d="m3.087 9 .54 9.176A3 3 0 0 0 6.62 21h10.757a3 3 0 0 0 2.995-2.824L20.913 9H3.087ZM12 10.5a.75.75 0 0 1 .75.75v4.94l1.72-1.72a.75.75 0 1 1 1.06 1.06l-3 3a.75.75 0 0 1-1.06 0l-3-3a.75.75 0 1 1 1.06-1.06l1.72 1.72v-4.94a.75.75 0 0 1 .75-.75Z"
  168. clip-rule="evenodd"
  169. />
  170. </svg>
  171. </div>
  172. <div class=" self-center text-sm font-medium">{$i18n.t('Archived Chats')}</div>
  173. </button>
  174. {#if showArchiveConfirm}
  175. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  176. <div class="flex items-center space-x-3">
  177. <svg
  178. xmlns="http://www.w3.org/2000/svg"
  179. viewBox="0 0 16 16"
  180. fill="currentColor"
  181. class="w-4 h-4"
  182. >
  183. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  184. <path
  185. fill-rule="evenodd"
  186. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM5.72 7.47a.75.75 0 0 1 1.06 0L8 8.69l1.22-1.22a.75.75 0 1 1 1.06 1.06L9.06 9.75l1.22 1.22a.75.75 0 1 1-1.06 1.06L8 10.81l-1.22 1.22a.75.75 0 0 1-1.06-1.06l1.22-1.22-1.22-1.22a.75.75 0 0 1 0-1.06Z"
  187. clip-rule="evenodd"
  188. />
  189. </svg>
  190. <span>{$i18n.t('Are you sure?')}</span>
  191. </div>
  192. <div class="flex space-x-1.5 items-center">
  193. <button
  194. class="hover:text-white transition"
  195. on:click={() => {
  196. archiveAllChatsHandler();
  197. showArchiveConfirm = false;
  198. }}
  199. >
  200. <svg
  201. xmlns="http://www.w3.org/2000/svg"
  202. viewBox="0 0 20 20"
  203. fill="currentColor"
  204. class="w-4 h-4"
  205. >
  206. <path
  207. fill-rule="evenodd"
  208. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  209. clip-rule="evenodd"
  210. />
  211. </svg>
  212. </button>
  213. <button
  214. class="hover:text-white transition"
  215. on:click={() => {
  216. showArchiveConfirm = false;
  217. }}
  218. >
  219. <svg
  220. xmlns="http://www.w3.org/2000/svg"
  221. viewBox="0 0 20 20"
  222. fill="currentColor"
  223. class="w-4 h-4"
  224. >
  225. <path
  226. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  227. />
  228. </svg>
  229. </button>
  230. </div>
  231. </div>
  232. {:else}
  233. <button
  234. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  235. on:click={() => {
  236. showArchiveConfirm = true;
  237. }}
  238. >
  239. <div class=" self-center mr-3">
  240. <svg
  241. xmlns="http://www.w3.org/2000/svg"
  242. viewBox="0 0 24 24"
  243. fill="currentColor"
  244. class="size-4"
  245. >
  246. <path
  247. d="M3.375 3C2.339 3 1.5 3.84 1.5 4.875v.75c0 1.036.84 1.875 1.875 1.875h17.25c1.035 0 1.875-.84 1.875-1.875v-.75C22.5 3.839 21.66 3 20.625 3H3.375Z"
  248. />
  249. <path
  250. fill-rule="evenodd"
  251. d="m3.087 9 .54 9.176A3 3 0 0 0 6.62 21h10.757a3 3 0 0 0 2.995-2.824L20.913 9H3.087Zm6.163 3.75A.75.75 0 0 1 10 12h4a.75.75 0 0 1 0 1.5h-4a.75.75 0 0 1-.75-.75Z"
  252. clip-rule="evenodd"
  253. />
  254. </svg>
  255. </div>
  256. <div class=" self-center text-sm font-medium">{$i18n.t('Archive All Chats')}</div>
  257. </button>
  258. {/if}
  259. {#if showDeleteConfirm}
  260. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  261. <div class="flex items-center space-x-3">
  262. <svg
  263. xmlns="http://www.w3.org/2000/svg"
  264. viewBox="0 0 16 16"
  265. fill="currentColor"
  266. class="w-4 h-4"
  267. >
  268. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  269. <path
  270. fill-rule="evenodd"
  271. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM5.72 7.47a.75.75 0 0 1 1.06 0L8 8.69l1.22-1.22a.75.75 0 1 1 1.06 1.06L9.06 9.75l1.22 1.22a.75.75 0 1 1-1.06 1.06L8 10.81l-1.22 1.22a.75.75 0 0 1-1.06-1.06l1.22-1.22-1.22-1.22a.75.75 0 0 1 0-1.06Z"
  272. clip-rule="evenodd"
  273. />
  274. </svg>
  275. <span>{$i18n.t('Are you sure?')}</span>
  276. </div>
  277. <div class="flex space-x-1.5 items-center">
  278. <button
  279. class="hover:text-white transition"
  280. on:click={() => {
  281. deleteAllChatsHandler();
  282. showDeleteConfirm = false;
  283. }}
  284. >
  285. <svg
  286. xmlns="http://www.w3.org/2000/svg"
  287. viewBox="0 0 20 20"
  288. fill="currentColor"
  289. class="w-4 h-4"
  290. >
  291. <path
  292. fill-rule="evenodd"
  293. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  294. clip-rule="evenodd"
  295. />
  296. </svg>
  297. </button>
  298. <button
  299. class="hover:text-white transition"
  300. on:click={() => {
  301. showDeleteConfirm = false;
  302. }}
  303. >
  304. <svg
  305. xmlns="http://www.w3.org/2000/svg"
  306. viewBox="0 0 20 20"
  307. fill="currentColor"
  308. class="w-4 h-4"
  309. >
  310. <path
  311. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  312. />
  313. </svg>
  314. </button>
  315. </div>
  316. </div>
  317. {:else}
  318. <button
  319. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  320. on:click={() => {
  321. showDeleteConfirm = true;
  322. }}
  323. >
  324. <div class=" self-center mr-3">
  325. <svg
  326. xmlns="http://www.w3.org/2000/svg"
  327. viewBox="0 0 16 16"
  328. fill="currentColor"
  329. class="w-4 h-4"
  330. >
  331. <path
  332. fill-rule="evenodd"
  333. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm7 7a.75.75 0 0 1-.75.75h-4.5a.75.75 0 0 1 0-1.5h4.5A.75.75 0 0 1 11 9Z"
  334. clip-rule="evenodd"
  335. />
  336. </svg>
  337. </div>
  338. <div class=" self-center text-sm font-medium">{$i18n.t('Delete All Chats')}</div>
  339. </button>
  340. {/if}
  341. </div>
  342. </div>
  343. </div>