ArchivedChatsModal.svelte 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <script>
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { toast } from 'svelte-sonner';
  5. import { getContext } from 'svelte';
  6. import { archiveChatById, getAllArchivedChats, getArchivedChatList } from '$lib/apis/chats';
  7. import ChatsModal from './ChatsModal.svelte';
  8. import UnarchiveAllConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  9. const i18n = getContext('i18n');
  10. export let show = false;
  11. export let onUpdate = () => {};
  12. let chatList = null;
  13. let page = 1;
  14. let query = '';
  15. let orderBy = 'updated_at';
  16. let direction = 'desc';
  17. let allChatsLoaded = false;
  18. let chatListLoading = false;
  19. let searchDebounceTimeout;
  20. let showUnarchiveAllConfirmDialog = false;
  21. let filter = {};
  22. $: filter = {
  23. ...(query ? { query } : {}),
  24. ...(orderBy ? { order_by: orderBy } : {}),
  25. ...(direction ? { direction } : {})
  26. };
  27. $: if (filter !== null) {
  28. searchHandler();
  29. }
  30. const searchHandler = async () => {
  31. console.log('search', query);
  32. if (searchDebounceTimeout) {
  33. clearTimeout(searchDebounceTimeout);
  34. }
  35. page = 1;
  36. chatList = null;
  37. if (query === '') {
  38. chatList = await getArchivedChatList(localStorage.token, page, filter);
  39. } else {
  40. searchDebounceTimeout = setTimeout(async () => {
  41. chatList = await getArchivedChatList(localStorage.token, page, filter);
  42. }, 500);
  43. }
  44. if ((chatList ?? []).length === 0) {
  45. allChatsLoaded = true;
  46. } else {
  47. allChatsLoaded = false;
  48. }
  49. };
  50. const loadMoreChats = async () => {
  51. chatListLoading = true;
  52. page += 1;
  53. let newChatList = [];
  54. if (query) {
  55. newChatList = await getArchivedChatList(localStorage.token, page, filter);
  56. } else {
  57. newChatList = await getArchivedChatList(localStorage.token, page, filter);
  58. }
  59. // once the bottom of the list has been reached (no results) there is no need to continue querying
  60. allChatsLoaded = newChatList.length === 0;
  61. if (newChatList.length > 0) {
  62. chatList = [...chatList, ...newChatList];
  63. }
  64. chatListLoading = false;
  65. };
  66. const exportChatsHandler = async () => {
  67. const chats = await getAllArchivedChats(localStorage.token);
  68. let blob = new Blob([JSON.stringify(chats)], {
  69. type: 'application/json'
  70. });
  71. saveAs(blob, `${$i18n.t('archived-chat-export')}-${Date.now()}.json`);
  72. };
  73. const unarchiveHandler = async (chatId) => {
  74. const res = await archiveChatById(localStorage.token, chatId).catch((error) => {
  75. toast.error(`${error}`);
  76. });
  77. onUpdate();
  78. init();
  79. };
  80. const unarchiveAllHandler = async () => {
  81. const chats = await getAllArchivedChats(localStorage.token);
  82. for (const chat of chats) {
  83. await archiveChatById(localStorage.token, chat.id);
  84. }
  85. onUpdate();
  86. init();
  87. };
  88. const init = async () => {
  89. chatList = await getArchivedChatList(localStorage.token);
  90. };
  91. $: if (show) {
  92. init();
  93. }
  94. </script>
  95. <UnarchiveAllConfirmDialog
  96. bind:show={showUnarchiveAllConfirmDialog}
  97. message={$i18n.t('Are you sure you want to unarchive all archived chats?')}
  98. confirmLabel={$i18n.t('Unarchive All')}
  99. on:confirm={() => {
  100. unarchiveAllHandler();
  101. }}
  102. />
  103. <ChatsModal
  104. bind:show
  105. bind:query
  106. bind:orderBy
  107. bind:direction
  108. title={$i18n.t('Archived Chats')}
  109. emptyPlaceholder={$i18n.t('You have no archived conversations.')}
  110. {chatList}
  111. {allChatsLoaded}
  112. {chatListLoading}
  113. onUpdate={() => {
  114. init();
  115. }}
  116. loadHandler={loadMoreChats}
  117. {unarchiveHandler}
  118. >
  119. <div slot="footer">
  120. <div class="flex flex-wrap text-sm font-medium gap-1.5 mt-2 m-1 justify-end w-full">
  121. <button
  122. class=" px-3.5 py-1.5 font-medium hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-gray-100 dark:outline-gray-800 rounded-3xl"
  123. on:click={() => {
  124. showUnarchiveAllConfirmDialog = true;
  125. }}
  126. >
  127. {$i18n.t('Unarchive All Archived Chats')}
  128. </button>
  129. <button
  130. class="px-3.5 py-1.5 font-medium hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-gray-100 dark:outline-gray-800 rounded-3xl"
  131. on:click={() => {
  132. exportChatsHandler();
  133. }}
  134. >
  135. {$i18n.t('Export All Archived Chats')}
  136. </button>
  137. </div>
  138. </div>
  139. </ChatsModal>