refactored the sned and clear messages button
This commit is contained in:
parent
5b7f46a414
commit
90e4718df3
14
src/lib/components/ClearMessages.svelte
Normal file
14
src/lib/components/ClearMessages.svelte
Normal file
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import { clearMessages } from '$lib/services/messages';
|
||||
|
||||
function handleClear() {
|
||||
clearMessages();
|
||||
}
|
||||
</script>
|
||||
|
||||
<button
|
||||
class="px-4 py-2 ml-2 text-white bg-red-500 rounded hover:bg-red-600"
|
||||
on:click={handleClear}
|
||||
>
|
||||
Clear
|
||||
</button>
|
@ -1,57 +1,12 @@
|
||||
<script>
|
||||
import { createMessage, clearMessages } from '$lib/services/messages';
|
||||
import SendMessage from './SendMessage.svelte';
|
||||
import ClearMessages from './ClearMessages.svelte';
|
||||
|
||||
let newMessageText = '';
|
||||
|
||||
// Default message composite structure
|
||||
const messageComposite = {
|
||||
layout: '',
|
||||
children: [
|
||||
{
|
||||
componentName: 'HelloEarth',
|
||||
props: {}
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
function handleSend() {
|
||||
if (newMessageText.trim() !== '') {
|
||||
// Default message without composite
|
||||
const message = {
|
||||
text: newMessageText,
|
||||
sender: 'user',
|
||||
type: 'chat'
|
||||
};
|
||||
|
||||
// Check if text contains @app:command and add the composite accordingly
|
||||
const appCommandPattern = /@app:(\w+)/;
|
||||
const match = newMessageText.match(appCommandPattern);
|
||||
if (match && match[1]) {
|
||||
message.composite = {
|
||||
layout: '',
|
||||
children: [
|
||||
{
|
||||
componentName: match[1], // Matched component name
|
||||
props: {}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
createMessage(message);
|
||||
|
||||
// Clear the input after sending the message
|
||||
newMessageText = '';
|
||||
}
|
||||
}
|
||||
|
||||
function handleClear() {
|
||||
clearMessages();
|
||||
}
|
||||
|
||||
function handleKeyDown(event) {
|
||||
if (event.key === 'Enter') {
|
||||
handleSend();
|
||||
newMessageText = ''; // Clear the input after sending the message
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
@ -65,13 +20,6 @@
|
||||
bind:value={newMessageText}
|
||||
on:keydown={handleKeyDown}
|
||||
/>
|
||||
<button class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600" on:click={handleSend}>
|
||||
Send Message
|
||||
</button>
|
||||
<button
|
||||
class="px-4 py-2 ml-2 text-white bg-red-500 rounded hover:bg-red-600"
|
||||
on:click={handleClear}
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
<SendMessage bind:newMessageText />
|
||||
<ClearMessages />
|
||||
</footer>
|
||||
|
34
src/lib/components/SendMessage.svelte
Normal file
34
src/lib/components/SendMessage.svelte
Normal file
@ -0,0 +1,34 @@
|
||||
<script>
|
||||
import { createMessage } from '$lib/services/messages';
|
||||
export let newMessageText = '';
|
||||
|
||||
function handleSend() {
|
||||
if (newMessageText.trim() !== '') {
|
||||
const message = {
|
||||
text: newMessageText,
|
||||
sender: 'user',
|
||||
type: 'chat'
|
||||
};
|
||||
|
||||
const appCommandPattern = /@app:(\w+)/;
|
||||
const match = newMessageText.match(appCommandPattern);
|
||||
if (match && match[1]) {
|
||||
message.composite = {
|
||||
layout: '',
|
||||
children: [
|
||||
{
|
||||
componentName: match[1], // Matched component name
|
||||
props: {}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
createMessage(message);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<button class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600" on:click={handleSend}>
|
||||
Send Message
|
||||
</button>
|
Loading…
Reference in New Issue
Block a user