Chats.svelte 12 KB

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