UserMessage.svelte 11.5 KB
Newer Older
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
	import dayjs from 'dayjs';

4
	import { tick, createEventDispatcher, getContext } from 'svelte';
5
6
	import Name from './Name.svelte';
	import ProfileImage from './ProfileImage.svelte';
7
	import { models, settings } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
8
	import Tooltip from '$lib/components/common/Tooltip.svelte';
9

10
	import { user as _user } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
11
	import { getFileContentById } from '$lib/apis/files';
Timothy J. Baek's avatar
Timothy J. Baek committed
12
	import FileItem from '$lib/components/common/FileItem.svelte';
13

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

16
17
	const dispatch = createEventDispatcher();

18
19
20
	export let user;
	export let message;
	export let siblings;
21
	export let isFirstMessage: boolean;
22
	export let readOnly: boolean;
23
24
25
26
27
28
29
30

	export let confirmEditMessage: Function;
	export let showPreviousMessage: Function;
	export let showNextMessage: Function;
	export let copyToClipboard: Function;

	let edit = false;
	let editedContent = '';
31
	let messageEditTextAreaElement: HTMLTextAreaElement;
32
33
34
35
36
37
	const editMessageHandler = async () => {
		edit = true;
		editedContent = message.content;

		await tick();

38
39
		messageEditTextAreaElement.style.height = '';
		messageEditTextAreaElement.style.height = `${messageEditTextAreaElement.scrollHeight}px`;
40

41
		messageEditTextAreaElement?.focus();
42
43
44
45
46
47
48
49
50
51
52
53
54
	};

	const editMessageConfirmHandler = async () => {
		confirmEditMessage(message.id, editedContent);

		edit = false;
		editedContent = '';
	};

	const cancelEditMessage = () => {
		edit = false;
		editedContent = '';
	};
55
56
57
58

	const deleteMessageHandler = async () => {
		dispatch('delete', message.id);
	};
