ShareChatModal.svelte 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 pt-4 pb-0.5">
  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. {#if chat}
  82. <div class="px-5 pt-4 pb-5 w-full flex flex-col justify-center">
  83. <div class=" text-sm dark:text-gray-300 mb-1">
  84. {#if chat.share_id}
  85. <a href="/s/{chat.share_id}" target="_blank"
  86. >{$i18n.t('You have shared this chat')}
  87. <span class=" underline">{$i18n.t('before')}</span>.</a
  88. >
  89. {$i18n.t('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. }}
  98. >{$i18n.t('delete this link')}
  99. </button>
  100. {$i18n.t('and create a new shared link.')}
  101. {:else}
  102. {$i18n.t(
  103. "Messages you send after creating your link won't be shared. Users with the URL will beable to view the shared chat."
  104. )}
  105. {/if}
  106. </div>
  107. <div class="flex justify-end">
  108. <div class="flex flex-col items-end space-x-1 mt-1.5">
  109. <div class="flex gap-1">
  110. <button
  111. 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"
  112. type="button"
  113. on:click={() => {
  114. shareChat();
  115. show = false;
  116. }}
  117. >
  118. {$i18n.t('Share to OpenWebUI Community')}
  119. </button>
  120. <button
  121. 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"
  122. type="button"
  123. on:click={async () => {
  124. const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  125. if (isSafari) {
  126. // Oh, Safari, you're so special, let's give you some extra love and attention
  127. console.log('isSafari');
  128. const getUrlPromise = async () => {
  129. const url = await shareLocalChat();
  130. return new Blob([url], { type: 'text/plain' });
  131. };
  132. navigator.clipboard
  133. .write([
  134. new ClipboardItem({
  135. 'text/plain': getUrlPromise()
  136. })
  137. ])
  138. .then(() => {
  139. console.log('Async: Copying to clipboard was successful!');
  140. return true;
  141. })
  142. .catch((error) => {
  143. console.error('Async: Could not copy text: ', error);
  144. return false;
  145. });
  146. } else {
  147. copyToClipboard(await shareLocalChat());
  148. }
  149. toast.success($i18n.t('Copied shared chat URL to clipboard!'));
  150. show = false;
  151. }}
  152. >
  153. <Link />
  154. {#if chat.share_id}
  155. {$i18n.t('Update and Copy Link')}
  156. {:else}
  157. {$i18n.t('Copy Link')}
  158. {/if}
  159. </button>
  160. </div>
  161. </div>
  162. </div>
  163. </div>
  164. {/if}
  165. </div>
  166. </Modal>