ArchivedChatsModal.svelte 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { getContext, createEventDispatcher } from 'svelte';
  5. const dispatch = createEventDispatcher();
  6. import Modal from '$lib/components/common/Modal.svelte';
  7. import { archiveChatById, deleteChatById, getArchivedChatList } from '$lib/apis/chats';
  8. import Tooltip from '$lib/components/common/Tooltip.svelte';
  9. const i18n = getContext('i18n');
  10. export let show = false;
  11. let chats = [];
  12. const unarchiveChatHandler = async (chatId) => {
  13. const res = await archiveChatById(localStorage.token, chatId).catch((error) => {
  14. toast.error(error);
  15. });
  16. chats = await getArchivedChatList(localStorage.token);
  17. dispatch('change');
  18. };
  19. const deleteChatHandler = async (chatId) => {
  20. const res = await deleteChatById(localStorage.token, chatId).catch((error) => {
  21. toast.error(error);
  22. });
  23. chats = await getArchivedChatList(localStorage.token);
  24. };
  25. $: if (show) {
  26. (async () => {
  27. chats = await getArchivedChatList(localStorage.token);
  28. })();
  29. }
  30. </script>
  31. <Modal size="lg" bind:show>
  32. <div>
  33. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  34. <div class=" text-lg font-medium self-center">{$i18n.t('Archived Chats')}</div>
  35. <button
  36. class="self-center"
  37. on:click={() => {
  38. show = false;
  39. }}
  40. >
  41. <svg
  42. xmlns="http://www.w3.org/2000/svg"
  43. viewBox="0 0 20 20"
  44. fill="currentColor"
  45. class="w-5 h-5"
  46. >
  47. <path
  48. 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"
  49. />
  50. </svg>
  51. </button>
  52. </div>
  53. <hr class=" dark:border-gray-850" />
  54. <div class="flex flex-col md:flex-row w-full px-5 py-4 md:space-x-4 dark:text-gray-200">
  55. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  56. {#if chats.length > 0}
  57. <div class="text-left text-sm w-full mb-4 h-[22rem] overflow-y-scroll">
  58. <div class="relative overflow-x-auto">
  59. <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400 table-auto">
  60. <thead
  61. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 border-gray-800"
  62. >
  63. <tr>
  64. <th scope="col" class="px-3 py-2"> {$i18n.t('Name')} </th>
  65. <th scope="col" class="px-3 py-2 hidden md:flex"> {$i18n.t('Created At')} </th>
  66. <th scope="col" class="px-3 py-2 text-right" />
  67. </tr>
  68. </thead>
  69. <tbody>
  70. {#each [...chats, ...chats, ...chats] as chat, idx}
  71. <tr
  72. class="bg-white {idx !== chats.length - 1 &&
  73. 'border-b'} dark:bg-gray-900 dark:border-gray-850 text-xs"
  74. >
  75. <td class="px-3 py-1 w-2/3">
  76. <a href="/c/{chat.id}" target="_blank">
  77. <div class=" underline line-clamp-1">
  78. {chat.title}
  79. </div>
  80. </a>
  81. </td>
  82. <td class=" px-3 py-1 hidden md:flex h-[2.5rem]">
  83. <div class="my-auto">
  84. {dayjs(chat.created_at * 1000).format($i18n.t('MMMM DD, YYYY HH:mm'))}
  85. </div>
  86. </td>
  87. <td class="px-3 py-1 text-right">
  88. <div class="flex justify-end w-full">
  89. <Tooltip content="Unarchive Chat">
  90. <button
  91. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  92. on:click={async () => {
  93. unarchiveChatHandler(chat.id);
  94. }}
  95. >
  96. <svg
  97. xmlns="http://www.w3.org/2000/svg"
  98. fill="none"
  99. viewBox="0 0 24 24"
  100. stroke-width="1.5"
  101. stroke="currentColor"
  102. class="size-4"
  103. >
  104. <path
  105. stroke-linecap="round"
  106. stroke-linejoin="round"
  107. 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"
  108. />
  109. </svg>
  110. </button>
  111. </Tooltip>
  112. <Tooltip content="Delete Chat">
  113. <button
  114. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  115. on:click={async () => {
  116. deleteChatHandler(chat.id);
  117. }}
  118. >
  119. <svg
  120. xmlns="http://www.w3.org/2000/svg"
  121. fill="none"
  122. viewBox="0 0 24 24"
  123. stroke-width="1.5"
  124. stroke="currentColor"
  125. class="w-4 h-4"
  126. >
  127. <path
  128. stroke-linecap="round"
  129. stroke-linejoin="round"
  130. 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"
  131. />
  132. </svg>
  133. </button>
  134. </Tooltip>
  135. </div>
  136. </td>
  137. </tr>
  138. {/each}
  139. </tbody>
  140. </table>
  141. </div>
  142. <!-- {#each chats as chat}
  143. <div>
  144. {JSON.stringify(chat)}
  145. </div>
  146. {/each} -->
  147. </div>
  148. {:else}
  149. <div class="text-left text-sm w-full mb-8">You have no archived conversations.</div>
  150. {/if}
  151. </div>
  152. </div>
  153. </div>
  154. </Modal>