Menu.svelte 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { DropdownMenu } from 'bits-ui';
  4. import { getContext } from 'svelte';
  5. import fileSaver from 'file-saver';
  6. const { saveAs } = fileSaver;
  7. import jsPDF from 'jspdf';
  8. import html2canvas from 'html2canvas-pro';
  9. import { downloadChatAsPDF } from '$lib/apis/utils';
  10. import { copyToClipboard, createMessagesList } from '$lib/utils';
  11. import {
  12. showOverview,
  13. showControls,
  14. showArtifacts,
  15. mobile,
  16. temporaryChatEnabled,
  17. theme
  18. } from '$lib/stores';
  19. import { flyAndScale } from '$lib/utils/transitions';
  20. import Dropdown from '$lib/components/common/Dropdown.svelte';
  21. import Tags from '$lib/components/chat/Tags.svelte';
  22. import Map from '$lib/components/icons/Map.svelte';
  23. import Clipboard from '$lib/components/icons/Clipboard.svelte';
  24. import AdjustmentsHorizontal from '$lib/components/icons/AdjustmentsHorizontal.svelte';
  25. import Cube from '$lib/components/icons/Cube.svelte';
  26. import { getChatById } from '$lib/apis/chats';
  27. const i18n = getContext('i18n');
  28. export let shareEnabled: boolean = false;
  29. export let shareHandler: Function;
  30. export let downloadHandler: Function;
  31. // export let tagHandler: Function;
  32. export let chat;
  33. export let onClose: Function = () => {};
  34. const getChatAsText = async () => {
  35. const history = chat.chat.history;
  36. const messages = createMessagesList(history, history.currentId);
  37. const chatText = messages.reduce((a, message, i, arr) => {
  38. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  39. }, '');
  40. return chatText.trim();
  41. };
  42. const downloadTxt = async () => {
  43. const chatText = await getChatAsText();
  44. let blob = new Blob([chatText], {
  45. type: 'text/plain'
  46. });
  47. saveAs(blob, `chat-${chat.chat.title}.txt`);
  48. };
  49. const downloadPdf = async () => {
  50. const containerElement = document.getElementById('messages-container');
  51. if (containerElement) {
  52. try {
  53. const isDarkMode = $theme.includes('dark'); // Check theme mode
  54. const canvas = await html2canvas(containerElement, {
  55. backgroundColor: isDarkMode ? '#000' : '#fff', // Ensure proper background
  56. scale: 2, // Enhance resolution
  57. height: containerElement.scrollHeight,
  58. windowHeight: containerElement.scrollHeight
  59. });
  60. const imgData = canvas.toDataURL('image/png');
  61. // A4 dimensions
  62. const pdf = new jsPDF('p', 'mm', 'a4');
  63. const imgWidth = 210;
  64. const pageHeight = 297;
  65. const imgHeight = (canvas.height * imgWidth) / canvas.width; // Maintain aspect ratio
  66. let heightLeft = imgHeight;
  67. let position = 0;
  68. // Set page background color for dark mode
  69. if (isDarkMode) {
  70. pdf.setFillColor(0, 0, 0); // Black background for each page
  71. pdf.rect(0, 0, imgWidth, pageHeight, 'F'); // Fill entire page
  72. }
  73. // Add first page
  74. pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
  75. heightLeft -= pageHeight;
  76. // Handle multiple pages
  77. while (heightLeft > 0) {
  78. position -= pageHeight;
  79. pdf.addPage();
  80. if (isDarkMode) {
  81. pdf.setFillColor(0, 0, 0); // Ensure dark background for new pages
  82. pdf.rect(0, 0, imgWidth, pageHeight, 'F');
  83. }
  84. pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
  85. heightLeft -= pageHeight;
  86. }
  87. pdf.save(`chat-${chat.chat.title}.pdf`);
  88. } catch (error) {
  89. console.error('Error generating PDF', error);
  90. }
  91. }
  92. };
  93. const downloadJSONExport = async () => {
  94. if (chat.id) {
  95. let chatObj = null;
  96. if (chat.id === 'local' || $temporaryChatEnabled) {
  97. chatObj = chat;
  98. } else {
  99. chatObj = await getChatById(localStorage.token, chat.id);
  100. }
  101. let blob = new Blob([JSON.stringify([chatObj])], {
  102. type: 'application/json'
  103. });
  104. saveAs(blob, `chat-export-${Date.now()}.json`);
  105. }
  106. };
  107. </script>
  108. <Dropdown
  109. on:change={(e) => {
  110. if (e.detail === false) {
  111. onClose();
  112. }
  113. }}
  114. >
  115. <slot />
  116. <div slot="content">
  117. <DropdownMenu.Content
  118. 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"
  119. sideOffset={8}
  120. side="bottom"
  121. align="end"
  122. transition={flyAndScale}
  123. >
  124. <!-- <DropdownMenu.Item
  125. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer dark:hover:bg-gray-800 rounded-md"
  126. on:click={async () => {
  127. await showSettings.set(!$showSettings);
  128. }}
  129. >
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. fill="none"
  133. viewBox="0 0 24 24"
  134. stroke-width="1.5"
  135. stroke="currentColor"
  136. class="size-4"
  137. >
  138. <path
  139. stroke-linecap="round"
  140. stroke-linejoin="round"
  141. d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z"
  142. />
  143. <path
  144. stroke-linecap="round"
  145. stroke-linejoin="round"
  146. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  147. />
  148. </svg>
  149. <div class="flex items-center">{$i18n.t('Settings')}</div>
  150. </DropdownMenu.Item> -->
  151. {#if $mobile}
  152. <DropdownMenu.Item
  153. 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"
  154. id="chat-controls-button"
  155. on:click={async () => {
  156. await showControls.set(true);
  157. await showOverview.set(false);
  158. await showArtifacts.set(false);
  159. }}
  160. >
  161. <AdjustmentsHorizontal className=" size-4" strokeWidth="0.5" />
  162. <div class="flex items-center">{$i18n.t('Controls')}</div>
  163. </DropdownMenu.Item>
  164. {/if}
  165. {#if !$temporaryChatEnabled}
  166. <DropdownMenu.Item
  167. 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"
  168. id="chat-share-button"
  169. on:click={() => {
  170. shareHandler();
  171. }}
  172. >
  173. <svg
  174. xmlns="http://www.w3.org/2000/svg"
  175. viewBox="0 0 24 24"
  176. fill="currentColor"
  177. class="size-4"
  178. >
  179. <path
  180. fill-rule="evenodd"
  181. d="M15.75 4.5a3 3 0 1 1 .825 2.066l-8.421 4.679a3.002 3.002 0 0 1 0 1.51l8.421 4.679a3 3 0 1 1-.729 1.31l-8.421-4.678a3 3 0 1 1 0-4.132l8.421-4.679a3 3 0 0 1-.096-.755Z"
  182. clip-rule="evenodd"
  183. />
  184. </svg>
  185. <div class="flex items-center">{$i18n.t('Share')}</div>
  186. </DropdownMenu.Item>
  187. {/if}
  188. <DropdownMenu.Item
  189. 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"
  190. id="chat-overview-button"
  191. on:click={async () => {
  192. await showControls.set(true);
  193. await showOverview.set(true);
  194. await showArtifacts.set(false);
  195. }}
  196. >
  197. <Map className=" size-4" strokeWidth="1.5" />
  198. <div class="flex items-center">{$i18n.t('Overview')}</div>
  199. </DropdownMenu.Item>
  200. <DropdownMenu.Item
  201. 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"
  202. id="chat-overview-button"
  203. on:click={async () => {
  204. await showControls.set(true);
  205. await showArtifacts.set(true);
  206. await showOverview.set(false);
  207. }}
  208. >
  209. <Cube className=" size-4" strokeWidth="1.5" />
  210. <div class="flex items-center">{$i18n.t('Artifacts')}</div>
  211. </DropdownMenu.Item>
  212. <DropdownMenu.Sub>
  213. <DropdownMenu.SubTrigger
  214. 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"
  215. >
  216. <svg
  217. xmlns="http://www.w3.org/2000/svg"
  218. fill="none"
  219. viewBox="0 0 24 24"
  220. stroke-width="1.5"
  221. stroke="currentColor"
  222. class="size-4"
  223. >
  224. <path
  225. stroke-linecap="round"
  226. stroke-linejoin="round"
  227. 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"
  228. />
  229. </svg>
  230. <div class="flex items-center">{$i18n.t('Download')}</div>
  231. </DropdownMenu.SubTrigger>
  232. <DropdownMenu.SubContent
  233. class="w-full rounded-xl px-1 py-1.5 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg"
  234. transition={flyAndScale}
  235. sideOffset={8}
  236. >
  237. <DropdownMenu.Item
  238. 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"
  239. on:click={() => {
  240. downloadJSONExport();
  241. }}
  242. >
  243. <div class="flex items-center line-clamp-1">{$i18n.t('Export chat (.json)')}</div>
  244. </DropdownMenu.Item>
  245. <DropdownMenu.Item
  246. 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"
  247. on:click={() => {
  248. downloadTxt();
  249. }}
  250. >
  251. <div class="flex items-center line-clamp-1">{$i18n.t('Plain text (.txt)')}</div>
  252. </DropdownMenu.Item>
  253. <DropdownMenu.Item
  254. 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"
  255. on:click={() => {
  256. downloadPdf();
  257. }}
  258. >
  259. <div class="flex items-center line-clamp-1">{$i18n.t('PDF document (.pdf)')}</div>
  260. </DropdownMenu.Item>
  261. </DropdownMenu.SubContent>
  262. </DropdownMenu.Sub>
  263. <DropdownMenu.Item
  264. 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"
  265. id="chat-copy-button"
  266. on:click={async () => {
  267. const res = await copyToClipboard(await getChatAsText()).catch((e) => {
  268. console.error(e);
  269. });
  270. if (res) {
  271. toast.success($i18n.t('Copied to clipboard'));
  272. }
  273. }}
  274. >
  275. <Clipboard className=" size-4" strokeWidth="1.5" />
  276. <div class="flex items-center">{$i18n.t('Copy')}</div>
  277. </DropdownMenu.Item>
  278. {#if !$temporaryChatEnabled}
  279. <hr class="border-gray-100 dark:border-gray-850 my-0.5" />
  280. <div class="flex p-1">
  281. <Tags chatId={chat.id} />
  282. </div>
  283. {/if}
  284. </DropdownMenu.Content>
  285. </div>
  286. </Dropdown>