open-webui/src/lib/components/chat/ShareChatModal.svelte

203 lines
5.3 KiB
Svelte
Raw Normal View History

2024-01-18 05:01:30 +00:00
<script lang="ts">
2024-04-02 13:42:57 +00:00
import { getContext, onMount } from 'svelte';
import { toast } from 'svelte-sonner';
2024-04-02 14:16:25 +00:00
import { deleteSharedChatById, getChatById, shareChatById } from '$lib/apis/chats';
2024-04-20 19:40:06 +00:00
import { modelfiles } from '$lib/stores';
2024-04-02 13:42:57 +00:00
import { copyToClipboard } from '$lib/utils';
2024-01-18 05:01:30 +00:00
import Modal from '../common/Modal.svelte';
2024-04-02 13:12:45 +00:00
import Link from '../icons/Link.svelte';
2024-01-18 05:01:30 +00:00
2024-04-20 19:40:06 +00:00
export let chatId;
2024-04-02 13:42:57 +00:00
let chat = null;
2024-04-19 11:34:55 +00:00
let shareUrl = null;
const i18n = getContext('i18n');
2024-04-02 13:42:57 +00:00
const shareLocalChat = async () => {
const _chat = chat;
2024-04-20 19:40:06 +00:00
const sharedChat = await shareChatById(localStorage.token, chatId);
2024-04-19 11:34:55 +00:00
shareUrl = `${window.location.origin}/s/${sharedChat.id}`;
console.log(shareUrl);
2024-04-20 19:40:06 +00:00
chat = await getChatById(localStorage.token, chatId);
2024-04-19 11:34:55 +00:00
return shareUrl;
2024-04-02 13:42:57 +00:00
};
const shareChat = async () => {
const _chat = chat.chat;
console.log('share', _chat);
toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
const url = 'https://openwebui.com';
// const url = 'http://localhost:5173';
const tab = await window.open(`${url}/chats/upload`, '_blank');
window.addEventListener(
'message',
(event) => {
if (event.origin !== url) return;
if (event.data === 'loaded') {
tab.postMessage(
JSON.stringify({
chat: _chat,
modelfiles: $modelfiles.filter((modelfile) =>
_chat.models.includes(modelfile.tagName)
)
}),
'*'
);
}
},
false
);
};
2024-01-18 05:01:30 +00:00
export let show = false;
2024-04-02 13:42:57 +00:00
const isDifferentChat = (_chat) => {
if (!chat) {
return true;
}
if (!_chat) {
return false;
}
return chat.id !== _chat.id || chat.share_id !== _chat.share_id;
2024-05-17 04:21:19 +00:00
};
2024-04-04 02:45:08 +00:00
$: if (show) {
(async () => {
2024-04-20 19:40:06 +00:00
if (chatId) {
const _chat = await getChatById(localStorage.token, chatId);
if (isDifferentChat(_chat)) {
chat = _chat;
}
2024-04-04 02:45:08 +00:00
} else {
chat = null;
console.log(chat);
}
})();
}
2024-01-18 05:01:30 +00:00
</script>
2024-04-02 13:12:45 +00:00
<Modal bind:show size="sm">
<div>
2024-05-02 00:55:18 +00:00
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-0.5">
2024-04-02 13:12:45 +00:00
<div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
2024-01-18 05:01:30 +00:00
<button
2024-04-02 13:12:45 +00:00
class="self-center"
2024-01-18 05:01:30 +00:00
on:click={() => {
show = false;
}}
>
2024-04-02 13:12:45 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5"
>
<path
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"
/>
</svg>
2024-01-18 05:01:30 +00:00
</button>
</div>
2024-04-02 13:12:45 +00:00
2024-04-02 14:16:25 +00:00
{#if chat}
2024-05-02 00:55:18 +00:00
<div class="px-5 pt-4 pb-5 w-full flex flex-col justify-center">
2024-04-02 14:16:25 +00:00
<div class=" text-sm dark:text-gray-300 mb-1">
{#if chat.share_id}
<a href="/s/{chat.share_id}" target="_blank"
2024-05-06 19:58:24 +00:00
>{$i18n.t('You have shared this chat')}
<span class=" underline">{$i18n.t('before')}</span>.</a
2024-04-02 13:12:45 +00:00
>
2024-05-06 19:58:24 +00:00
{$i18n.t('Click here to')}
2024-04-02 13:12:45 +00:00
<button
2024-04-02 14:16:25 +00:00
class="underline"
on:click={async () => {
2024-04-20 19:40:06 +00:00
const res = await deleteSharedChatById(localStorage.token, chatId);
2024-04-02 14:16:25 +00:00
if (res) {
2024-04-20 19:40:06 +00:00
chat = await getChatById(localStorage.token, chatId);
2024-04-02 14:16:25 +00:00
}
2024-05-06 19:58:24 +00:00
}}
>{$i18n.t('delete this link')}
</button>
{$i18n.t('and create a new shared link.')}
2024-04-02 14:16:25 +00:00
{:else}
2024-05-06 19:58:24 +00:00
{$i18n.t(
2024-05-17 18:17:17 +00:00
"Messages you send after creating your link won't be shared. Users with the URL will be able to view the shared chat."
2024-05-06 19:58:24 +00:00
)}
2024-04-02 14:16:25 +00:00
{/if}
</div>
<div class="flex justify-end">
<div class="flex flex-col items-end space-x-1 mt-1.5">
<div class="flex gap-1">
<button
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"
type="button"
on:click={() => {
shareChat();
show = false;
}}
>
{$i18n.t('Share to OpenWebUI Community')}
</button>
<button
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"
type="button"
id="copy-and-share-chat-button"
2024-04-22 00:28:16 +00:00
on:click={async () => {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (isSafari) {
// Oh, Safari, you're so special, let's give you some extra love and attention
console.log('isSafari');
const getUrlPromise = async () => {
const url = await shareLocalChat();
return new Blob([url], { type: 'text/plain' });
};
navigator.clipboard
.write([
new ClipboardItem({
'text/plain': getUrlPromise()
})
])
.then(() => {
console.log('Async: Copying to clipboard was successful!');
return true;
})
.catch((error) => {
console.error('Async: Could not copy text: ', error);
return false;
});
} else {
copyToClipboard(await shareLocalChat());
}
2024-04-19 11:34:55 +00:00
toast.success($i18n.t('Copied shared chat URL to clipboard!'));
2024-04-02 14:16:25 +00:00
show = false;
}}
>
<Link />
{#if chat.share_id}
{$i18n.t('Update and Copy Link')}
{:else}
{$i18n.t('Copy Link')}
{/if}
</button>
</div>
2024-04-02 13:12:45 +00:00
</div>
</div>
</div>
2024-04-02 14:16:25 +00:00
{/if}
2024-01-18 05:01:30 +00:00
</div>
</Modal>