File size: 8,347 Bytes
1fff71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
299
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
<script lang="ts">
	import { sessionStore, activeSessions, sessionCount } from '$lib/stores/session';
	import { formatRelativeTime } from '$lib/utils/formatters';
	import { validateSessionTitle } from '$lib/utils/validators';
	import { SESSION_LIMIT, SESSION_LIMIT_WARNING } from '$lib/utils/constants';
	import { goto } from '$app/navigation';

	// Modal state
	let showCreateModal = false;
	let showDeleteModal = false;
	let deleteSessionId: string | null = null;
	let deleteSessionTitle = '';

	// Form state
	let newSessionTitle = '';
	let titleError = '';
	let creating = false;

	function openCreateModal() {
		newSessionTitle = '';
		titleError = '';
		showCreateModal = true;
	}

	function closeCreateModal() {
		showCreateModal = false;
		newSessionTitle = '';
		titleError = '';
	}

	async function handleCreateSession() {
		// Validate title
		const validationError = validateSessionTitle(newSessionTitle);
		if (validationError) {
			titleError = validationError;
			return;
		}

		creating = true;
		const sessionId = await sessionStore.createSession(newSessionTitle);
		creating = false;

		if (sessionId) {
			closeCreateModal();
			// Navigate to new session
			goto(`/session/${sessionId}`);
		}
	}

	function openDeleteModal(sessionId: string, title: string) {
		deleteSessionId = sessionId;
		deleteSessionTitle = title;
		showDeleteModal = true;
	}

	function closeDeleteModal() {
		showDeleteModal = false;
		deleteSessionId = null;
		deleteSessionTitle = '';
	}

	async function handleDeleteSession() {
		if (!deleteSessionId) return;

		const success = await sessionStore.deleteSession(deleteSessionId);
		if (success) {
			closeDeleteModal();
		}
	}

	function handleSessionClick(sessionId: string) {
		goto(`/session/${sessionId}`);
	}

	// Check if at limit
	$: atLimit = $sessionCount >= SESSION_LIMIT;
	$: nearLimit = $sessionCount >= SESSION_LIMIT_WARNING;
</script>

