Navbar.svelte 4.09 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
2
	import { getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
3
	import { toast } from 'svelte-sonner';
4
5
6
	import fileSaver from 'file-saver';
	const { saveAs } = fileSaver;

Timothy J. Baek's avatar
Timothy J. Baek committed
7
	import { getChatById } from '$lib/apis/chats';
8
	import { WEBUI_NAME, chatId, modelfiles, settings } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
9
	import ShareChatModal from '../chat/ShareChatModal.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
	import TagInput from '../common/Tags/TagInput.svelte';
	import Tags from '../common/Tags.svelte';
12

13
14
	const i18n = getContext('i18n');

15
	export let initNewChat: Function;
16
	export let title: string = $WEBUI_NAME;
Timothy J. Baek's avatar
Timothy J. Baek committed
17
	export let shareEnabled: boolean = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
18

19
20
21
	export let tags = [];
	export let addTag: Function;
	export let deleteTag: Function;
Timothy J. Baek's avatar
Timothy J. Baek committed
22

23
	let showShareChatModal = false;
24
25
26
27

	let tagName = '';
	let showTagInput = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
28
	const shareChat = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
29
		const chat = (await getChatById(localStorage.token, $chatId)).chat;
Timothy J. Baek's avatar
Timothy J. Baek committed
30
31
		console.log('share', chat);

32
		toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
Timothy J. Baek's avatar
Timothy J. Baek committed
33
		const url = 'https://openwebui.com';
34
		// const url = 'http://localhost:5173';
Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
39
40
41

		const tab = await window.open(`${url}/chats/upload`, '_blank');
		window.addEventListener(
			'message',
			(event) => {
				if (event.origin !== url) return;
				if (event.data === 'loaded') {
42
43
44
45
46
47
48
					tab.postMessage(
						JSON.stringify({
							chat: chat,
							modelfiles: $modelfiles.filter((modelfile) => chat.models.includes(modelfile.tagName))
						}),
						'*'
					);
Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
				}
			},
			false
		);
	};
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

	const downloadChat = async () => {
		const chat = (await getChatById(localStorage.token, $chatId)).chat;
		console.log('download', chat);

		const chatText = chat.messages.reduce((a, message, i, arr) => {
			return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
		}, '');

		let blob = new Blob([chatText], {
			type: 'text/plain'
		});

		saveAs(blob, `chat-${chat.title}.txt`);
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
69
70
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
71
<ShareChatModal bind:show={showShareChatModal} {downloadChat} {shareChat} />
Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
<nav
	id="nav"
Timothy J. Baek's avatar
Timothy J. Baek committed
74
	class=" sticky py-2.5 top-0 flex flex-row justify-center bg-white/95 dark:bg-gray-900/90 dark:text-gray-200 backdrop-blur-xl z-30"
Timothy J. Baek's avatar
Timothy J. Baek committed
75
>
Timothy J. Baek's avatar
Timothy J. Baek committed
76
	<div
Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
		class=" flex {$settings?.fullScreenMode ?? null
			? 'max-w-full'
			: 'max-w-3xl'}  w-full mx-auto px-3"
Timothy J. Baek's avatar
Timothy J. Baek committed
80
	>
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
		<div class="flex items-center w-full max-w-full">
			<div class="pr-2 self-start">
Timothy J. Baek's avatar
Timothy J. Baek committed
83
				<button
84
					id="new-chat-button"
Timothy J. Baek's avatar
Timothy J. Baek committed
85
					class=" cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition"
86
					on:click={initNewChat}
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88
89
90
91
92
93
				>
					<div class=" m-auto self-center">
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 20 20"
							fill="currentColor"
							class="w-5 h-5"
Timothy J. Baek's avatar
Timothy J. Baek committed
94
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
97
98
99
100
101
							<path
								d="M5.433 13.917l1.262-3.155A4 4 0 017.58 9.42l6.92-6.918a2.121 2.121 0 013 3l-6.92 6.918c-.383.383-.84.685-1.343.886l-3.154 1.262a.5.5 0 01-.65-.65z"
							/>
							<path
								d="M3.5 5.75c0-.69.56-1.25 1.25-1.25H10A.75.75 0 0010 3H4.75A2.75 2.75 0 002 5.75v9.5A2.75 2.75 0 004.75 18h9.5A2.75 2.75 0 0017 15.25V10a.75.75 0 00-1.5 0v5.25c0 .69-.56 1.25-1.25 1.25h-9.5c-.69 0-1.25-.56-1.25-1.25v-9.5z"
							/>
						</svg>
Timothy J. Baek's avatar
Timothy J. Baek committed
102
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
				</button>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
			<div class=" flex-1 self-center font-medium line-clamp-1">
				<div>
107
					{title != '' ? title : $WEBUI_NAME}
Timothy J. Baek's avatar
Timothy J. Baek committed
108
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
			</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
111
			<div class="pl-2 self-center flex items-center space-x-2">
112
				{#if shareEnabled}
Timothy J. Baek's avatar
Timothy J. Baek committed
113
					<Tags {tags} {deleteTag} {addTag} />
114

Timothy J. Baek's avatar
Timothy J. Baek committed
115
					<button
Timothy J. Baek's avatar
Timothy J. Baek committed
116
						class=" cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition border dark:border-gray-600"
Timothy J. Baek's avatar
Timothy J. Baek committed
117
						on:click={async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
120
							showShareChatModal = !showShareChatModal;

							// console.log(showShareChatModal);
Timothy J. Baek's avatar
Timothy J. Baek committed
121
						}}
Timothy J. Baek's avatar
Timothy J. Baek committed
122
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
123
124
125
						<div class=" m-auto self-center">
							<svg
								xmlns="http://www.w3.org/2000/svg"
Timothy J. Baek's avatar
Timothy J. Baek committed
126
								viewBox="0 0 24 24"
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128
129
130
								fill="currentColor"
								class="w-4 h-4"
							>
								<path
Timothy J. Baek's avatar
Timothy J. Baek committed
131
132
133
									fill-rule="evenodd"
									d="M15.75 4.5a3 3 0 1 1 .825 2.066l-8.421 4.679a3.002 3.002 0 0 1 0 1.51l8.421 4.679a3 3 0 1 1-.729 1.31l-8.421-4.678a3 3 0 1 1 0-4.132l8.421-4.679a3 3 0 0 1-.096-.755Z"
									clip-rule="evenodd"
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
137
								/>
							</svg>
						</div>
					</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
138
139
				{/if}
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
140
		</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
141
	</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
142
</nav>