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

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

12
	export let initNewChat: Function;
13
	export let title: string = $WEBUI_NAME;
Timothy J. Baek's avatar
Timothy J. Baek committed
14
	export let shareEnabled: boolean = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
15

16
17
18
	export let tags = [];
	export let addTag: Function;
	export let deleteTag: Function;
Timothy J. Baek's avatar
Timothy J. Baek committed
19

20
	let showShareChatModal = false;
21
22
23
24

	let tagName = '';
	let showTagInput = false;

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

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

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

	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
66
67
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
68
<ShareChatModal bind:show={showShareChatModal} {downloadChat} {shareChat} />
Timothy J. Baek's avatar
Timothy J. Baek committed
69
70
<nav
	id="nav"
Timothy J. Baek's avatar
Timothy J. Baek committed
71
	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
72
>
Timothy J. Baek's avatar
Timothy J. Baek committed
73
	<div
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
		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
77
	>
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
		<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
80
				<button
81
					id="new-chat-button"
Timothy J. Baek's avatar
Timothy J. Baek committed
82
					class=" cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition"
83
					on:click={initNewChat}
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
86
87
88
89
90
				>
					<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
91
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
92
93
94
95
96
97
98
							<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
99
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
				</button>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
102
103
			<div class=" flex-1 self-center font-medium line-clamp-1">
				<div>
104
					{title != '' ? title : $WEBUI_NAME}
Timothy J. Baek's avatar
Timothy J. Baek committed
105
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
106
107
			</div>

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

Timothy J. Baek's avatar
Timothy J. Baek committed
112
					<button
Timothy J. Baek's avatar
Timothy J. Baek committed
113
						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
114
						on:click={async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
							showShareChatModal = !showShareChatModal;

							// console.log(showShareChatModal);
Timothy J. Baek's avatar
Timothy J. Baek committed
118
						}}
Timothy J. Baek's avatar
Timothy J. Baek committed
119
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
120
121
122
						<div class=" m-auto self-center">
							<svg
								xmlns="http://www.w3.org/2000/svg"
Timothy J. Baek's avatar
Timothy J. Baek committed
123
								viewBox="0 0 24 24"
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
126
127
								fill="currentColor"
								class="w-4 h-4"
							>
								<path
Timothy J. Baek's avatar
Timothy J. Baek committed
128
129
130
									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
131
132
133
134
								/>
							</svg>
						</div>
					</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
135
136
				{/if}
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
137
		</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
138
	</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
139
</nav>