ChatMenu.svelte 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { flyAndScale } from '$lib/utils/transitions';
  4. import { getContext, createEventDispatcher } from 'svelte';
  5. import fileSaver from 'file-saver';
  6. const { saveAs } = fileSaver;
  7. const dispatch = createEventDispatcher();
  8. import Dropdown from '$lib/components/common/Dropdown.svelte';
  9. import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
  10. import Pencil from '$lib/components/icons/Pencil.svelte';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. import Tags from '$lib/components/chat/Tags.svelte';
  13. import Share from '$lib/components/icons/Share.svelte';
  14. import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
  15. import DocumentDuplicate from '$lib/components/icons/DocumentDuplicate.svelte';
  16. import Bookmark from '$lib/components/icons/Bookmark.svelte';
  17. import BookmarkSlash from '$lib/components/icons/BookmarkSlash.svelte';
  18. import {
  19. getChatById,
  20. getChatPinnedStatusById,
  21. toggleChatPinnedStatusById
  22. } from '$lib/apis/chats';
  23. import { chats } from '$lib/stores';
  24. import { createMessagesList } from '$lib/utils';
  25. import { downloadChatAsPDF } from '$lib/apis/utils';
  26. import Download from '$lib/components/icons/Download.svelte';
  27. const i18n = getContext('i18n');
  28. export let shareHandler: Function;
  29. export let cloneChatHandler: Function;
  30. export let archiveChatHandler: Function;
  31. export let renameHandler: Function;
  32. export let deleteHandler: Function;
  33. export let onClose: Function;
  34. export let chatId = '';
  35. let show = false;
  36. let pinned = false;
  37. const pinHandler = async () => {
  38. await toggleChatPinnedStatusById(localStorage.token, chatId);
  39. dispatch('change');
  40. };
  41. const checkPinned = async () => {
  42. pinned = await getChatPinnedStatusById(localStorage.token, chatId);
  43. };
  44. const getChatAsText = async (chat) => {
  45. const history = chat.chat.history;
  46. const messages = createMessagesList(history, history.currentId);
  47. const chatText = messages.reduce((a, message, i, arr) => {
  48. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  49. }, '');
  50. return chatText.trim();
  51. };
  52. const downloadTxt = async () => {
  53. const chat = await getChatById(localStorage.token, chatId);
  54. if (!chat) {
  55. return;
  56. }
  57. const chatText = await getChatAsText(chat);
  58. let blob = new Blob([chatText], {
  59. type: 'text/plain'
  60. });
  61. saveAs(blob, `chat-${chat.chat.title}.txt`);
  62. };
  63. const downloadPdf = async () => {
  64. const chat = await getChatById(localStorage.token, chatId);
  65. if (!chat) {
  66. return;
  67. }
  68. const history = chat.chat.history;
  69. const messages = createMessagesList(history, history.currentId);
  70. const blob = await downloadChatAsPDF(chat.chat.title, messages);
  71. // Create a URL for the blob
  72. const url = window.URL.createObjectURL(blob);
  73. // Create a link element to trigger the download
  74. const a = document.createElement('a');
  75. a.href = url;
  76. a.download = `chat-${chat.chat.title}.pdf`;
  77. // Append the link to the body and click it programmatically
  78. document.body.appendChild(a);
  79. a.click();
  80. // Remove the link from the body
  81. document.body.removeChild(a);
  82. // Revoke the URL to release memory
  83. window.URL.revokeObjectURL(url);
  84. };
  85. const downloadJSONExport = async () => {
  86. const chat = await getChatById(localStorage.token, chatId);
  87. if (chat) {
  88. let blob = new Blob([JSON.stringify([chat])], {
  89. type: 'application/json'
  90. });
  91. saveAs(blob, `chat-export-${Date.now()}.json`);
  92. }
  93. };
  94. $: if (show) {
  95. checkPinned();
  96. }
  97. </script>
  98. <Dropdown
  99. bind:show
  100. on:change={(e) => {
  101. if (e.detail === false) {
  102. onClose();
  103. }
  104. }}
  105. >
  106. <Tooltip content={$i18n.t('More')}>
  107. <slot />
  108. </Tooltip>
  109. <div slot="content">
  110. <DropdownMenu.Content
  111. class="w-full max-w-[200px] rounded-xl px-1 py-1.5 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg"
  112. sideOffset={-2}
  113. side="bottom"
  114. align="start"
  115. transition={flyAndScale}
  116. >
  117. <DropdownMenu.Item
  118. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  119. on:click={() => {
  120. pinHandler();
  121. }}
  122. >
  123. {#if pinned}
  124. <BookmarkSlash strokeWidth="2" />
  125. <div class="flex items-center">{$i18n.t('Unpin')}</div>
  126. {:else}
  127. <Bookmark strokeWidth="2" />
  128. <div class="flex items-center">{$i18n.t('Pin')}</div>
  129. {/if}
  130. </DropdownMenu.Item>
  131. <DropdownMenu.Item
  132. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  133. on:click={() => {
  134. renameHandler();
  135. }}
  136. >
  137. <Pencil strokeWidth="2" />
  138. <div class="flex items-center">{$i18n.t('Rename')}</div>
  139. </DropdownMenu.Item>
  140. <DropdownMenu.Item
  141. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  142. on:click={() => {
  143. cloneChatHandler();
  144. }}
  145. >
  146. <DocumentDuplicate strokeWidth="2" />
  147. <div class="flex items-center">{$i18n.t('Clone')}</div>
  148. </DropdownMenu.Item>
  149. <DropdownMenu.Item
  150. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  151. on:click={() => {
  152. archiveChatHandler();
  153. }}
  154. >
  155. <ArchiveBox strokeWidth="2" />
  156. <div class="flex items-center">{$i18n.t('Archive')}</div>
  157. </DropdownMenu.Item>
  158. <DropdownMenu.Item
  159. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  160. on:click={() => {
  161. shareHandler();
  162. }}
  163. >
  164. <Share />
  165. <div class="flex items-center">{$i18n.t('Share')}</div>
  166. </DropdownMenu.Item>
  167. <DropdownMenu.Sub>
  168. <DropdownMenu.SubTrigger
  169. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  170. >
  171. <Download strokeWidth="2" />
  172. <div class="flex items-center">{$i18n.t('Download')}</div>
  173. </DropdownMenu.SubTrigger>
  174. <DropdownMenu.SubContent
  175. class="w-full rounded-xl px-1 py-1.5 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg"
  176. transition={flyAndScale}
  177. sideOffset={8}
  178. >
  179. <DropdownMenu.Item
  180. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  181. on:click={() => {
  182. downloadJSONExport();
  183. }}
  184. >
  185. <div class="flex items-center line-clamp-1">{$i18n.t('Export chat (.json)')}</div>
  186. </DropdownMenu.Item>
  187. <DropdownMenu.Item
  188. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  189. on:click={() => {
  190. downloadTxt();
  191. }}
  192. >
  193. <div class="flex items-center line-clamp-1">{$i18n.t('Plain text (.txt)')}</div>
  194. </DropdownMenu.Item>
  195. <DropdownMenu.Item
  196. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  197. on:click={() => {
  198. downloadPdf();
  199. }}
  200. >
  201. <div class="flex items-center line-clamp-1">{$i18n.t('PDF document (.pdf)')}</div>
  202. </DropdownMenu.Item>
  203. </DropdownMenu.SubContent>
  204. </DropdownMenu.Sub>
  205. <DropdownMenu.Item
  206. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  207. on:click={() => {
  208. deleteHandler();
  209. }}
  210. >
  211. <GarbageBin strokeWidth="2" />
  212. <div class="flex items-center">{$i18n.t('Delete')}</div>
  213. </DropdownMenu.Item>
  214. <hr class="border-gray-100 dark:border-gray-850 my-0.5" />
  215. <div class="flex p-1">
  216. <Tags
  217. {chatId}
  218. on:add={(e) => {
  219. dispatch('tag', {
  220. type: 'add',
  221. name: e.detail.name
  222. });
  223. show = false;
  224. }}
  225. on:delete={(e) => {
  226. dispatch('tag', {
  227. type: 'delete',
  228. name: e.detail.name
  229. });
  230. show = false;
  231. }}
  232. on:close={() => {
  233. show = false;
  234. onClose();
  235. }}
  236. />
  237. </div>
  238. </DropdownMenu.Content>
  239. </div>
  240. </Dropdown>