ShareChatModal.svelte 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. const isDifferentChat = (_chat) => {
  49. if (!chat) {
  50. return true;
  51. }
  52. if (!_chat) {
  53. return false;
  54. }
  55. return chat.id !== _chat.id || chat.share_id !== _chat.share_id;
  56. }
  57. $: if (show) {
  58. (async () => {
  59. if (chatId) {
  60. const _chat = await getChatById(localStorage.token, chatId);
  61. if (isDifferentChat(_chat)) {
  62. chat = _chat;
  63. }
  64. } else {
  65. chat = null;
  66. console.log(chat);
  67. }
  68. })();
  69. }
  70. </script>
  71. <Modal bind:show size="sm">
  72. <div>
  73. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-0.5">
  74. <div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
  75. <button
  76. class="self-center"
  77. on:click={() => {
  78. show = false;
  79. }}
  80. >
  81. <svg
  82. xmlns="http://www.w3.org/2000/svg"
  83. viewBox="0 0 20 20"
  84. fill="currentColor"
  85. class="w-5 h-5"
  86. >
  87. <path
  88. 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"
  89. />
  90. </svg>
  91. </button>
  92. </div>
  93. {#if chat}
  94. <div class="px-5 pt-4 pb-5 w-full flex flex-col justify-center">
  95. <div class=" text-sm dark:text-gray-300 mb-1">
  96. {#if chat.share_id}
  97. <a href="/s/{chat.share_id}" target="_blank"
  98. >{$i18n.t('You have shared this chat')}
  99. <span class=" underline">{$i18n.t('before')}</span>.</a
  100. >
  101. {$i18n.t('Click here to')}
  102. <button
  103. class="underline"
  104. on:click={async () => {
  105. const res = await deleteSharedChatById(localStorage.token, chatId);
  106. if (res) {
  107. chat = await getChatById(localStorage.token, chatId);
  108. }
  109. }}
  110. >{$i18n.t('delete this link')}
  111. </button>
  112. {$i18n.t('and create a new shared link.')}
  113. {:else}
  114. {$i18n.t(
  115. "Messages you send after creating your link won't be shared. Users with the URL will beable to view the shared chat."
  116. )}
  117. {/if}
  118. </div>
  119. <div class="flex justify-end">
  120. <div class="flex flex-col items-end space-x-1 mt-1.5">
  121. <div class="flex gap-1">
  122. <button
  123. 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"
  124. type="button"
  125. on:click={() => {
  126. shareChat();
  127. show = false;
  128. }}
  129. >
  130. {$i18n.t('Share to OpenWebUI Community')}
  131. </button>
  132. <button
  133. 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"
  134. type="button"
  135. id="copy-and-share-chat-button"
  136. on:click={async () => {
  137. const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  138. if (isSafari) {
  139. // Oh, Safari, you're so special, let's give you some extra love and attention
  140. console.log('isSafari');
  141. const getUrlPromise = async () => {
  142. const url = await shareLocalChat();
  143. return new Blob([url], { type: 'text/plain' });
  144. };
  145. navigator.clipboard
  146. .write([
  147. new ClipboardItem({
  148. 'text/plain': getUrlPromise()
  149. })
  150. ])
  151. .then(() => {
  152. console.log('Async: Copying to clipboard was successful!');
  153. return true;
  154. })
  155. .catch((error) => {
  156. console.error('Async: Could not copy text: ', error);
  157. return false;
  158. });
  159. } else {
  160. copyToClipboard(await shareLocalChat());
  161. }
  162. toast.success($i18n.t('Copied shared chat URL to clipboard!'));
  163. show = false;
  164. }}
  165. >
  166. <Link />
  167. {#if chat.share_id}
  168. {$i18n.t('Update and Copy Link')}
  169. {:else}
  170. {$i18n.t('Copy Link')}
  171. {/if}
  172. </button>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. {/if}
  178. </div>
  179. </Modal>