<div class="session-list h-100 d-flex flex-column">
	<!-- Header -->
	<div class="session-list-header p-2 p-md-3 border-bottom bg-light">
		<div class="d-flex justify-content-between align-items-center mb-2">
			<h5 class="mb-0 fs-6 fs-md-5">
				<i class="bi bi-chat-left-dots me-2"></i>
				<span class="d-none d-sm-inline">Sessions</span>
			</h5>
			<button
				class="btn btn-primary btn-sm"
				on:click={openCreateModal}
				disabled={atLimit}
				title={atLimit ? 'Session limit reached' : 'Create new session'}
			>
				<i class="bi bi-plus-lg"></i>
				<span class="d-none d-lg-inline ms-1">New</span>
			</button>
		</div>

		{#if nearLimit}
			<div class="alert alert-{atLimit ? 'danger' : 'warning'} alert-sm mb-0 py-1 px-2" role="alert">
				<small>
					{#if atLimit}
						<i class="bi bi-exclamation-triangle-fill me-1"></i>
						Session limit reached ({$sessionCount}/{SESSION_LIMIT})
					{:else}
						<i class="bi bi-info-circle-fill me-1"></i>
						{$sessionCount}/{SESSION_LIMIT} sessions
					{/if}
				</small>
			</div>
		{/if}
	</div>

	<!-- Session List -->
	<div class="session-list-body flex-grow-1 overflow-auto">
		{#if $activeSessions.length === 0}
			<div class="text-center text-muted p-4">
				<i class="bi bi-chat-dots" style="font-size: 3rem; opacity: 0.3;"></i>
				<p class="mt-2 mb-0">No sessions yet</p>
				<small>Create a session to get started</small>
			</div>
		{:else}
			<div class="list-group list-group-flush">
				{#each $activeSessions as session (session.id)}
					<button
						type="button"
						class="list-group-item list-group-item-action px-2 px-md-3 py-2"
						class:active={session.is_active}
						on:click={() => handleSessionClick(session.id)}
					>
						<div class="d-flex w-100 justify-content-between align-items-start">
							<div class="flex-grow-1 text-start me-2 min-w-0">
								<div class="d-flex align-items-center mb-1">
									<h6 class="mb-0 text-truncate session-title">
										{session.title}
									</h6>
									{#if session.is_reference}
										<span class="badge bg-info ms-2 flex-shrink-0" title="Reference session">
											<i class="bi bi-star-fill"></i>
										</span>
									{/if}
								</div>
								<small class="text-muted d-block text-truncate">
									<span class="d-none d-md-inline">{formatRelativeTime(session.last_interaction)} · </span>
									{session.message_count} msg
								</small>
							</div>
							<button
								type="button"
								class="btn btn-sm btn-outline-danger flex-shrink-0"
								on:click|stopPropagation={() => openDeleteModal(session.id, session.title)}
								title="Delete session"
							>
								<i class="bi bi-trash"></i>
							</button>
						</div>
					</button>
				{/each}
			</div>
		{/if}
	</div>
</div>

<!-- Create Session Modal -->
{#if showCreateModal}
	<div class="modal show d-block" tabindex="-1" role="dialog">
		<div class="modal-dialog modal-dialog-centered" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title">Create New Session</h5>
					<button
						type="button"
						class="btn-close"
						aria-label="Close"
						on:click={closeCreateModal}
					></button>
				</div>
				<div class="modal-body">
					<form on:submit|preventDefault={handleCreateSession}>
						<div class="mb-3">
							<label for="sessionTitle" class="form-label">Session Title</label>
							<input
								type="text"
								class="form-control"
								class:is-invalid={titleError}
								id="sessionTitle"
								bind:value={newSessionTitle}
								placeholder="Enter session title..."
								maxlength="200"
								required
							/>
							{#if titleError}
								<div class="invalid-feedback">{titleError}</div>
							{/if}
							<small class="form-text text-muted">Max 200 characters</small>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" on:click={closeCreateModal}>
						Cancel
					</button>
					<button
						type="button"
						class="btn btn-primary"
						on:click={handleCreateSession}
						disabled={creating || !newSessionTitle.trim()}
					>
						{#if creating}
							<span class="spinner-border spinner-border-sm me-2" role="status"></span>
						{/if}
						Create
					</button>
				</div>
			</div>
		</div>
	</div>
	<div class="modal-backdrop show"></div>
{/if}

<!-- Delete Confirmation Modal -->
{#if showDeleteModal}
	<div class="modal show d-block" tabindex="-1" role="dialog">
		<div class="modal-dialog modal-dialog-centered" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title">Delete Session</h5>
					<button
						type="button"
						class="btn-close"
						aria-label="Close"
						on:click={closeDeleteModal}
					></button>
				</div>
				<div class="modal-body">
					<p>Are you sure you want to delete this session?</p>
					<p class="text-muted mb-0">
						<strong>{deleteSessionTitle}</strong>
					</p>
					<p class="text-danger mt-2 mb-0">
						<i class="bi bi-exclamation-triangle-fill me-2"></i>
						This action cannot be undone.
					</p>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary" on:click={closeDeleteModal}>
						Cancel
					</button>
					<button type="button" class="btn btn-danger" on:click={handleDeleteSession}>
						<i class="bi bi-trash me-2"></i>
						Delete
					</button>
				</div>
			</div>
		</div>
	</div>
	<div class="modal-backdrop show"></div>
{/if}

<style>
	.session-list {
		background-color: #f8f9fa;
	}

	.session-list-body {
		position: relative;
	}

	.list-group-item {
		cursor: pointer;
		border-left: 3px solid transparent;
		transition: all 0.2s ease;
	}

	.list-group-item:hover {
		border-left-color: #0d6efd;
	}

	.list-group-item.active {
		border-left-color: #0d6efd;
		background-color: #e7f1ff;
		color: #000;
	}

	.session-title {
		max-width: 100%;
		font-size: 0.95rem;
	}

	.min-w-0 {
		min-width: 0;
	}

	.modal {
		background-color: rgba(0, 0, 0, 0.5);
	}

	.alert-sm {
		font-size: 0.875rem;
	}

	/* Mobile optimizations */
	@media (max-width: 576px) {
		.session-title {
			font-size: 0.875rem;
		}

		.list-group-item {
			border-left-width: 2px;
		}

		.badge {
			font-size: 0.7rem;
			padding: 0.2rem 0.35rem;
		}
	}

	/* Larger screens */
	@media (min-width: 1200px) {
		.session-title {
			font-size: 1rem;
		}
	}
</style>