"magic_pdf/pre_proc/detect_tables.py" did not exist on "7f0371da0e20efb6e466f0dbfad498fed59ced97"
ShareChatModal.svelte 4.17 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
3
4
	import { getContext, onMount } from 'svelte';

	import { toast } from 'svelte-sonner';
5
	import { deleteSharedChatById, getChatById, shareChatById } from '$lib/apis/chats';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7
8
	import { chatId, modelfiles } from '$lib/stores';
	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
	let chat = null;
Timothy J. Baek's avatar
Timothy J. Baek committed
13
	let shareUrl = null;
14
15
	const i18n = getContext('i18n');

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

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

		return shareUrl;
Timothy J. Baek's avatar
Timothy J. Baek committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	};

	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
		);
	};

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

Timothy J. Baek's avatar
Timothy J. Baek committed
58
59
60
61
62
63
64
65
66
67
	$: if (show) {
		(async () => {
			if ($chatId) {
				chat = await getChatById(localStorage.token, $chatId);
			} else {
				chat = null;
				console.log(chat);
			}
		})();
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
68
69
</script>

70
71
72
73
<Modal bind:show size="sm">
	<div>
		<div class=" flex justify-between dark:text-gray-300 px-5 py-4">
			<div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
74
			<button
75
				class="self-center"
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
78
79
				on:click={() => {
					show = false;
				}}
			>
80
81
82
83
84
85
86
87
88
89
				<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
90
91
			</button>
		</div>
92
93
		<hr class=" dark:border-gray-800" />

94
95
96
97
98
99
		{#if chat}
			<div class="px-4 pt-4 pb-5 w-full flex flex-col justify-center">
				<div class=" text-sm dark:text-gray-300 mb-1">
					{#if chat.share_id}
						<a href="/s/{chat.share_id}" target="_blank"
							>You have shared this chat <span class=" underline">before</span>.</a
100
						>
101
						Click here to
102
						<button
103
104
105
106
107
108
109
110
111
112
							class="underline"
							on:click={async () => {
								const res = await deleteSharedChatById(localStorage.token, $chatId);

								if (res) {
									chat = await getChatById(localStorage.token, $chatId);
								}
							}}>delete this link</button
						> and create a new shared link.
					{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
113
						Messages you send after creating your link won't be shared. Users with the URL will be
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
						able to view the shared chat.
					{/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"
Timothy J. Baek's avatar
Timothy J. Baek committed
135
								on:pointerdown={() => {
136
									shareLocalChat();
Timothy J. Baek's avatar
Timothy J. Baek committed
137
138
139
140
								}}
								on:click={async () => {
									copyToClipboard(shareUrl);
									toast.success($i18n.t('Copied shared chat URL to clipboard!'));
141
142
143
144
145
146
147
148
149
150
151
152
									show = false;
								}}
							>
								<Link />

								{#if chat.share_id}
									{$i18n.t('Update and Copy Link')}
								{:else}
									{$i18n.t('Copy Link')}
								{/if}
							</button>
						</div>
153
154
155
					</div>
				</div>
			</div>
156
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
157
158
	</div>
</Modal>