Chats.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { chats, user, settings, scrollPaginationEnabled, pageSkip, pageLimit } from '$lib/stores';
  5. import {
  6. archiveAllChats,
  7. createNewChat,
  8. deleteAllChats,
  9. getAllChats,
  10. getAllUserChats,
  11. getChatList
  12. } from '$lib/apis/chats';
  13. import {
  14. getImportOrigin,
  15. convertOpenAIChats,
  16. disablePagination,
  17. enablePagination
  18. } from '$lib/utils';
  19. import { onMount, getContext } from 'svelte';
  20. import { goto } from '$app/navigation';
  21. import { toast } from 'svelte-sonner';
  22. const i18n = getContext('i18n');
  23. export let saveSettings: Function;
  24. // Chats
  25. let saveChatHistory = true;
  26. let importFiles;
  27. let showArchiveConfirm = false;
  28. let showDeleteConfirm = false;
  29. let chatImportInputElement: HTMLInputElement;
  30. $: if (importFiles) {
  31. console.log(importFiles);
  32. let reader = new FileReader();
  33. reader.onload = (event) => {
  34. let chats = JSON.parse(event.target.result);
  35. console.log(chats);
  36. if (getImportOrigin(chats) == 'openai') {
  37. try {
  38. chats = convertOpenAIChats(chats);
  39. } catch (error) {
  40. console.log('Unable to import chats:', error);
  41. }
  42. }
  43. importChats(chats);
  44. };
  45. if (importFiles.length > 0) {
  46. reader.readAsText(importFiles[0]);
  47. }
  48. }
  49. const importChats = async (_chats) => {
  50. for (const chat of _chats) {
  51. console.log(chat);
  52. if (chat.chat) {
  53. await createNewChat(localStorage.token, chat.chat);
  54. } else {
  55. await createNewChat(localStorage.token, chat);
  56. }
  57. }
  58. enablePagination();
  59. };
  60. const exportChats = async () => {
  61. let blob = new Blob([JSON.stringify(await getAllChats(localStorage.token))], {
  62. type: 'application/json'
  63. });
  64. saveAs(blob, `chat-export-${Date.now()}.json`);
  65. };
  66. const archiveAllChatsHandler = async () => {
  67. await goto('/');
  68. await archiveAllChats(localStorage.token).catch((error) => {
  69. toast.error(error);
  70. });
  71. enablePagination();
  72. };
  73. const deleteAllChatsHandler = async () => {
  74. await goto('/');
  75. await deleteAllChats(localStorage.token).catch((error) => {
  76. toast.error(error);
  77. });
  78. enablePagination();
  79. };
  80. const toggleSaveChatHistory = async () => {
  81. saveChatHistory = !saveChatHistory;
  82. console.log(saveChatHistory);
  83. if (saveChatHistory === false) {
  84. await goto('/');
  85. }
  86. saveSettings({ saveChatHistory: saveChatHistory });
  87. };
  88. onMount(async () => {
  89. saveChatHistory = $settings.saveChatHistory ?? true;
  90. });
  91. </script>
  92. <div class="flex flex-col h-full justify-between space-y-3 text-sm max-h-[22rem]">
  93. <div class=" space-y-2">
  94. <div
  95. class="flex flex-col justify-between rounded-md items-center py-2 px-3.5 w-full transition"
  96. >
  97. <div class="flex w-full justify-between">
  98. <div class=" self-center text-sm font-medium">{$i18n.t('Chat History')}</div>
  99. <button
  100. class="p-1 px-3 text-xs flex rounded transition"
  101. type="button"
  102. on:click={() => {
  103. toggleSaveChatHistory();
  104. }}
  105. >
  106. {#if saveChatHistory === true}
  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 d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  114. <path
  115. fill-rule="evenodd"
  116. d="M1.38 8.28a.87.87 0 0 1 0-.566 7.003 7.003 0 0 1 13.238.006.87.87 0 0 1 0 .566A7.003 7.003 0 0 1 1.379 8.28ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  117. clip-rule="evenodd"
  118. />
  119. </svg>
  120. <span class="ml-2 self-center"> {$i18n.t('On')} </span>
  121. {:else}
  122. <svg
  123. xmlns="http://www.w3.org/2000/svg"
  124. viewBox="0 0 16 16"
  125. fill="currentColor"
  126. class="w-4 h-4"
  127. >
  128. <path
  129. fill-rule="evenodd"
  130. d="M3.28 2.22a.75.75 0 0 0-1.06 1.06l10.5 10.5a.75.75 0 1 0 1.06-1.06l-1.322-1.323a7.012 7.012 0 0 0 2.16-3.11.87.87 0 0 0 0-.567A7.003 7.003 0 0 0 4.82 3.76l-1.54-1.54Zm3.196 3.195 1.135 1.136A1.502 1.502 0 0 1 9.45 8.389l1.136 1.135a3 3 0 0 0-4.109-4.109Z"
  131. clip-rule="evenodd"
  132. />
  133. <path
  134. d="m7.812 10.994 1.816 1.816A7.003 7.003 0 0 1 1.38 8.28a.87.87 0 0 1 0-.566 6.985 6.985 0 0 1 1.113-2.039l2.513 2.513a3 3 0 0 0 2.806 2.806Z"
  135. />
  136. </svg>
  137. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  138. {/if}
  139. </button>
  140. </div>
  141. <div class="text-xs text-left w-full font-medium mt-0.5">
  142. {$i18n.t('This setting does not sync across browsers or devices.')}
  143. </div>
  144. </div>
  145. <hr class=" dark:border-gray-850" />
  146. <div class="flex flex-col">
  147. <input
  148. id="chat-import-input"
  149. bind:this={chatImportInputElement}
  150. bind:files={importFiles}
  151. type="file"
  152. accept=".json"
  153. hidden
  154. />
  155. <button
  156. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  157. on:click={() => {
  158. chatImportInputElement.click();
  159. }}
  160. >
  161. <div class=" self-center mr-3">
  162. <svg
  163. xmlns="http://www.w3.org/2000/svg"
  164. viewBox="0 0 16 16"
  165. fill="currentColor"
  166. class="w-4 h-4"
  167. >
  168. <path
  169. fill-rule="evenodd"
  170. 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"
  171. clip-rule="evenodd"
  172. />
  173. </svg>
  174. </div>
  175. <div class=" self-center text-sm font-medium">{$i18n.t('Import Chats')}</div>
  176. </button>
  177. <button
  178. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  179. on:click={() => {
  180. exportChats();
  181. }}
  182. >
  183. <div class=" self-center mr-3">
  184. <svg
  185. xmlns="http://www.w3.org/2000/svg"
  186. viewBox="0 0 16 16"
  187. fill="currentColor"
  188. class="w-4 h-4"
  189. >
  190. <path
  191. fill-rule="evenodd"
  192. 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"
  193. clip-rule="evenodd"
  194. />
  195. </svg>
  196. </div>
  197. <div class=" self-center text-sm font-medium">{$i18n.t('Export Chats')}</div>
  198. </button>
  199. </div>
  200. <hr class=" dark:border-gray-850" />
  201. <div class="flex flex-col">
  202. {#if showArchiveConfirm}
  203. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  204. <div class="flex items-center space-x-3">
  205. <svg
  206. xmlns="http://www.w3.org/2000/svg"
  207. viewBox="0 0 16 16"
  208. fill="currentColor"
  209. class="w-4 h-4"
  210. >
  211. <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" />
  212. <path
  213. fill-rule="evenodd"
  214. 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"
  215. clip-rule="evenodd"
  216. />
  217. </svg>
  218. <span>{$i18n.t('Are you sure?')}</span>
  219. </div>
  220. <div class="flex space-x-1.5 items-center">
  221. <button
  222. class="hover:text-white transition"
  223. on:click={() => {
  224. archiveAllChatsHandler();
  225. showArchiveConfirm = false;
  226. }}
  227. >
  228. <svg
  229. xmlns="http://www.w3.org/2000/svg"
  230. viewBox="0 0 20 20"
  231. fill="currentColor"
  232. class="w-4 h-4"
  233. >
  234. <path
  235. fill-rule="evenodd"
  236. 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"
  237. clip-rule="evenodd"
  238. />
  239. </svg>
  240. </button>
  241. <button
  242. class="hover:text-white transition"
  243. on:click={() => {
  244. showArchiveConfirm = false;
  245. }}
  246. >
  247. <svg
  248. xmlns="http://www.w3.org/2000/svg"
  249. viewBox="0 0 20 20"
  250. fill="currentColor"
  251. class="w-4 h-4"
  252. >
  253. <path
  254. 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"
  255. />
  256. </svg>
  257. </button>
  258. </div>
  259. </div>
  260. {:else}
  261. <button
  262. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  263. on:click={() => {
  264. showArchiveConfirm = true;
  265. }}
  266. >
  267. <div class=" self-center mr-3">
  268. <svg
  269. xmlns="http://www.w3.org/2000/svg"
  270. viewBox="0 0 24 24"
  271. fill="currentColor"
  272. class="size-4"
  273. >
  274. <path
  275. 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"
  276. />
  277. <path
  278. fill-rule="evenodd"
  279. 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"
  280. clip-rule="evenodd"
  281. />
  282. </svg>
  283. </div>
  284. <div class=" self-center text-sm font-medium">{$i18n.t('Archive All Chats')}</div>
  285. </button>
  286. {/if}
  287. {#if showDeleteConfirm}
  288. <div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
  289. <div class="flex items-center space-x-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 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" />
  297. <path
  298. fill-rule="evenodd"
  299. 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"
  300. clip-rule="evenodd"
  301. />
  302. </svg>
  303. <span>{$i18n.t('Are you sure?')}</span>
  304. </div>
  305. <div class="flex space-x-1.5 items-center">
  306. <button
  307. class="hover:text-white transition"
  308. on:click={() => {
  309. deleteAllChatsHandler();
  310. showDeleteConfirm = false;
  311. }}
  312. >
  313. <svg
  314. xmlns="http://www.w3.org/2000/svg"
  315. viewBox="0 0 20 20"
  316. fill="currentColor"
  317. class="w-4 h-4"
  318. >
  319. <path
  320. fill-rule="evenodd"
  321. 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"
  322. clip-rule="evenodd"
  323. />
  324. </svg>
  325. </button>
  326. <button
  327. class="hover:text-white transition"
  328. on:click={() => {
  329. showDeleteConfirm = false;
  330. }}
  331. >
  332. <svg
  333. xmlns="http://www.w3.org/2000/svg"
  334. viewBox="0 0 20 20"
  335. fill="currentColor"
  336. class="w-4 h-4"
  337. >
  338. <path
  339. 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"
  340. />
  341. </svg>
  342. </button>
  343. </div>
  344. </div>
  345. {:else}
  346. <button
  347. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  348. on:click={() => {
  349. showDeleteConfirm = true;
  350. }}
  351. >
  352. <div class=" self-center mr-3">
  353. <svg
  354. xmlns="http://www.w3.org/2000/svg"
  355. viewBox="0 0 16 16"
  356. fill="currentColor"
  357. class="w-4 h-4"
  358. >
  359. <path
  360. fill-rule="evenodd"
  361. 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"
  362. clip-rule="evenodd"
  363. />
  364. </svg>
  365. </div>
  366. <div class=" self-center text-sm font-medium">{$i18n.t('Delete All Chats')}</div>
  367. </button>
  368. {/if}
  369. </div>
  370. </div>
  371. </div>