Menu.svelte 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 { downloadChatAsPDF } from '$lib/apis/utils';
  8. import { copyToClipboard, createMessagesList } from '$lib/utils';
  9. import {
  10. showOverview,
  11. showControls,
  12. showArtifacts,
  13. mobile,
  14. temporaryChatEnabled
  15. } from '$lib/stores';
  16. import { flyAndScale } from '$lib/utils/transitions';
  17. import Dropdown from '$lib/components/common/Dropdown.svelte';
  18. import Tags from '$lib/components/chat/Tags.svelte';
  19. import Map from '$lib/components/icons/Map.svelte';
  20. import Clipboard from '$lib/components/icons/Clipboard.svelte';
  21. import AdjustmentsHorizontal from '$lib/components/icons/AdjustmentsHorizontal.svelte';
  22. import Cube from '$lib/components/icons/Cube.svelte';
  23. import { getChatById } from '$lib/apis/chats';
  24. const i18n = getContext('i18n');
  25. export let shareEnabled: boolean = false;
  26. export let shareHandler: Function;
  27. export let downloadHandler: Function;
  28. // export let tagHandler: Function;
  29. export let chat;
  30. export let onClose: Function = () => {};
  31. const getChatAsText = async () => {
  32. const history = chat.chat.history;
  33. const messages = createMessagesList(history, history.currentId);
  34. const chatText = messages.reduce((a, message, i, arr) => {
  35. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  36. }, '');
  37. return chatText.trim();
  38. };
  39. const downloadTxt = async () => {
  40. const chatText = await getChatAsText();
  41. let blob = new Blob([chatText], {
  42. type: 'text/plain'
  43. });
  44. saveAs(blob, `chat-${chat.chat.title}.txt`);
  45. };
  46. const downloadPdf = async () => {
  47. const history = chat.chat.history;
  48. const messages = createMessagesList(history, history.currentId);
  49. const blob = await downloadChatAsPDF(chat.chat.title, messages);
  50. // Create a URL for the blob
  51. const url = window.URL.createObjectURL(blob);
  52. // Create a link element to trigger the download
  53. const a = document.createElement('a');
  54. a.href = url;
  55. a.download = `chat-${chat.chat.title}.pdf`;
  56. // Append the link to the body and click it programmatically
  57. document.body.appendChild(a);
  58. a.click();
  59. // Remove the link from the body
  60. document.body.removeChild(a);
  61. // Revoke the URL to release memory
  62. window.URL.revokeObjectURL(url);
  63. };
  64. const downloadJSONExport = async () => {
  65. if (chat.id) {
  66. chat = await getChatById(localStorage.token, chat.id);
  67. }
  68. let blob = new Blob([JSON.stringify([chat])], {
  69. type: 'application/json'
  70. });
  71. saveAs(blob, `chat-export-${Date.now()}.json`);
  72. };
  73. </script>
  74. <Dropdown
  75. on:change={(e) => {
  76. if (e.detail === false) {
  77. onClose();
  78. }
  79. }}
  80. >
  81. <slot />
  82. <div slot="content">
  83. <DropdownMenu.Content
  84. 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"
  85. sideOffset={8}
  86. side="bottom"
  87. align="end"
  88. transition={flyAndScale}
  89. >
  90. <!-- <DropdownMenu.Item
  91. class="flex gap-2 items-center px-3 py-2 text-sm cursor-pointer dark:hover:bg-gray-800 rounded-md"
  92. on:click={async () => {
  93. await showSettings.set(!$showSettings);
  94. }}
  95. >
  96. <svg
  97. xmlns="http://www.w3.org/2000/svg"
  98. fill="none"
  99. viewBox="0 0 24 24"
  100. stroke-width="1.5"
  101. stroke="currentColor"
  102. class="size-4"
  103. >
  104. <path
  105. stroke-linecap="round"
  106. stroke-linejoin="round"
  107. 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"
  108. />
  109. <path
  110. stroke-linecap="round"
  111. stroke-linejoin="round"
  112. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  113. />
  114. </svg>
  115. <div class="flex items-center">{$i18n.t('Settings')}</div>
  116. </DropdownMenu.Item> -->
  117. {#if $mobile}
  118. <DropdownMenu.Item
  119. 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"
  120. id="chat-controls-button"
  121. on:click={async () => {
  122. await showControls.set(true);
  123. await showOverview.set(false);
  124. await showArtifacts.set(false);
  125. }}
  126. >
  127. <AdjustmentsHorizontal className=" size-4" strokeWidth="0.5" />
  128. <div class="flex items-center">{$i18n.t('Controls')}</div>
  129. </DropdownMenu.Item>
  130. {/if}
  131. {#if !$temporaryChatEnabled}
  132. <DropdownMenu.Item
  133. 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"
  134. id="chat-share-button"
  135. on:click={() => {
  136. shareHandler();
  137. }}
  138. >
  139. <svg
  140. xmlns="http://www.w3.org/2000/svg"
  141. viewBox="0 0 24 24"
  142. fill="currentColor"
  143. class="size-4"
  144. >
  145. <path
  146. fill-rule="evenodd"
  147. 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"
  148. clip-rule="evenodd"
  149. />
  150. </svg>
  151. <div class="flex items-center">{$i18n.t('Share')}</div>
  152. </DropdownMenu.Item>
  153. {/if}
  154. <DropdownMenu.Item
  155. 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"
  156. id="chat-overview-button"
  157. on:click={async () => {
  158. await showControls.set(true);
  159. await showOverview.set(true);
  160. await showArtifacts.set(false);
  161. }}
  162. >
  163. <Map className=" size-4" strokeWidth="1.5" />
  164. <div class="flex items-center">{$i18n.t('Overview')}</div>
  165. </DropdownMenu.Item>
  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-overview-button"
  169. on:click={async () => {
  170. await showControls.set(true);
  171. await showArtifacts.set(true);
  172. await showOverview.set(false);
  173. }}
  174. >
  175. <Cube className=" size-4" strokeWidth="1.5" />
  176. <div class="flex items-center">{$i18n.t('Artifacts')}</div>
  177. </DropdownMenu.Item>
  178. <DropdownMenu.Sub>
  179. <DropdownMenu.SubTrigger
  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. >
  182. <svg
  183. xmlns="http://www.w3.org/2000/svg"
  184. fill="none"
  185. viewBox="0 0 24 24"
  186. stroke-width="1.5"
  187. stroke="currentColor"
  188. class="size-4"
  189. >
  190. <path
  191. stroke-linecap="round"
  192. stroke-linejoin="round"
  193. 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"
  194. />
  195. </svg>
  196. <div class="flex items-center">{$i18n.t('Download')}</div>
  197. </DropdownMenu.SubTrigger>
  198. <DropdownMenu.SubContent
  199. class="w-full rounded-xl px-1 py-1.5 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg"
  200. transition={flyAndScale}
  201. sideOffset={8}
  202. >
  203. <DropdownMenu.Item
  204. 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"
  205. on:click={() => {
  206. downloadJSONExport();
  207. }}
  208. >
  209. <div class="flex items-center line-clamp-1">{$i18n.t('Export chat (.json)')}</div>
  210. </DropdownMenu.Item>
  211. <DropdownMenu.Item
  212. 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"
  213. on:click={() => {
  214. downloadTxt();
  215. }}
  216. >
  217. <div class="flex items-center line-clamp-1">{$i18n.t('Plain text (.txt)')}</div>
  218. </DropdownMenu.Item>
  219. <DropdownMenu.Item
  220. 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"
  221. on:click={() => {
  222. downloadPdf();
  223. }}
  224. >
  225. <div class="flex items-center line-clamp-1">{$i18n.t('PDF document (.pdf)')}</div>
  226. </DropdownMenu.Item>
  227. </DropdownMenu.SubContent>
  228. </DropdownMenu.Sub>
  229. <DropdownMenu.Item
  230. 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"
  231. id="chat-copy-button"
  232. on:click={async () => {
  233. const res = await copyToClipboard(await getChatAsText()).catch((e) => {
  234. console.error(e);
  235. });
  236. if (res) {
  237. toast.success($i18n.t('Copied to clipboard'));
  238. }
  239. }}
  240. >
  241. <Clipboard className=" size-4" strokeWidth="1.5" />
  242. <div class="flex items-center">{$i18n.t('Copy')}</div>
  243. </DropdownMenu.Item>
  244. {#if !$temporaryChatEnabled}
  245. <hr class="border-gray-100 dark:border-gray-800 my-1" />
  246. <div class="flex p-1">
  247. <Tags chatId={chat.id} />
  248. </div>
  249. {/if}
  250. </DropdownMenu.Content>
  251. </div>
  252. </Dropdown>