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

5
	import { chats, user, settings, scrollPaginationEnabled, pageSkip, pageLimit } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7

	import {
Timothy J. Baek's avatar
Timothy J. Baek committed
8
		archiveAllChats,
Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
12
13
14
15
		createNewChat,
		deleteAllChats,
		getAllChats,
		getAllUserChats,
		getChatList
	} from '$lib/apis/chats';
	import { getImportOrigin, convertOpenAIChats } from '$lib/utils';
16
	import { onMount, getContext } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
17
	import { goto } from '$app/navigation';
Jannik Streidl's avatar
Jannik Streidl committed
18
	import { toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
19

20
21
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
	export let saveSettings: Function;
	// Chats
	let saveChatHistory = true;
	let importFiles;
Timothy J. Baek's avatar
Timothy J. Baek committed
26
27

	let showArchiveConfirm = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
28
	let showDeleteConfirm = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
29

30
	let chatImportInputElement: HTMLInputElement;
Timothy J. Baek's avatar
Timothy J. Baek committed
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
56
57
58
59
60
61
62
63

	$: if (importFiles) {
		console.log(importFiles);

		let reader = new FileReader();
		reader.onload = (event) => {
			let chats = JSON.parse(event.target.result);
			console.log(chats);
			if (getImportOrigin(chats) == 'openai') {
				try {
					chats = convertOpenAIChats(chats);
				} catch (error) {
					console.log('Unable to import chats:', error);
				}
			}
			importChats(chats);
		};

		if (importFiles.length > 0) {
			reader.readAsText(importFiles[0]);
		}
	}

	const importChats = async (_chats) => {
		for (const chat of _chats) {
			console.log(chat);

			if (chat.chat) {
				await createNewChat(localStorage.token, chat.chat);
			} else {
				await createNewChat(localStorage.token, chat);
			}
		}
64
65
66
67
68
69
		// loading all chats. disable pagination on scrol.
		scrollPaginationEnabled.set(false);
		// subsequent queries will calculate page size to rehydrate the ui.
		// since every chat is already loaded, the calculation should now load all chats.
		pageSkip.set(0);
		pageLimit.set(-1);
Timothy J. Baek's avatar
Timothy J. Baek committed
70
71
72
73
74
75
76
77
78
79
		await chats.set(await getChatList(localStorage.token));
	};

	const exportChats = async () => {
		let blob = new Blob([JSON.stringify(await getAllChats(localStorage.token))], {
			type: 'application/json'
		});
		saveAs(blob, `chat-export-${Date.now()}.json`);
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
83
84
	const archiveAllChatsHandler = async () => {
		await goto('/');
		await archiveAllChats(localStorage.token).catch((error) => {
			toast.error(error);
		});
85
86
87
88
89
90
		// loading all chats. disable pagination on scrol.
		scrollPaginationEnabled.set(false);
		// subsequent queries will calculate page size to rehydrate the ui.
		// since every chat is already loaded, the calculation should now load all chats.
		pageSkip.set(0);
		pageLimit.set(-1);
Timothy J. Baek's avatar
Timothy J. Baek committed
91
92
93
		await chats.set(await getChatList(localStorage.token));
	};

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
94
	const deleteAllChatsHandler = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
95
		await goto('/');
96
97
98
		await deleteAllChats(localStorage.token).catch((error) => {
			toast.error(error);
		});
99
100
101
102
103
104
		// loading all chats. disable pagination on scrol.
		scrollPaginationEnabled.set(false);
		// subsequent queries will calculate page size to rehydrate the ui.
		// since every chat is already loaded, the calculation should now load all chats.
		pageSkip.set(0);
		pageLimit.set(-1);
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
		await chats.set(await getChatList(localStorage.token));
	};

	const toggleSaveChatHistory = async () => {
		saveChatHistory = !saveChatHistory;
		console.log(saveChatHistory);

		if (saveChatHistory === false) {
			await goto('/');
		}
		saveSettings({ saveChatHistory: saveChatHistory });
	};

	onMount(async () => {
119
		saveChatHistory = $settings.saveChatHistory ?? true;
Timothy J. Baek's avatar
Timothy J. Baek committed
120
121
122
	});
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
123
<div class="flex flex-col h-full justify-between space-y-3 text-sm max-h-[22rem]">
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
126
127
128
	<div class=" space-y-2">
		<div
			class="flex flex-col justify-between rounded-md items-center py-2 px-3.5 w-full transition"
		>
			<div class="flex w-full justify-between">
129
				<div class=" self-center text-sm font-medium">{$i18n.t('Chat History')}</div>
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
151
152

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					type="button"
					on:click={() => {
						toggleSaveChatHistory();
					}}
				>
					{#if saveChatHistory === true}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
							<path
								fill-rule="evenodd"
								d="M1.38 8.28a.87.87 0 0 1 0-.566 7.003 7.003 0 0 1 13.238.006.87.87 0 0 1 0 .566A7.003 7.003 0 0 1 1.379 8.28ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
								clip-rule="evenodd"
							/>
						</svg>

153
						<span class="ml-2 self-center"> {$i18n.t('On')} </span>
Timothy J. Baek's avatar
Timothy J. Baek committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
					{:else}
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								fill-rule="evenodd"
								d="M3.28 2.22a.75.75 0 0 0-1.06 1.06l10.5 10.5a.75.75 0 1 0 1.06-1.06l-1.322-1.323a7.012 7.012 0 0 0 2.16-3.11.87.87 0 0 0 0-.567A7.003 7.003 0 0 0 4.82 3.76l-1.54-1.54Zm3.196 3.195 1.135 1.136A1.502 1.502 0 0 1 9.45 8.389l1.136 1.135a3 3 0 0 0-4.109-4.109Z"
								clip-rule="evenodd"
							/>
							<path
								d="m7.812 10.994 1.816 1.816A7.003 7.003 0 0 1 1.38 8.28a.87.87 0 0 1 0-.566 6.985 6.985 0 0 1 1.113-2.039l2.513 2.513a3 3 0 0 0 2.806 2.806Z"
							/>
						</svg>

171
						<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
172
173
174
175
176
					{/if}
				</button>
			</div>

			<div class="text-xs text-left w-full font-medium mt-0.5">
Jannik Streidl's avatar
Jannik Streidl committed
177
				{$i18n.t('This setting does not sync across browsers or devices.')}
Timothy J. Baek's avatar
Timothy J. Baek committed
178
179
180
			</div>
		</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
181
		<hr class=" dark:border-gray-850" />
Timothy J. Baek's avatar
Timothy J. Baek committed
182
183

		<div class="flex flex-col">
184
185
186
187
188
189
190
191
			<input
				id="chat-import-input"
				bind:this={chatImportInputElement}
				bind:files={importFiles}
				type="file"
				accept=".json"
				hidden
			/>
Timothy J. Baek's avatar
Timothy J. Baek committed
192
193
			<button
				class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
Timothy J. Baek's avatar
Timothy J. Baek committed
194
195
196
				on:click={() => {
					chatImportInputElement.click();
				}}
Timothy J. Baek's avatar
Timothy J. Baek committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			>
				<div class=" self-center mr-3">
					<svg
						xmlns="http://www.w3.org/2000/svg"
						viewBox="0 0 16 16"
						fill="currentColor"
						class="w-4 h-4"
					>
						<path
							fill-rule="evenodd"
							d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
							clip-rule="evenodd"
						/>
					</svg>
				</div>
212
				<div class=" self-center text-sm font-medium">{$i18n.t('Import Chats')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
			</button>
			<button
				class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
				on:click={() => {
					exportChats();
				}}
			>
				<div class=" self-center mr-3">
					<svg
						xmlns="http://www.w3.org/2000/svg"
						viewBox="0 0 16 16"
						fill="currentColor"
						class="w-4 h-4"
					>
						<path
							fill-rule="evenodd"
							d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
							clip-rule="evenodd"
						/>
					</svg>
				</div>
234
				<div class=" self-center text-sm font-medium">{$i18n.t('Export Chats')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
235
236
237
			</button>
		</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
238
		<hr class=" dark:border-gray-850" />
Timothy J. Baek's avatar
Timothy J. Baek committed
239

Timothy J. Baek's avatar
Timothy J. Baek committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
		<div class="flex flex-col">
			{#if showArchiveConfirm}
				<div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
					<div class="flex items-center space-x-3">
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
							<path
								fill-rule="evenodd"
								d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM5.72 7.47a.75.75 0 0 1 1.06 0L8 8.69l1.22-1.22a.75.75 0 1 1 1.06 1.06L9.06 9.75l1.22 1.22a.75.75 0 1 1-1.06 1.06L8 10.81l-1.22 1.22a.75.75 0 0 1-1.06-1.06l1.22-1.22-1.22-1.22a.75.75 0 0 1 0-1.06Z"
								clip-rule="evenodd"
							/>
						</svg>
						<span>{$i18n.t('Are you sure?')}</span>
					</div>

					<div class="flex space-x-1.5 items-center">
						<button
							class="hover:text-white transition"
							on:click={() => {
								archiveAllChatsHandler();
								showArchiveConfirm = false;
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								viewBox="0 0 20 20"
								fill="currentColor"
								class="w-4 h-4"
							>
								<path
									fill-rule="evenodd"
									d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
									clip-rule="evenodd"
								/>
							</svg>
						</button>
						<button
							class="hover:text-white transition"
							on:click={() => {
								showArchiveConfirm = false;
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								viewBox="0 0 20 20"
								fill="currentColor"
								class="w-4 h-4"
							>
								<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>
						</button>
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
299
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
			{:else}
				<button
					class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
					on:click={() => {
						showArchiveConfirm = true;
					}}
				>
					<div class=" self-center mr-3">
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 24 24"
							fill="currentColor"
							class="size-4"
						>
							<path
								d="M3.375 3C2.339 3 1.5 3.84 1.5 4.875v.75c0 1.036.84 1.875 1.875 1.875h17.25c1.035 0 1.875-.84 1.875-1.875v-.75C22.5 3.839 21.66 3 20.625 3H3.375Z"
							/>
							<path
								fill-rule="evenodd"
								d="m3.087 9 .54 9.176A3 3 0 0 0 6.62 21h10.757a3 3 0 0 0 2.995-2.824L20.913 9H3.087Zm6.163 3.75A.75.75 0 0 1 10 12h4a.75.75 0 0 1 0 1.5h-4a.75.75 0 0 1-.75-.75Z"
								clip-rule="evenodd"
							/>
						</svg>
					</div>
					<div class=" self-center text-sm font-medium">{$i18n.t('Archive All Chats')}</div>
				</button>
			{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
327

Timothy J. Baek's avatar
Timothy J. Baek committed
328
329
330
			{#if showDeleteConfirm}
				<div class="flex justify-between rounded-md items-center py-2 px-3.5 w-full transition">
					<div class="flex items-center space-x-3">
Timothy J. Baek's avatar
Timothy J. Baek committed
331
332
						<svg
							xmlns="http://www.w3.org/2000/svg"
Timothy J. Baek's avatar
Timothy J. Baek committed
333
							viewBox="0 0 16 16"
Timothy J. Baek's avatar
Timothy J. Baek committed
334
335
336
							fill="currentColor"
							class="w-4 h-4"
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
337
							<path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
Timothy J. Baek's avatar
Timothy J. Baek committed
338
339
							<path
								fill-rule="evenodd"
Timothy J. Baek's avatar
Timothy J. Baek committed
340
								d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM5.72 7.47a.75.75 0 0 1 1.06 0L8 8.69l1.22-1.22a.75.75 0 1 1 1.06 1.06L9.06 9.75l1.22 1.22a.75.75 0 1 1-1.06 1.06L8 10.81l-1.22 1.22a.75.75 0 0 1-1.06-1.06l1.22-1.22-1.22-1.22a.75.75 0 0 1 0-1.06Z"
Timothy J. Baek's avatar
Timothy J. Baek committed
341
342
343
								clip-rule="evenodd"
							/>
						</svg>
Timothy J. Baek's avatar
Timothy J. Baek committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
						<span>{$i18n.t('Are you sure?')}</span>
					</div>

					<div class="flex space-x-1.5 items-center">
						<button
							class="hover:text-white transition"
							on:click={() => {
								deleteAllChatsHandler();
								showDeleteConfirm = false;
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								viewBox="0 0 20 20"
								fill="currentColor"
								class="w-4 h-4"
							>
								<path
									fill-rule="evenodd"
									d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
									clip-rule="evenodd"
								/>
							</svg>
						</button>
						<button
							class="hover:text-white transition"
							on:click={() => {
								showDeleteConfirm = false;
							}}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								viewBox="0 0 20 20"
								fill="currentColor"
								class="w-4 h-4"
							>
								<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>
						</button>
					</div>
				</div>
			{:else}
				<button
					class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
					on:click={() => {
						showDeleteConfirm = true;
					}}
				>
					<div class=" self-center mr-3">
Timothy J. Baek's avatar
Timothy J. Baek committed
395
396
						<svg
							xmlns="http://www.w3.org/2000/svg"
Timothy J. Baek's avatar
Timothy J. Baek committed
397
							viewBox="0 0 16 16"
Timothy J. Baek's avatar
Timothy J. Baek committed
398
399
400
401
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
Timothy J. Baek's avatar
Timothy J. Baek committed
402
403
404
								fill-rule="evenodd"
								d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm7 7a.75.75 0 0 1-.75.75h-4.5a.75.75 0 0 1 0-1.5h4.5A.75.75 0 0 1 11 9Z"
								clip-rule="evenodd"
Timothy J. Baek's avatar
Timothy J. Baek committed
405
406
							/>
						</svg>
Timothy J. Baek's avatar
Timothy J. Baek committed
407
408
409
410
411
					</div>
					<div class=" self-center text-sm font-medium">{$i18n.t('Delete All Chats')}</div>
				</button>
			{/if}
		</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
412
413
	</div>
</div>