UserChatsModal.svelte 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $: {
  37. console.log(chats);
  38. }
  39. </script>
  40. <Modal size="lg" bind:show>
  41. <div>
  42. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  43. <div class=" text-lg font-medium self-center capitalize">
  44. {$i18n.t("{{user}}'s Chats", { user: user.name })}
  45. </div>
  46. <button
  47. class="self-center"
  48. on:click={() => {
  49. show = false;
  50. }}
  51. >
  52. <svg
  53. xmlns="http://www.w3.org/2000/svg"
  54. viewBox="0 0 20 20"
  55. fill="currentColor"
  56. class="w-5 h-5"
  57. >
  58. <path
  59. 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"
  60. />
  61. </svg>
  62. </button>
  63. </div>
  64. <hr class=" dark:border-gray-850" />
  65. <div class="flex flex-col md:flex-row w-full px-5 py-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.length > 0}
  68. <div class="text-left text-sm w-full mb-4 max-h-[22rem] overflow-y-scroll">
  69. <div class="relative overflow-x-auto">
  70. <table class="w-full text-sm text-left text-gray-600 dark:text-gray-400 table-auto">
  71. <thead
  72. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 dark:border-gray-800"
  73. >
  74. <tr>
  75. <th scope="col" class="px-3 py-2 cursor-pointer select-none" on:click={() => setSortKey('title')}>
  76. {$i18n.t('Name')}
  77. {#if sortKey === 'title'}
  78. {sortOrder === 'asc' ? '▲' : '▼'}
  79. {:else}
  80. <span style="visibility:hidden">▲</span>
  81. {/if}
  82. </th>
  83. <th scope="col" class="px-3 py-2 cursor-pointer select-none" on:click={() => setSortKey('created_at')}>
  84. {$i18n.t('Created at')}
  85. {#if sortKey === 'created_at'}
  86. {sortOrder === 'asc' ? '▲' : '▼'}
  87. {:else}
  88. <span style="visibility:hidden">▲</span>
  89. {/if}
  90. </th>
  91. <th scope="col" class="px-3 py-2 hidden md:flex cursor-pointer select-none" on:click={() => setSortKey('updated_at')}>
  92. {$i18n.t('Updated at')}
  93. {#if sortKey === 'updated_at'}
  94. {sortOrder === 'asc' ? '▲' : '▼'}
  95. {:else}
  96. <span style="visibility:hidden">▲</span>
  97. {/if}
  98. </th>
  99. <th scope="col" class="px-3 py-2 text-right" />
  100. </tr>
  101. </thead>
  102. <tbody>
  103. {#each chats
  104. .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">
  116. {chat.title}
  117. </div>
  118. </a>
  119. </td>
  120. <td class=" px-3 py-1 h-[2.5rem]">
  121. <div class="my-auto">
  122. {dayjs(chat.created_at * 1000).format($i18n.t('MMMM DD, YYYY HH:mm'))}
  123. </div>
  124. </td>
  125. <td class=" px-3 py-1 hidden md:flex h-[2.5rem]">
  126. <div class="my-auto">
  127. {dayjs(chat.updated_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={$i18n.t('Delete 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. deleteChatHandler(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="w-4 h-4"
  146. >
  147. <path
  148. stroke-linecap="round"
  149. stroke-linejoin="round"
  150. 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"
  151. />
  152. </svg>
  153. </button>
  154. </Tooltip>
  155. </div>
  156. </td>
  157. </tr>
  158. {/each}
  159. </tbody>
  160. </table>
  161. </div>
  162. <!-- {#each chats as chat}
  163. <div>
  164. {JSON.stringify(chat)}
  165. </div>
  166. {/each} -->
  167. </div>
  168. {:else}
  169. <div class="text-left text-sm w-full mb-8">
  170. {user.name}
  171. {$i18n.t('has no conversations.')}
  172. </div>
  173. {/if}
  174. </div>
  175. </div>
  176. </div>
  177. </Modal>