59
60
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
61
<div class=" flex w-full user-message" dir={$settings.chatDirection}>
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
64
	{#if !($settings?.chatBubble ?? true)}
		<ProfileImage
			src={message.user
65
				? $models.find((m) => m.id === message.user)?.info?.meta?.profile_image_url ?? '/user.png'
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
				: user?.profile_image_url ?? '/user.png'}
		/>
	{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
69
	<div class="w-full overflow-hidden pl-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
70
71
72
73
		{#if !($settings?.chatBubble ?? true)}
			<div>
				<Name>
					{#if message.user}
74
75
						{$i18n.t('You')}
						<span class=" text-gray-500 text-sm font-medium">{message?.user ?? ''}</span>
76
					{:else if $settings.showUsername || $_user.name !== user.name}
Timothy J. Baek's avatar
Timothy J. Baek committed
77
						{user.name}
Timothy J. Baek's avatar
Timothy J. Baek committed
78
					{:else}
79
						{$i18n.t('You')}
Timothy J. Baek's avatar
Timothy J. Baek committed
80
					{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82

					{#if message.timestamp}
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
						<span
							class=" invisible group-hover:visible text-gray-400 text-xs font-medium uppercase"
						>
							{dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88
89
90
91
						</span>
					{/if}
				</Name>
			</div>
		{/if}
92
93

		<div
Timothy J. Baek's avatar
Timothy J. Baek committed
94
			class="prose chat-{message.role} w-full max-w-full flex flex-col justify-end dark:prose-invert prose-headings:my-0 prose-p:my-0 prose-p:-mb-4 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-6 prose-li:-mb-4 whitespace-pre-line"
95
96
		>
			{#if message.files}
Timothy J. Baek's avatar
Timothy J. Baek committed
97
				<div class="mt-2.5 mb-1 w-full flex flex-col justify-end overflow-x-auto gap-1 flex-wrap">
98
					{#each message.files as file}
Timothy J. Baek's avatar
Timothy J. Baek committed
99
						<div class={$settings?.chatBubble ?? true ? 'self-end' : ''}>
100
101
							{#if file.type === 'image'}
								<img src={file.url} alt="input" class=" max-h-96 rounded-lg" draggable="false" />
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
102
103
							{:else}
								<FileItem url={file.url} name={file.name} type={file.type} />
104
105
106
107
108
109
110
							{/if}
						</div>
					{/each}
				</div>
			{/if}

			{#if edit === true}
Timothy J. Baek's avatar
Timothy J. Baek committed
111
				<div class=" w-full bg-gray-50 dark:bg-gray-800 rounded-3xl px-5 py-3 mb-2">
112
113
					<textarea
						id="message-edit-{message.id}"
114
						bind:this={messageEditTextAreaElement}
115
116
117
						class=" bg-transparent outline-none w-full resize-none"
						bind:value={editedContent}
						on:input={(e) => {
118
119
							e.target.style.height = '';
							e.target.style.height = `${e.target.scrollHeight}px`;
120
						}}
121
						on:keydown={(e) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
124
							if (e.key === 'Escape') {
								document.getElementById('close-edit-message-button')?.click();
							}
125

Timothy J. Baek's avatar
Timothy J. Baek committed
126
							const isCmdOrCtrlPressed = e.metaKey || e.ctrlKey;
127
128
129
130
131
132
							const isEnterPressed = e.key === 'Enter';

							if (isCmdOrCtrlPressed && isEnterPressed) {
								document.getElementById('save-edit-message-button')?.click();
							}
						}}
133
134
					/>

Timothy J. Baek's avatar
Timothy J. Baek committed
135
					<div class=" mt-2 mb-1 flex justify-end space-x-1.5 text-sm font-medium">
136
						<button
Timothy J. Baek's avatar
Timothy J. Baek committed
137
							id="close-edit-message-button"
Timothy J. Baek's avatar
Timothy J. Baek committed
138
							class="px-4 py-2 bg-white dark:bg-gray-900 hover:bg-gray-100 text-gray-800 dark:text-gray-100 transition rounded-3xl"
139
							on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
140
								cancelEditMessage();
141
142
							}}
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
143
							{$i18n.t('Cancel')}
144
145
146
						</button>

						<button
Timothy J. Baek's avatar
Timothy J. Baek committed
147
							id="save-edit-message-button"
Timothy J. Baek's avatar
Timothy J. Baek committed
148
							class=" px-4 py-2 bg-gray-900 dark:bg-white hover:bg-gray-850 text-gray-100 dark:text-gray-800 transition rounded-3xl"
149
							on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
150
								editMessageConfirmHandler();
151
152
							}}
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
153
							{$i18n.t('Send')}
154
155
156
157
158
						</button>
					</div>
				</div>
			{:else}
				<div class="w-full">
Timothy J. Baek's avatar
Timothy J. Baek committed
159
					<div class="flex {$settings?.chatBubble ?? true ? 'justify-end' : ''} mb-2">
Timothy J. Baek's avatar
Timothy J. Baek committed
160
						<div
Timothy J. Baek's avatar
Timothy J. Baek committed
161
162
163
164
165
							class="rounded-3xl {$settings?.chatBubble ?? true
								? `max-w-[90%] px-5 py-2  bg-gray-50 dark:bg-gray-850 ${
										message.files ? 'rounded-tr-lg' : ''
								  }`
								: ''}  "
Timothy J. Baek's avatar
Timothy J. Baek committed
166
167
168
169
						>
							<pre id="user-message">{message.content}</pre>
						</div>
					</div>
170

Timothy J. Baek's avatar
Timothy J. Baek committed
171
172
173
					<div
						class=" flex {$settings?.chatBubble ?? true
							? 'justify-end'
Timothy J. Baek's avatar
Timothy J. Baek committed
174
							: ''}  text-gray-600 dark:text-gray-500"
Timothy J. Baek's avatar
Timothy J. Baek committed
175
176
177
					>
						{#if !($settings?.chatBubble ?? true)}
							{#if siblings.length > 1}
Timothy J. Baek's avatar
Timothy J. Baek committed
178
								<div class="flex self-center" dir="ltr">
Timothy J. Baek's avatar
Timothy J. Baek committed
179
180
181
182
183
									<button
										class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
										on:click={() => {
											showPreviousMessage(message);
										}}
184
									>
Timothy J. Baek's avatar
Timothy J. Baek committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
										<svg
											xmlns="http://www.w3.org/2000/svg"
											fill="none"
											viewBox="0 0 24 24"
											stroke="currentColor"
											stroke-width="2.5"
											class="size-3.5"
										>
											<path
												stroke-linecap="round"
												stroke-linejoin="round"
												d="M15.75 19.5 8.25 12l7.5-7.5"
											/>
										</svg>
									</button>
200

Timothy J. Baek's avatar
Timothy J. Baek committed
201
202
203
									<div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
										{siblings.indexOf(message.id) + 1}/{siblings.length}
									</div>
204

Timothy J. Baek's avatar
Timothy J. Baek committed
205
206
207
208
209
									<button
										class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
										on:click={() => {
											showNextMessage(message);
										}}
210
									>
Timothy J. Baek's avatar
Timothy J. Baek committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
										<svg
											xmlns="http://www.w3.org/2000/svg"
											fill="none"
											viewBox="0 0 24 24"
											stroke="currentColor"
											stroke-width="2.5"
											class="size-3.5"
										>
											<path
												stroke-linecap="round"
												stroke-linejoin="round"
												d="m8.25 4.5 7.5 7.5-7.5 7.5"
											/>
										</svg>
									</button>
								</div>
							{/if}
228
						{/if}
229
						{#if !readOnly}
230
							<Tooltip content={$i18n.t('Edit')} placement="bottom">
231
								<button
Timothy J. Baek's avatar
Timothy J. Baek committed
232
									class="invisible group-hover:visible p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition edit-user-message-button"
233
234
235
									on:click={() => {
										editMessageHandler();
									}}
Timothy J. Baek's avatar
Timothy J. Baek committed
236
								>
237
238
239
240
									<svg
										xmlns="http://www.w3.org/2000/svg"
										fill="none"
										viewBox="0 0 24 24"
Timothy J. Baek's avatar
Timothy J. Baek committed
241
										stroke-width="2.3"
242
243
244
245
246
247
248
249
250
251
252
253
										stroke="currentColor"
										class="w-4 h-4"
									>
										<path
											stroke-linecap="round"
											stroke-linejoin="round"
											d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
										/>
									</svg>
								</button>
							</Tooltip>
						{/if}
254

255
						<Tooltip content={$i18n.t('Copy')} placement="bottom">
Timothy J. Baek's avatar
Timothy J. Baek committed
256
							<button
Timothy J. Baek's avatar
Timothy J. Baek committed
257
								class="invisible group-hover:visible p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition"
Timothy J. Baek's avatar
Timothy J. Baek committed
258
								on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
259
									copyToClipboard(message.content);
Timothy J. Baek's avatar
Timothy J. Baek committed
260
								}}
261
							>
Timothy J. Baek's avatar
Timothy J. Baek committed
262
263
264
265
								<svg
									xmlns="http://www.w3.org/2000/svg"
									fill="none"
									viewBox="0 0 24 24"
Timothy J. Baek's avatar
Timothy J. Baek committed
266
									stroke-width="2.3"
Timothy J. Baek's avatar
Timothy J. Baek committed
267
268
269
270
271
272
									stroke="currentColor"
									class="w-4 h-4"
								>
									<path
										stroke-linecap="round"
										stroke-linejoin="round"
Timothy J. Baek's avatar
Timothy J. Baek committed
273
										d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
Timothy J. Baek's avatar
Timothy J. Baek committed
274
275
276
									/>
								</svg>
							</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
277
278
						</Tooltip>

279
						{#if !isFirstMessage && !readOnly}
280
							<Tooltip content={$i18n.t('Delete')} placement="bottom">
Timothy J. Baek's avatar
Timothy J. Baek committed
281
282
283
284
285
286
287
288
289
290
								<button
									class="invisible group-hover:visible p-1 rounded dark:hover:text-white hover:text-black transition"
									on:click={() => {
										deleteMessageHandler();
									}}
								>
									<svg
										xmlns="http://www.w3.org/2000/svg"
										fill="none"
										viewBox="0 0 24 24"
291
										stroke-width="2"
Timothy J. Baek's avatar
Timothy J. Baek committed
292
293
294
295
296
297
298
299
300
301
302
										stroke="currentColor"
										class="w-4 h-4"
									>
										<path
											stroke-linecap="round"
											stroke-linejoin="round"
											d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
										/>
									</svg>
								</button>
							</Tooltip>
Timothy J. Baek's avatar
Timothy J. Baek committed
303
						{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
304
305
306

						{#if $settings?.chatBubble ?? true}
							{#if siblings.length > 1}
Timothy J. Baek's avatar
Timothy J. Baek committed
307
								<div class="flex self-center" dir="ltr">
Timothy J. Baek's avatar
Timothy J. Baek committed
308
309
310
311
312
									<button
										class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
										on:click={() => {
											showPreviousMessage(message);
										}}
Timothy J. Baek's avatar
Timothy J. Baek committed
313
									>
Timothy J. Baek's avatar
Timothy J. Baek committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
										<svg
											xmlns="http://www.w3.org/2000/svg"
											fill="none"
											viewBox="0 0 24 24"
											stroke="currentColor"
											stroke-width="2.5"
											class="size-3.5"
										>
											<path
												stroke-linecap="round"
												stroke-linejoin="round"
												d="M15.75 19.5 8.25 12l7.5-7.5"
											/>
										</svg>
									</button>
Timothy J. Baek's avatar
Timothy J. Baek committed
329

Timothy J. Baek's avatar
Timothy J. Baek committed
330
331
332
									<div class="text-sm tracking-widest font-semibold self-center dark:text-gray-100">
										{siblings.indexOf(message.id) + 1}/{siblings.length}
									</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
333

Timothy J. Baek's avatar
Timothy J. Baek committed
334
335
336
337
338
									<button
										class="self-center p-1 hover:bg-black/5 dark:hover:bg-white/5 dark:hover:text-white hover:text-black rounded-md transition"
										on:click={() => {
											showNextMessage(message);
										}}
Timothy J. Baek's avatar
Timothy J. Baek committed
339
									>
Timothy J. Baek's avatar
Timothy J. Baek committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
										<svg
											xmlns="http://www.w3.org/2000/svg"
											fill="none"
											viewBox="0 0 24 24"
											stroke="currentColor"
											stroke-width="2.5"
											class="size-3.5"
										>
											<path
												stroke-linecap="round"
												stroke-linejoin="round"
												d="m8.25 4.5 7.5 7.5-7.5 7.5"
											/>
										</svg>
									</button>
								</div>
							{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
357
						{/if}
358
359
360
361
362
363
					</div>
				</div>
			{/if}
		</div>
	</div>
</div>