94 lines
2.6 KiB
Svelte
94 lines
2.6 KiB
Svelte
<script>
|
|
import { HashIcon } from "svelte-feather-icons";
|
|
import request from "../../../request";
|
|
import { apiRoute } from "../../../storage";
|
|
import { messagesStoreProvider, userInfoStore } from "../../../stores";
|
|
import Messages from "./Messages.svelte";
|
|
|
|
export let channel;
|
|
let messageInput = "";
|
|
|
|
$: messages = messagesStoreProvider.getStore(channel.id);
|
|
|
|
const onKeydown = async (e) => {
|
|
if (e.code !== "Enter")
|
|
return;
|
|
|
|
if (messageInput.trim() === "" || !$userInfoStore)
|
|
return;
|
|
|
|
// optimistically add message to store
|
|
const optimisticMessageId = Math.floor(Math.random() * 999999);
|
|
const optimisticMessage = {
|
|
id: optimisticMessageId,
|
|
content: messageInput,
|
|
channel_id: channel.id,
|
|
author_id: $userInfoStore.id,
|
|
author_username: $userInfoStore.username,
|
|
createdAt: Date.now().toString()
|
|
};
|
|
messages.addMessage(optimisticMessage);
|
|
|
|
const res = await request("POST", apiRoute(`channels/${channel.id}/messages`), true, {
|
|
content: messageInput
|
|
});
|
|
|
|
if (res.success && res.ok) {
|
|
messages.updateId(optimisticMessageId, res.json.id);
|
|
} else {
|
|
messages.deleteMessage({
|
|
id: optimisticMessageId
|
|
});
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<style>
|
|
.main-container {
|
|
display: flex;
|
|
flex-direction: column;
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
|
|
.top-bar {
|
|
height: 3.4em;
|
|
width: 100%;
|
|
padding: var(--space-xs);
|
|
background-color: var(--background-color-1);
|
|
display: flex;
|
|
align-items: center;
|
|
border-bottom: 1px solid var(--background-color-2);
|
|
}
|
|
|
|
.channel-heading {
|
|
margin-left: var(--space-xxs);
|
|
}
|
|
|
|
.message-input-container {
|
|
width: 100%;
|
|
padding: var(--space-md);
|
|
background-color: var(--background-color-1);
|
|
}
|
|
|
|
.message-input {
|
|
height: 3em;
|
|
width: 100%;
|
|
background-color : var(--background-color-2);
|
|
border: none;
|
|
color: currentColor;
|
|
border-radius: var(--radius-md);
|
|
padding: var(--space-sm);
|
|
}
|
|
</style>
|
|
|
|
<div class="main-container">
|
|
<div class="top-bar">
|
|
<HashIcon />
|
|
<span class="h5 channel-heading">{ channel.name }</span>
|
|
</div>
|
|
<Messages channelId="{ channel.id }" />
|
|
<div class="message-input-container">
|
|
<input type="text" class="message-input" on:keydown={ onKeydown } bind:value={ messageInput }>
|
|
</div>
|
|
</div>
|