File size: 3,293 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
<script lang="ts">
	import { createEventDispatcher } from 'svelte';
	import type { MessageMode } from '$lib/types/enums';
	import ModeSelector from './ModeSelector.svelte';
	import { validateMessageContent } from '$lib/utils/validators';

	export let disabled = false;
	export let loading = false;

	const dispatch = createEventDispatcher<{
		send: { content: string; mode: MessageMode };
	}>();

	let content = '';
	let selectedMode: MessageMode = 'chat';
	let error: string | null = null;

	function handleKeyDown(event: KeyboardEvent) {
		// Cmd/Ctrl + Enter to send
		if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
			event.preventDefault();
			handleSubmit();
		}
	}

	function handleSubmit() {
		error = null;

		// Validate content
		const validationError = validateMessageContent(content);
		if (validationError) {
			error = validationError;
			return;
		}

		// Dispatch send event
		dispatch('send', {
			content: content.trim(),
			mode: selectedMode
		});

		// Clear input
		content = '';
	}

	$: canSend = content.trim().length > 0 && !loading && !disabled;
</script>

<div class="message-input border-top bg-white p-2 p-md-3">
	{#if error}
		<div class="alert alert-danger alert-sm mb-2" role="alert">
			<i class="bi bi-exclamation-triangle-fill me-2"></i>
			{error}
		</div>
	{/if}

	<div class="input-container">
		<!-- Mode Selector -->
		<div class="mb-2">
			<ModeSelector bind:selectedMode disabled={loading || disabled} />
		</div>

		<!-- Message Input -->
		<div class="d-flex gap-2">
			<textarea
				class="form-control"
				placeholder="Type your message..."
				bind:value={content}
				on:keydown={handleKeyDown}
				{disabled}
				rows="2"
				maxlength="10000"
			></textarea>

			<button
				type="button"
				class="btn btn-primary send-btn"
				on:click={handleSubmit}
				disabled={!canSend}
				title="Send message"
			>
				{#if loading}
					<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
				{:else}
					<i class="bi bi-send-fill"></i>
				{/if}
			</button>
		</div>

		<!-- Character Counter -->
		<div class="d-flex justify-content-between align-items-center mt-2">
			<small class="text-muted d-none d-sm-block">
				<i class="bi bi-info-circle me-1"></i>
				<span class="d-none d-md-inline">Press Cmd/Ctrl+Enter to send</span>
				<span class="d-inline d-md-none">Cmd/Ctrl+Enter</span>
			</small>
			<small class="text-muted ms-auto">
				{content.length} / 10k
			</small>
		</div>
	</div>
</div>

<style>
	.message-input {
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
	}

	.input-container {
		max-width: 100%;
	}

	textarea {
		resize: vertical;
		min-height: 60px;
		max-height: 200px;
		font-size: 1rem;
	}

	.send-btn {
		min-width: 48px;
		height: auto;
		align-self: stretch;
		display: flex;
		align-items: center;
		justify-content: center;
	}

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

	/* Mobile optimizations */
	@media (max-width: 576px) {
		textarea {
			min-height: 50px;
			font-size: 16px; /* Prevents zoom on iOS */
		}

		.send-btn {
			min-width: 44px;
			padding: 0.5rem;
		}

		.message-input {
			padding: 0.5rem !important;
		}
	}

	/* Tablet and up */
	@media (min-width: 768px) {
		textarea {
			min-height: 80px;
		}
	}
</style>