ArchivedChatsModal.svelte 4.3 KB

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