UserChatsModal.svelte 6.3 KB

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