ChatMenu.svelte 8.1 KB

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