ShareChatModal.svelte 5.27 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
	import { getContext, onMount } from 'svelte';
3
	import { models } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
4
5

	import { toast } from 'svelte-sonner';
6
	import { deleteSharedChatById, getChatById, shareChatById } from '$lib/apis/chats';
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
	import { copyToClipboard } from '$lib/utils';

Timothy J. Baek's avatar
Timothy J. Baek committed
9
	import Modal from '../common/Modal.svelte';
10
	import Link from '../icons/Link.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
11

Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
	export let chatId;

Timothy J. Baek's avatar
Timothy J. Baek committed
14
	let chat = null;
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	let shareUrl = null;
16
17
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
	const shareLocalChat = async () => {
		const _chat = chat;

Timothy J. Baek's avatar
Timothy J. Baek committed
21
		const sharedChat = await shareChatById(localStorage.token, chatId);
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
		shareUrl = `${window.location.origin}/s/${sharedChat.id}`;
		console.log(shareUrl);
Timothy J. Baek's avatar
Timothy J. Baek committed
24
		chat = await getChatById(localStorage.token, chatId);
Timothy J. Baek's avatar
Timothy J. Baek committed
25
26

		return shareUrl;
Timothy J. Baek's avatar
Timothy J. Baek committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	};

	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,
46
							models: $models.filter((m) => _chat.models.includes(m.id))
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
52
53
54
55
						}),
						'*'
					);
				}
			},
			false
		);
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
56
	export let show = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
57

58
59
60
61
62
63
64
65
	const isDifferentChat = (_chat) => {
		if (!chat) {
			return true;
		}
		if (!_chat) {
			return false;
		}
		return chat.id !== _chat.id || chat.share_id !== _chat.share_id;
Timothy J. Baek's avatar
Timothy J. Baek committed
66
	};
67

Timothy J. Baek's avatar
Timothy J. Baek committed
68
69
	$: if (show) {
		(async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
70
			if (chatId) {
71
72
73
74
				const _chat = await getChatById(localStorage.token, chatId);
				if (isDifferentChat(_chat)) {
					chat = _chat;
				}
Timothy J. Baek's avatar
Timothy J. Baek committed
75
76
77
78
79
80
			} else {
				chat = null;
				console.log(chat);
			}
		})();
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
</script>

83
84
<Modal bind:show size="sm">
	<div>
Timothy J. Baek's avatar
Timothy J. Baek committed
85
		<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-0.5">
86
			<div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
87
			<button
88
				class="self-center"
Timothy J. Baek's avatar
Timothy J. Baek committed
89
90
91
92
				on:click={() => {
					show = false;
				}}
			>
93
94
95
96
97
98
99
100
101
102
				<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>
Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
			</button>
		</div>
105

106
		{#if chat}
Timothy J. Baek's avatar
Timothy J. Baek committed
107
			<div class="px-5 pt-4 pb-5 w-full flex flex-col justify-center">
108
109
110
				<div class=" text-sm dark:text-gray-300 mb-1">
					{#if chat.share_id}
						<a href="/s/{chat.share_id}" target="_blank"
Timothy J. Baek's avatar
Timothy J. Baek committed
111
112
							>{$i18n.t('You have shared this chat')}
							<span class=" underline">{$i18n.t('before')}</span>.</a
113
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
114
						{$i18n.t('Click here to')}
115
						<button
116
117
							class="underline"
							on:click={async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
118
								const res = await deleteSharedChatById(localStorage.token, chatId);
119
120

								if (res) {
Timothy J. Baek's avatar
Timothy J. Baek committed
121
									chat = await getChatById(localStorage.token, chatId);
122
								}
Timothy J. Baek's avatar
Timothy J. Baek committed
123
124
125
126
							}}
							>{$i18n.t('delete this link')}
						</button>
						{$i18n.t('and create a new shared link.')}
127
					{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
128
						{$i18n.t(
Timothy J. Baek's avatar
Timothy J. Baek committed
129
							"Messages you send after creating your link won't be shared. Users with the URL will be able to view the shared chat."
Timothy J. Baek's avatar
Timothy J. Baek committed
130
						)}
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
					{/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"
151
								id="copy-and-share-chat-button"
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
								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());
									}

Timothy J. Baek's avatar
Timothy J. Baek committed
182
									toast.success($i18n.t('Copied shared chat URL to clipboard!'));
183
184
185
186
187
188
189
190
191
192
193
194
									show = false;
								}}
							>
								<Link />

								{#if chat.share_id}
									{$i18n.t('Update and Copy Link')}
								{:else}
									{$i18n.t('Copy Link')}
								{/if}
							</button>
						</div>
195
196
197
					</div>
				</div>
			</div>
198
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
199
200
	</div>
</Modal>