ShareChatModal.svelte 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <script lang="ts">
  2. import { getContext, onMount } from 'svelte';
  3. import { toast } from 'svelte-sonner';
  4. import { deleteSharedChatById, getChatById, shareChatById } from '$lib/apis/chats';
  5. import { modelfiles } from '$lib/stores';
  6. import { copyToClipboard } from '$lib/utils';
  7. import Modal from '../common/Modal.svelte';
  8. import Link from '../icons/Link.svelte';
  9. export let chatId;
  10. let chat = null;
  11. let shareUrl = null;
  12. const i18n = getContext('i18n');
  13. const shareLocalChat = async () => {
  14. const _chat = chat;
  15. const sharedChat = await shareChatById(localStorage.token, chatId);
  16. shareUrl = `${window.location.origin}/s/${sharedChat.id}`;
  17. console.log(shareUrl);
  18. chat = await getChatById(localStorage.token, chatId);
  19. return shareUrl;
  20. };
  21. const shareChat = async () => {
  22. const _chat = chat.chat;
  23. console.log('share', _chat);
  24. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  25. const url = 'https://openwebui.com';
  26. // const url = 'http://localhost:5173';
  27. const tab = await window.open(`${url}/chats/upload`, '_blank');
  28. window.addEventListener(
  29. 'message',
  30. (event) => {
  31. if (event.origin !== url) return;
  32. if (event.data === 'loaded') {
  33. tab.postMessage(
  34. JSON.stringify({
  35. chat: _chat,
  36. modelfiles: $modelfiles.filter((modelfile) =>
  37. _chat.models.includes(modelfile.tagName)
  38. )
  39. }),
  40. '*'
  41. );
  42. }
  43. },
  44. false
  45. );
  46. };
  47. export let show = false;
  48. $: if (show) {
  49. (async () => {
  50. if (chatId) {
  51. chat = await getChatById(localStorage.token, chatId);
  52. } else {
  53. chat = null;
  54. console.log(chat);
  55. }
  56. })();
  57. }
  58. </script>
  59. <Modal bind:show size="sm">
  60. <div>
  61. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  62. <div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
  63. <button
  64. class="self-center"
  65. on:click={() => {
  66. show = false;
  67. }}
  68. >
  69. <svg
  70. xmlns="http://www.w3.org/2000/svg"
  71. viewBox="0 0 20 20"
  72. fill="currentColor"
  73. class="w-5 h-5"
  74. >
  75. <path
  76. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  77. />
  78. </svg>
  79. </button>
  80. </div>
  81. <hr class=" dark:border-gray-800" />
  82. {#if chat}
  83. <div class="px-4 pt-4 pb-5 w-full flex flex-col justify-center">
  84. <div class=" text-sm dark:text-gray-300 mb-1">
  85. {#if chat.share_id}
  86. <a href="/s/{chat.share_id}" target="_blank"
  87. >You have shared this chat <span class=" underline">before</span>.</a
  88. >
  89. Click here to
  90. <button
  91. class="underline"
  92. on:click={async () => {
  93. const res = await deleteSharedChatById(localStorage.token, chatId);
  94. if (res) {
  95. chat = await getChatById(localStorage.token, chatId);
  96. }
  97. }}>delete this link</button
  98. > and create a new shared link.
  99. {:else}
  100. Messages you send after creating your link won't be shared. Users with the URL will be
  101. able to view the shared chat.
  102. {/if}
  103. </div>
  104. <div class="flex justify-end">
  105. <div class="flex flex-col items-end space-x-1 mt-1.5">
  106. <div class="flex gap-1">
  107. <button
  108. class=" self-center px-3.5 py-2 rounded-xl text-sm font-medium bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-white"
  109. type="button"
  110. on:click={() => {
  111. shareChat();
  112. show = false;
  113. }}
  114. >
  115. {$i18n.t('Share to OpenWebUI Community')}
  116. </button>
  117. <button
  118. class=" self-center flex items-center gap-1 px-3.5 py-2 rounded-xl text-sm font-medium bg-emerald-600 hover:bg-emerald-500 text-white"
  119. type="button"
  120. on:click={async () => {
  121. const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  122. if (isSafari) {
  123. // Oh, Safari, you're so special, let's give you some extra love and attention
  124. console.log('isSafari');
  125. const getUrlPromise = async () => {
  126. const url = await shareLocalChat();
  127. return new Blob([url], { type: 'text/plain' });
  128. };
  129. navigator.clipboard
  130. .write([
  131. new ClipboardItem({
  132. 'text/plain': getUrlPromise()
  133. })
  134. ])
  135. .then(() => {
  136. console.log('Async: Copying to clipboard was successful!');
  137. return true;
  138. })
  139. .catch((error) => {
  140. console.error('Async: Could not copy text: ', error);
  141. return false;
  142. });
  143. } else {
  144. copyToClipboard(await shareLocalChat());
  145. }
  146. toast.success($i18n.t('Copied shared chat URL to clipboard!'));
  147. show = false;
  148. }}
  149. >
  150. <Link />
  151. {#if chat.share_id}
  152. {$i18n.t('Update and Copy Link')}
  153. {:else}
  154. {$i18n.t('Copy Link')}
  155. {/if}
  156. </button>
  157. </div>
  158. </div>
  159. </div>
  160. </div>
  161. {/if}
  162. </div>
  163. </Modal>