"src/vscode:/vscode.git/clone" did not exist on "9e9070b6e97e987e2f986760130fd5a075142185"
ChatItem.svelte 7.77 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
<script lang="ts">
	import { toast } from 'svelte-sonner';
3
4
	import { goto, invalidate, invalidateAll } from '$app/navigation';
	import { onMount, getContext, createEventDispatcher, tick } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
7
8
9
10
11
12
13
	const i18n = getContext('i18n');

	const dispatch = createEventDispatcher();

	import {
		archiveChatById,
		cloneChatById,
		deleteChatById,
		getChatList,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
		getChatListByTagName,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
		updateChatById
	} from '$lib/apis/chats';
17
	import { chatId, chats, mobile, pinnedChats, showSidebar, currentChatPage } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20

	import ChatMenu from './ChatMenu.svelte';
	import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
21
22
23
	import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
	import Tooltip from '$lib/components/common/Tooltip.svelte';
	import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25

	export let chat;
Timothy J. Baek's avatar
Timothy J. Baek committed
26
	export let selected = false;
27
28
29
	export let shiftKey = false;

	let mouseOver = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
30
31
32
33

	let showShareChatModal = false;
	let confirmEdit = false;

Timothy J. Baek's avatar
Timothy J. Baek committed
34
	let chatTitle = chat.title;
Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
39
40
41
42

	const editChatTitle = async (id, _title) => {
		if (_title === '') {
			toast.error($i18n.t('Title cannot be an empty string.'));
		} else {
			await updateChatById(localStorage.token, id, {
				title: _title
			});
43
44
45

			currentChatPage.set(0);
			await chats.set(await getChatList(localStorage.token, $currentChatPage));
Timothy J. Baek's avatar
Timothy J. Baek committed
46
			await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
52
53
54
55
56
57
		}
	};

	const cloneChatHandler = async (id) => {
		const res = await cloneChatById(localStorage.token, id).catch((error) => {
			toast.error(error);
			return null;
		});

		if (res) {
			goto(`/c/${res.id}`);
58
59
60

			currentChatPage.set(0);
			await chats.set(await getChatList(localStorage.token, $currentChatPage));
Timothy J. Baek's avatar
Timothy J. Baek committed
61
			await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
64
65
66
		}
	};

	const archiveChatHandler = async (id) => {
		await archiveChatById(localStorage.token, id);
67
68
69

		currentChatPage.set(0);
		await chats.set(await getChatList(localStorage.token, $currentChatPage));
Timothy J. Baek's avatar
Timothy J. Baek committed
70
		await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
Timothy J. Baek's avatar
Timothy J. Baek committed
71
72
73
74
75
76
77
78
79
80
81
82
	};

	const focusEdit = async (node: HTMLInputElement) => {
		node.focus();
	};
</script>

<ShareChatModal bind:show={showShareChatModal} chatId={chat.id} />

<div class=" w-full pr-2 relative group">
	{#if confirmEdit}
		<div
Timothy J. Baek's avatar
Timothy J. Baek committed
83
			class=" w-full flex justify-between rounded-xl px-3 py-2 {chat.id === $chatId || confirmEdit
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
86
87
88
89
90
91
92
93
94
95
96
				? 'bg-gray-200 dark:bg-gray-900'
				: selected
				? 'bg-gray-100 dark:bg-gray-950'
				: 'group-hover:bg-gray-100 dark:group-hover:bg-gray-950'}  whitespace-nowrap text-ellipsis"
		>
			<input
				use:focusEdit
				bind:value={chatTitle}
				class=" bg-transparent w-full outline-none mr-10"
			/>
		</div>
	{:else}
		<a
Timothy J. Baek's avatar
Timothy J. Baek committed
97
			class=" w-full flex justify-between rounded-xl px-3 py-2 {chat.id === $chatId || confirmEdit
Timothy J. Baek's avatar
Timothy J. Baek committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
				? 'bg-gray-200 dark:bg-gray-900'
				: selected
				? 'bg-gray-100 dark:bg-gray-950'
				: ' group-hover:bg-gray-100 dark:group-hover:bg-gray-950'}  whitespace-nowrap text-ellipsis"
			href="/c/{chat.id}"
			on:click={() => {
				dispatch('select');

				if ($mobile) {
					showSidebar.set(false);
				}
			}}
			on:dblclick={() => {
				chatTitle = chat.title;
				confirmEdit = true;
			}}
114
115
116
117
118
			on:mouseenter={(e) => {
				mouseOver = true;
			}}
			on:mouseleave={(e) => {
				mouseOver = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
119
120
121
122
123
124
125
126
127
128
129
130
			}}
			on:focus={(e) => {}}
			draggable="false"
		>
			<div class=" flex self-center flex-1 w-full">
				<div class=" text-left self-center overflow-hidden w-full h-[20px]">
					{chat.title}
				</div>
			</div>
		</a>
	{/if}

