ShareChatModal.svelte 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 { chatId, 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. let chat = null;
  10. const i18n = getContext('i18n');
  11. const shareLocalChat = async () => {
  12. const _chat = chat;
  13. const sharedChat = await shareChatById(localStorage.token, $chatId);
  14. const chatShareUrl = `${window.location.origin}/s/${sharedChat.id}`;
  15. toast.success($i18n.t('Copied shared chat URL to clipboard!'));
  16. copyToClipboard(chatShareUrl);
  17. chat = await getChatById(localStorage.token, $chatId);
  18. };
  19. const shareChat = async () => {
  20. const _chat = chat.chat;
  21. console.log('share', _chat);
  22. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  23. const url = 'https://openwebui.com';
  24. // const url = 'http://localhost:5173';
  25. const tab = await window.open(`${url}/chats/upload`, '_blank');
  26. window.addEventListener(
  27. 'message',
  28. (event) => {
  29. if (event.origin !== url) return;
  30. if (event.data === 'loaded') {
  31. tab.postMessage(
  32. JSON.stringify({
  33. chat: _chat,
  34. modelfiles: $modelfiles.filter((modelfile) =>
  35. _chat.models.includes(modelfile.tagName)
  36. )
  37. }),
  38. '*'
  39. );
  40. }
  41. },
  42. false
  43. );
  44. };
  45. export let show = false;
  46. $: if (show) {
  47. (async () => {
  48. if ($chatId) {
  49. chat = await getChatById(localStorage.token, $chatId);
  50. } else {
  51. chat = null;
  52. console.log(chat);
  53. }
  54. })();
  55. }
  56. </script>
  57. <Modal bind:show size="sm">
  58. <div>
  59. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  60. <div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
  61. <button
  62. class="self-center"
  63. on:click={() => {
  64. show = false;
  65. }}
  66. >
  67. <svg
  68. xmlns="http://www.w3.org/2000/svg"
  69. viewBox="0 0 20 20"
  70. fill="currentColor"
  71. class="w-5 h-5"
  72. >
  73. <path
  74. 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"
  75. />
  76. </svg>
  77. </button>
  78. </div>
  79. <hr class=" dark:border-gray-800" />
  80. {#if chat}
  81. <div class="px-4 pt-4 pb-5 w-full flex flex-col justify-center">
  82. <div class=" text-sm dark:text-gray-300 mb-1">
  83. {#if chat.share_id}
  84. <a href="/s/{chat.share_id}" target="_blank"
  85. >You have shared this chat <span class=" underline">before</span>.</a
  86. >
  87. Click here to
  88. <button
  89. class="underline"
  90. on:click={async () => {
  91. const res = await deleteSharedChatById(localStorage.token, $chatId);
  92. if (res) {
  93. chat = await getChatById(localStorage.token, $chatId);
  94. }
  95. }}>delete this link</button
  96. > and create a new shared link.
  97. {:else}
  98. Messages you send after creating your link won't be shared. Users with the URL will be
  99. able to view the shared chat.
  100. {/if}
  101. </div>
  102. <div class="flex justify-end">
  103. <div class="flex flex-col items-end space-x-1 mt-1.5">
  104. <div class="flex gap-1">
  105. <button
  106. 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"
  107. type="button"
  108. on:click={() => {
  109. shareChat();
  110. show = false;
  111. }}
  112. >
  113. {$i18n.t('Share to OpenWebUI Community')}
  114. </button>
  115. <button
  116. 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"
  117. type="button"
  118. on:click={() => {
  119. shareLocalChat();
  120. show = false;
  121. }}
  122. >
  123. <Link />
  124. {#if chat.share_id}
  125. {$i18n.t('Update and Copy Link')}
  126. {:else}
  127. {$i18n.t('Copy Link')}
  128. {/if}
  129. </button>
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. {/if}
  135. </div>
  136. </Modal>