ArchivedChatsModal.svelte 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { toast } from 'svelte-sonner';
  5. import dayjs from 'dayjs';
  6. import { getContext, createEventDispatcher } from 'svelte';
  7. const dispatch = createEventDispatcher();
  8. import Modal from '$lib/components/common/Modal.svelte';
  9. import {
  10. archiveChatById,
  11. deleteChatById,
  12. getAllArchivedChats,
  13. getArchivedChatList
  14. } from '$lib/apis/chats';
  15. import Tooltip from '$lib/components/common/Tooltip.svelte';
  16. const i18n = getContext('i18n');
  17. export let show = false;
  18. let searchValue = '';
  19. let chats = [];
  20. const unarchiveChatHandler = async (chatId) => {
  21. const res = await archiveChatById(localStorage.token, chatId).catch((error) => {
  22. toast.error(error);
  23. });
  24. chats = await getArchivedChatList(localStorage.token);
  25. dispatch('change');
  26. };
  27. const deleteChatHandler = async (chatId) => {
  28. const res = await deleteChatById(localStorage.token, chatId).catch((error) => {
  29. toast.error(error);
  30. });
  31. chats = await getArchivedChatList(localStorage.token);
  32. };
  33. const exportChatsHandler = async () => {
  34. const chats = await getAllArchivedChats(localStorage.token);
  35. let blob = new Blob([JSON.stringify(chats)], {
  36. type: 'application/json'
  37. });
  38. saveAs(blob, `archived-chat-export-${Date.now()}.json`);
  39. };
  40. $: if (show) {
  41. (async () => {
  42. chats = await getArchivedChatList(localStorage.token);
  43. })();
  44. }
  45. </script>
  46. <Modal size="lg" bind:show>
  47. <div>
  48. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-1">
  49. <div class=" text-lg font-medium self-center">{$i18n.t('Archived Chats')}</div>
  50. <button
  51. class="self-center"
  52. on:click={() => {
  53. show = false;
  54. }}
  55. >
  56. <svg
  57. xmlns="http://www.w3.org/2000/svg"
  58. viewBox="0 0 20 20"
  59. fill="currentColor"
  60. class="w-5 h-5"
  61. >
  62. <path
  63. 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"
  64. />
  65. </svg>
  66. </button>
  67. </div>
  68. <div class="flex flex-col w-full px-5 pb-4 dark:text-gray-200">
  69. <div class=" flex w-full mt-2 space-x-2">
  70. <div class="flex flex-1">
  71. <div class=" self-center ml-1 mr-3">
  72. <svg
  73. xmlns="http://www.w3.org/2000/svg"
  74. viewBox="0 0 20 20"
  75. fill="currentColor"
  76. class="w-4 h-4"
  77. >
  78. <path
  79. fill-rule="evenodd"
  80. d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
  81. clip-rule="evenodd"
  82. />
  83. </svg>
  84. </div>
  85. <input
  86. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  87. bind:value={searchValue}
  88. placeholder={$i18n.t('Search Chats')}
  89. />
  90. </div>
  91. </div>
  92. <hr class=" dark:border-gray-850 my-2" />
  93. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  94. {#if chats.length > 0}
  95. <div class="w-full">
  96. <div class="text-left text-sm w-full mb-3 max-h-[22rem] overflow-y-scroll">
  97. <div class="relative overflow-x-auto">
  98. <table class="w-full text-sm text-left text-gray-600 dark:text-gray-400 table-auto">
  99. <thead
  100. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 dark:border-gray-800"
  101. >
  102. <tr>
  103. <th scope="col" class="px-3 py-2"> {$i18n.t('Name')} </th>
  104. <th scope="col" class="px-3 py-2 hidden md:flex">
  105. {$i18n.t('Created At')}
  106. </th>
  107. <th scope="col" class="px-3 py-2 text-right" />
  108. </tr>
  109. </thead>
  110. <tbody>
  111. {#each chats.filter((c) => searchValue === '' || c.title
  112. .toLowerCase()
  113. .includes(searchValue.toLowerCase())) as chat, idx}
  114. <tr
  115. class="bg-transparent {idx !== chats.length - 1 &&
  116. 'border-b'} dark:bg-gray-900 dark:border-gray-850 text-xs"
  117. >
  118. <td class="px-3 py-1 w-2/3">
  119. <a href="/c/{chat.id}" target="_blank">
  120. <div class=" underline line-clamp-1">
  121. {chat.title}
  122. </div>
  123. </a>
  124. </td>
  125. <td class=" px-3 py-1 hidden md:flex h-[2.5rem]">
  126. <div class="my-auto">
  127. {dayjs(chat.created_at * 1000).format($i18n.t('MMMM DD, YYYY HH:mm'))}
  128. </div>
  129. </td>
  130. <td class="px-3 py-1 text-right">
  131. <div class="flex justify-end w-full">
  132. <Tooltip content="Unarchive Chat">
  133. <button
  134. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  135. on:click={async () => {
  136. unarchiveChatHandler(chat.id);
  137. }}
  138. >
  139. <svg
  140. xmlns="http://www.w3.org/2000/svg"
  141. fill="none"
  142. viewBox="0 0 24 24"
  143. stroke-width="1.5"
  144. stroke="currentColor"
  145. class="size-4"
  146. >
  147. <path
  148. stroke-linecap="round"
  149. stroke-linejoin="round"
  150. d="M9 8.25H7.5a2.25 2.25 0 0 0-2.25 2.25v9a2.25 2.25 0 0 0 2.25 2.25h9a2.25 2.25 0 0 0 2.25-2.25v-9a2.25 2.25 0 0 0-2.25-2.25H15m0-3-3-3m0 0-3 3m3-3V15"
  151. />
  152. </svg>
  153. </button>
  154. </Tooltip>
  155. <Tooltip content="Delete Chat">
  156. <button
  157. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  158. on:click={async () => {
  159. deleteChatHandler(chat.id);
  160. }}
  161. >
  162. <svg
  163. xmlns="http://www.w3.org/2000/svg"
  164. fill="none"
  165. viewBox="0 0 24 24"
  166. stroke-width="1.5"
  167. stroke="currentColor"
  168. class="w-4 h-4"
  169. >
  170. <path
  171. stroke-linecap="round"
  172. stroke-linejoin="round"
  173. 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"
  174. />
  175. </svg>
  176. </button>
  177. </Tooltip>
  178. </div>
  179. </td>
  180. </tr>
  181. {/each}
  182. </tbody>
  183. </table>
  184. </div>
  185. </div>
  186. <div class="flex flex-wrap text-sm font-medium gap-1.5 mt-2 m-1">
  187. <button
  188. class=" px-3.5 py-1.5 font-medium hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-gray-300 dark:outline-gray-800 rounded-3xl"
  189. on:click={() => {
  190. exportChatsHandler();
  191. }}>Export All Archived Chats</button
  192. >
  193. </div>
  194. </div>
  195. {:else}
  196. <div class="text-left text-sm w-full mb-8">
  197. {$i18n.t('You have no archived conversations.')}
  198. </div>
  199. {/if}
  200. </div>
  201. </div>
  202. </div>
  203. </Modal>