UserChatsModal.svelte 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. </script>
  27. <Modal size="lg" bind:show>
  28. <div>
  29. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  30. <div class=" text-lg font-medium self-center capitalize">
  31. {$i18n.t("{{user}}'s Chats", { user: user.name })}
  32. </div>
  33. <button
  34. class="self-center"
  35. on:click={() => {
  36. show = false;
  37. }}
  38. >
  39. <svg
  40. xmlns="http://www.w3.org/2000/svg"
  41. viewBox="0 0 20 20"
  42. fill="currentColor"
  43. class="w-5 h-5"
  44. >
  45. <path
  46. 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"
  47. />
  48. </svg>
  49. </button>
  50. </div>
  51. <hr class=" dark:border-gray-850" />
  52. <div class="flex flex-col md:flex-row w-full px-5 py-4 md:space-x-4 dark:text-gray-200">
  53. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  54. {#if chats.length > 0}
  55. <div class="text-left text-sm w-full mb-4 max-h-[22rem] overflow-y-scroll">
  56. <div class="relative overflow-x-auto">
  57. <table class="w-full text-sm text-left text-gray-600 dark:text-gray-400 table-auto">
  58. <thead
  59. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 dark:border-gray-800"
  60. >
  61. <tr>
  62. <th scope="col" class="px-3 py-2"> {$i18n.t('Name')} </th>
  63. <th scope="col" class="px-3 py-2 hidden md:flex"> {$i18n.t('Created at')} </th>
  64. <th scope="col" class="px-3 py-2 text-right" />
  65. </tr>
  66. </thead>
  67. <tbody>
  68. {#each chats as chat, idx}
  69. <tr
  70. class="bg-transparent {idx !== chats.length - 1 &&
  71. 'border-b'} dark:bg-gray-900 dark:border-gray-850 text-xs"
  72. >
  73. <td class="px-3 py-1 w-2/3">
  74. <a href="/s/{chat.id}" target="_blank">
  75. <div class=" underline line-clamp-1">
  76. {chat.title}
  77. </div>
  78. </a>
  79. </td>
  80. <td class=" px-3 py-1 hidden md:flex h-[2.5rem]">
  81. <div class="my-auto">
  82. {dayjs(chat.created_at * 1000).format($i18n.t('MMMM DD, YYYY HH:mm'))}
  83. </div>
  84. </td>
  85. <td class="px-3 py-1 text-right">
  86. <div class="flex justify-end w-full">
  87. <Tooltip content={$i18n.t('Delete Chat')}>
  88. <button
  89. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  90. on:click={async () => {
  91. deleteChatHandler(chat.id);
  92. }}
  93. >
  94. <svg
  95. xmlns="http://www.w3.org/2000/svg"
  96. fill="none"
  97. viewBox="0 0 24 24"
  98. stroke-width="1.5"
  99. stroke="currentColor"
  100. class="w-4 h-4"
  101. >
  102. <path
  103. stroke-linecap="round"
  104. stroke-linejoin="round"
  105. 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"
  106. />
  107. </svg>
  108. </button>
  109. </Tooltip>
  110. </div>
  111. </td>
  112. </tr>
  113. {/each}
  114. </tbody>
  115. </table>
  116. </div>
  117. <!-- {#each chats as chat}
  118. <div>
  119. {JSON.stringify(chat)}
  120. </div>
  121. {/each} -->
  122. </div>
  123. {:else}
  124. <div class="text-left text-sm w-full mb-8">{user.name} {$i18n.t('has no conversations.')}</div>
  125. {/if}
  126. </div>
  127. </div>
  128. </div>
  129. </Modal>