UserChatsModal.svelte 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { getContext, createEventDispatcher } from 'svelte';
  5. import localizedFormat from 'dayjs/plugin/localizedFormat';
  6. const dispatch = createEventDispatcher();
  7. dayjs.extend(localizedFormat);
  8. import { getChatListByUserId, deleteChatById, getArchivedChatList } from '$lib/apis/chats';
  9. import Modal from '$lib/components/common/Modal.svelte';
  10. import Tooltip from '$lib/components/common/Tooltip.svelte';
  11. import Spinner from '$lib/components/common/Spinner.svelte';
  12. const i18n = getContext('i18n');
  13. export let show = false;
  14. export let user;
  15. let chats = null;
  16. const deleteChatHandler = async (chatId) => {
  17. const res = await deleteChatById(localStorage.token, chatId).catch((error) => {
  18. toast.error(`${error}`);
  19. });
  20. chats = await getChatListByUserId(localStorage.token, user.id);
  21. };
  22. $: if (show) {
  23. (async () => {
  24. if (user.id) {
  25. chats = await getChatListByUserId(localStorage.token, user.id);
  26. }
  27. })();
  28. } else {
  29. chats = null;
  30. }
  31. let sortKey = 'updated_at'; // default sort key
  32. let sortOrder = 'desc'; // default sort order
  33. function setSortKey(key) {
  34. if (sortKey === key) {
  35. sortOrder = sortOrder === 'asc' ? 'desc' : 'asc';
  36. } else {
  37. sortKey = key;
  38. sortOrder = 'asc';
  39. }
  40. }
  41. </script>
  42. <Modal size="lg" bind:show>
  43. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4">
  44. <div class=" text-lg font-medium self-center capitalize">
  45. {$i18n.t("{{user}}'s Chats", { user: user.name })}
  46. </div>
  47. <button
  48. class="self-center"
  49. on:click={() => {
  50. show = false;
  51. }}
  52. >
  53. <svg
  54. xmlns="http://www.w3.org/2000/svg"
  55. viewBox="0 0 20 20"
  56. fill="currentColor"
  57. class="w-5 h-5"
  58. >
  59. <path
  60. 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"
  61. />
  62. </svg>
  63. </button>
  64. </div>
  65. <div class="flex flex-col md:flex-row w-full px-5 pt-2 pb-4 md:space-x-4 dark:text-gray-200">
  66. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  67. {#if chats}
  68. {#if chats.length > 0}
  69. <div class="text-left text-sm w-full mb-4 max-h-[22rem] overflow-y-scroll">
  70. <div class="relative overflow-x-auto">
  71. <table class="w-full text-sm text-left text-gray-600 dark:text-gray-400 table-auto">
  72. <thead
  73. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 dark:border-gray-800"
  74. >
  75. <tr>
  76. <th
  77. scope="col"
  78. class="px-3 py-2 cursor-pointer select-none"
  79. on:click={() => setSortKey('title')}
  80. >
  81. {$i18n.t('Title')}
  82. {#if sortKey === 'title'}
  83. {sortOrder === 'asc' ? '▲' : '▼'}
  84. {:else}
  85. <span class="invisible">▲</span>
  86. {/if}
  87. </th>
  88. <th
  89. scope="col"
  90. class="px-3 py-2 hidden md:flex cursor-pointer select-none justify-end"
  91. on:click={() => setSortKey('updated_at')}
  92. >
  93. {$i18n.t('Updated at')}
  94. {#if sortKey === 'updated_at'}
  95. {sortOrder === 'asc' ? '▲' : '▼'}
  96. {:else}
  97. <span class="invisible">▲</span>
  98. {/if}
  99. </th>
  100. <th scope="col" class="px-3 py-2 text-right" />
  101. </tr>
  102. </thead>
  103. <tbody>
  104. {#each chats.sort((a, b) => {
  105. if (a[sortKey] < b[sortKey]) return sortOrder === 'asc' ? -1 : 1;
  106. if (a[sortKey] > b[sortKey]) return sortOrder === 'asc' ? 1 : -1;
  107. return 0;
  108. }) as chat, idx}
  109. <tr
  110. class="bg-transparent {idx !== chats.length - 1 &&
  111. 'border-b'} dark:bg-gray-900 dark:border-gray-850 text-xs"
  112. >
  113. <td class="px-3 py-1">
  114. <a href="/s/{chat.id}" target="_blank">
  115. <div class=" underline line-clamp-1 max-w-96">
  116. {chat.title}
  117. </div>
  118. </a>
  119. </td>
  120. <td class=" px-3 py-1 hidden md:flex h-[2.5rem] justify-end">
  121. <div class="my-auto shrink-0">
  122. {dayjs(chat.updated_at * 1000).format('LLL')}
  123. </div>
  124. </td>
  125. <td class="px-3 py-1 text-right">
  126. <div class="flex justify-end w-full">
  127. <Tooltip content={$i18n.t('Delete Chat')}>
  128. <button
  129. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  130. on:click={async () => {
  131. deleteChatHandler(chat.id);
  132. }}
  133. >
  134. <svg
  135. xmlns="http://www.w3.org/2000/svg"
  136. fill="none"
  137. viewBox="0 0 24 24"
  138. stroke-width="1.5"
  139. stroke="currentColor"
  140. class="w-4 h-4"
  141. >
  142. <path
  143. stroke-linecap="round"
  144. stroke-linejoin="round"
  145. 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"
  146. />
  147. </svg>
  148. </button>
  149. </Tooltip>
  150. </div>
  151. </td>
  152. </tr>
  153. {/each}
  154. </tbody>
  155. </table>
  156. </div>
  157. <!-- {#each chats as chat}
  158. <div>
  159. {JSON.stringify(chat)}
  160. </div>
  161. {/each} -->
  162. </div>
  163. {:else}
  164. <div class="text-left text-sm w-full mb-8">
  165. {user.name}
  166. {$i18n.t('has no conversations.')}
  167. </div>
  168. {/if}
  169. {:else}
  170. <Spinner />
  171. {/if}
  172. </div>
  173. </div>
  174. </Modal>