131
	<!-- svelte-ignore a11y-no-static-element-interactions -->
Timothy J. Baek's avatar
Timothy J. Baek committed
132
133
	<div
		class="
Timothy J. Baek's avatar
Timothy J. Baek committed
134
        {chat.id === $chatId || confirmEdit
Timothy J. Baek's avatar
Timothy J. Baek committed
135
136
137
138
			? 'from-gray-200 dark:from-gray-900'
			: selected
			? 'from-gray-100 dark:from-gray-950'
			: 'invisible group-hover:visible from-gray-100 dark:from-gray-950'}
Timothy J. Baek's avatar
Timothy J. Baek committed
139
            absolute right-[10px] top-[6px] py-1 pr-2 pl-5 bg-gradient-to-l from-80%
Timothy J. Baek's avatar
Timothy J. Baek committed
140
141

              to-transparent"
142
143
144
145
146
147
		on:mouseenter={(e) => {
			mouseOver = true;
		}}
		on:mouseleave={(e) => {
			mouseOver = false;
		}}
Timothy J. Baek's avatar
Timothy J. Baek committed
148
149
150
	>
		{#if confirmEdit}
			<div class="flex self-center space-x-1.5 z-10">
151
				<Tooltip content={$i18n.t('Confirm')}>
152
153
154
155
156
157
158
					<button
						class=" self-center dark:hover:text-white transition"
						on:click={() => {
							editChatTitle(chat.id, chatTitle);
							confirmEdit = false;
							chatTitle = '';
						}}
Timothy J. Baek's avatar
Timothy J. Baek committed
159
					>
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
						<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>
				</Tooltip>

175
				<Tooltip content={$i18n.t('Cancel')}>
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
					<button
						class=" self-center dark:hover:text-white transition"
						on:click={() => {
							confirmEdit = false;
							chatTitle = '';
						}}
					>
						<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>
				</Tooltip>
			</div>
		{:else if shiftKey && mouseOver}
			<div class=" flex items-center self-center space-x-1.5">
198
				<Tooltip content={$i18n.t('Archive')} className="flex items-center">
199
200
201
202
203
204
205
206
207
208
209
					<button
						class=" self-center dark:hover:text-white transition"
						on:click={() => {
							archiveChatHandler(chat.id);
						}}
						type="button"
					>
						<ArchiveBox className="size-4  translate-y-[0.5px]" strokeWidth="2" />
					</button>
				</Tooltip>

210
				<Tooltip content={$i18n.t('Delete')}>
211
212
213
					<button
						class=" self-center dark:hover:text-white transition"
						on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
214
							dispatch('delete', 'shift');
215
216
						}}
						type="button"
Timothy J. Baek's avatar
Timothy J. Baek committed
217
					>
218
219
220
						<GarbageBin strokeWidth="2" />
					</button>
				</Tooltip>
Timothy J. Baek's avatar
Timothy J. Baek committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
			</div>
		{:else}
			<div class="flex self-center space-x-1 z-10">
				<ChatMenu
					chatId={chat.id}
					cloneChatHandler={() => {
						cloneChatHandler(chat.id);
					}}
					shareHandler={() => {
						showShareChatModal = true;
					}}
					archiveChatHandler={() => {
						archiveChatHandler(chat.id);
					}}
					renameHandler={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
236
237
						chatTitle = chat.title;

Timothy J. Baek's avatar
Timothy J. Baek committed
238
239
240
						confirmEdit = true;
					}}
					deleteHandler={() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
241
						dispatch('delete');
Timothy J. Baek's avatar
Timothy J. Baek committed
242
243
					}}
					onClose={() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
244
						dispatch('unselect');
Timothy J. Baek's avatar
Timothy J. Baek committed
245
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
246
247
248
					on:change={async () => {
						await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
					}}
Timothy J. Baek's avatar
Timothy J. Baek committed
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
				>
					<button
						aria-label="Chat Menu"
						class=" self-center dark:hover:text-white transition"
						on:click={() => {
							dispatch('select');
						}}
					>
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								d="M2 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM6.5 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM12.5 6.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
							/>
						</svg>
					</button>
				</ChatMenu>

				{#if chat.id === $chatId}
					<!-- Shortcut support using "delete-chat-button" id -->
					<button
						id="delete-chat-button"
						class="hidden"
						on:click={() => {
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
276
							dispatch('delete');
Timothy J. Baek's avatar
Timothy J. Baek committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
						}}
					>
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 16 16"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
								d="M2 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM6.5 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM12.5 6.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
							/>
						</svg>
					</button>
				{/if}
			</div>
		{/if}
	</div>
</div>