Chats.svelte 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. const i18n = getContext('i18n');
  18. export let saveSettings: Function;
  19. // Chats
  20. let importFiles;
  21. let showArchiveConfirm = false;
  22. let showDeleteConfirm = false;
  23. let chatImportInputElement: HTMLInputElement;
  24. $: if (importFiles) {
  25. console.log(importFiles);
  26. let reader = new FileReader();
  27. reader.onload = (event) => {
  28. let chats = JSON.parse(event.target.result);
  29. console.log(chats);
  30. if (getImportOrigin(chats) == 'openai') {
  31. try {
  32. chats = convertOpenAIChats(chats);
  33. } catch (error) {
  34. console.log('Unable to import chats:', error);
  35. }
  36. }
  37. importChats(chats);
  38. };
  39. if (importFiles.length > 0) {
  40. reader.readAsText(importFiles[0]);
  41. }
  42. }
  43. const importChats = async (_chats) => {
  44. for (const chat of _chats) {
  45. console.log(chat);
  46. if (chat.chat) {
  47. await createNewChat(localStorage.token, chat.chat);
  48. } else {
  49. await createNewChat(localStorage.token, chat);
  50. }
  51. }
  52. currentChatPage.set(1);
  53. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  54. scrollPaginationEnabled.set(true);
  55. };
  56. const exportChats = async () => {
  57. let blob = new Blob([JSON.stringify(await getAllChats(localStorage.token))], {
  58. type: 'application/json'
  59. });
  60. saveAs(blob, `chat-export-${Date.now()}.json`);
  61. };
  62. const archiveAllChatsHandler = async () => {
  63. await goto('/');
  64. await archiveAllChats(localStorage.token).catch((error) => {
  65. toast.error(`${error}`);
  66. });
  67. currentChatPage.set(1);
  68. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  69. scrollPaginationEnabled.set(true);
  70. };
  71. const deleteAllChatsHandler = async () => {
  72. await goto('/');
  73. await deleteAllChats(localStorage.token).catch((error) => {
  74. toast.error(`${error}`);
  75. });
  76. currentChatPage.set(1);
  77. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  78. scrollPaginationEnabled.set(true);
  79. };
  80. </script>
  81. <div class="flex flex-col h-full justify-between space-y-3 text-sm">
  82. <div class=" space-y-2 overflow-y-scroll max-h-[28rem] lg:max-h-full">
  83. <div class="flex flex-col">
  84. <input
  85. id="chat-import-input"
  86. bind:this={chatImportInputElement}
  87. bind:files={importFiles}
  88. type="file"
  89. accept=".json"
  90. hidden
  91. />
  92. <button
  93. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  94. on:click={() => {
  95. chatImportInputElement.click();
  96. }}
  97. >
  98. <div class=" self-center mr-3">
  99. <svg
  100. xmlns="http://www.w3.org/2000/svg"
  101. viewBox="0 0 16 16"
  102. fill="currentColor"
  103. class="w-4 h-4"
  104. >
  105. <path
  106. fill-rule="evenodd"
  107. 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"
  108. clip-rule="evenodd"
  109. />
  110. </svg>
  111. </div>
  112. <div class=" self-center text-sm font-medium">{$i18n.t('Import Chats')}</div>
  113. </button>
  114. <button
  115. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  116. on:click={() => {
  117. exportChats();
  118. }}
  119. >
  120. <div class=" self-center mr-3">
  121. <svg
  122. xmlns="http://www.w3.org/2000/svg"
  123. viewBox="0 0 16 16"
  124. fill="currentColor"
  125. class="w-4 h-4"
  126. >
  127. <path
  128. fill-rule="evenodd"
  129. 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"
  130. clip-rule="evenodd"
  131. />
  132. </svg>
  133. </div>
  134. <div class=" self-center text-sm font-medium">{$i18n.t('Export Chats')}</div>
  135. </button>
  136. </div>
  137. <hr class=" border-gray-100 dark:border-gray-850" />
  138. <div class="flex flex-col">
  139. {#if showArchiveConfirm}
  140. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  141. <div class="flex items-center space-x-3">
  142. <svg
  143. xmlns="http://www.w3.org/2000/svg"
  144. viewBox="0 0 16 16"
  145. fill="currentColor"
  146. class="w-4 h-4"
  147. >
  148. <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" />
  149. <path
  150. fill-rule="evenodd"
  151. 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"
  152. clip-rule="evenodd"
  153. />
  154. </svg>
  155. <span>{$i18n.t('Are you sure?')}</span>
  156. </div>
  157. <div class="flex space-x-1.5 items-center">
  158. <button
  159. class="hover:text-white transition"
  160. on:click={() => {
  161. archiveAllChatsHandler();
  162. showArchiveConfirm = false;
  163. }}
  164. >
  165. <svg
  166. xmlns="http://www.w3.org/2000/svg"
  167. viewBox="0 0 20 20"
  168. fill="currentColor"
  169. class="w-4 h-4"
  170. >
  171. <path
  172. fill-rule="evenodd"
  173. 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"
  174. clip-rule="evenodd"
  175. />
  176. </svg>
  177. </button>
  178. <button
  179. class="hover:text-white transition"
  180. on:click={() => {
  181. showArchiveConfirm = false;
  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. 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"
  192. />
  193. </svg>
  194. </button>
  195. </div>
  196. </div>
  197. {:else}
  198. <button
  199. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  200. on:click={() => {
  201. showArchiveConfirm = true;
  202. }}
  203. >
  204. <div class=" self-center mr-3">
  205. <svg
  206. xmlns="http://www.w3.org/2000/svg"
  207. viewBox="0 0 24 24"
  208. fill="currentColor"
  209. class="size-4"
  210. >
  211. <path
  212. 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"
  213. />
  214. <path
  215. fill-rule="evenodd"
  216. 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"
  217. clip-rule="evenodd"
  218. />
  219. </svg>
  220. </div>
  221. <div class=" self-center text-sm font-medium">{$i18n.t('Archive All Chats')}</div>
  222. </button>
  223. {/if}
  224. {#if showDeleteConfirm}
  225. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  226. <div class="flex items-center space-x-3">
  227. <svg
  228. xmlns="http://www.w3.org/2000/svg"
  229. viewBox="0 0 16 16"
  230. fill="currentColor"
  231. class="w-4 h-4"
  232. >
  233. <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" />
  234. <path
  235. fill-rule="evenodd"
  236. 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"
  237. clip-rule="evenodd"
  238. />
  239. </svg>
  240. <span>{$i18n.t('Are you sure?')}</span>
  241. </div>
  242. <div class="flex space-x-1.5 items-center">
  243. <button
  244. class="hover:text-white transition"
  245. on:click={() => {
  246. deleteAllChatsHandler();
  247. showDeleteConfirm = false;
  248. }}
  249. >
  250. <svg
  251. xmlns="http://www.w3.org/2000/svg"
  252. viewBox="0 0 20 20"
  253. fill="currentColor"
  254. class="w-4 h-4"
  255. >
  256. <path
  257. fill-rule="evenodd"
  258. 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"
  259. clip-rule="evenodd"
  260. />
  261. </svg>
  262. </button>
  263. <button
  264. class="hover:text-white transition"
  265. on:click={() => {
  266. showDeleteConfirm = false;
  267. }}
  268. >
  269. <svg
  270. xmlns="http://www.w3.org/2000/svg"
  271. viewBox="0 0 20 20"
  272. fill="currentColor"
  273. class="w-4 h-4"
  274. >
  275. <path
  276. 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"
  277. />
  278. </svg>
  279. </button>
  280. </div>
  281. </div>
  282. {:else}
  283. <button
  284. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  285. on:click={() => {
  286. showDeleteConfirm = true;
  287. }}
  288. >
  289. <div class=" self-center mr-3">
  290. <svg
  291. xmlns="http://www.w3.org/2000/svg"
  292. viewBox="0 0 16 16"
  293. fill="currentColor"
  294. class="w-4 h-4"
  295. >
  296. <path
  297. fill-rule="evenodd"
  298. 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"
  299. clip-rule="evenodd"
  300. />
  301. </svg>
  302. </div>
  303. <div class=" self-center text-sm font-medium">{$i18n.t('Delete All Chats')}</div>
  304. </button>
  305. {/if}
  306. </div>
  307. </div>
  308. </div>