2022-04-16 04:17:48 +03:00
|
|
|
<script>
|
|
|
|
import { HashIcon } from "svelte-feather-icons";
|
2022-04-20 03:44:48 +03:00
|
|
|
import request from "../../../request";
|
2022-04-20 17:49:31 +03:00
|
|
|
import { apiRoute } from "../../../storage";
|
2022-04-20 03:44:48 +03:00
|
|
|
import { messagesStoreProvider, userInfoStore } from "../../../stores";
|
|
|
|
import Messages from "./Messages.svelte";
|
2022-04-17 04:08:40 +03:00
|
|
|
|
|
|
|
export let channel;
|
2022-04-20 03:44:48 +03:00
|
|
|
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,
|
2022-04-21 01:55:37 +03:00
|
|
|
created_at: Date.now().toString(),
|
|
|
|
_isPending: true
|
2022-04-20 03:44:48 +03:00
|
|
|
};
|
|
|
|
messages.addMessage(optimisticMessage);
|
2022-04-20 17:49:31 +03:00
|
|
|
messageInput = "";
|
2022-04-20 03:44:48 +03:00
|
|
|
|
|
|
|
const res = await request("POST", apiRoute(`channels/${channel.id}/messages`), true, {
|
2022-04-20 17:49:31 +03:00
|
|
|
content: optimisticMessage.content
|
2022-04-20 03:44:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (res.success && res.ok) {
|
2022-04-21 01:55:37 +03:00
|
|
|
messages.setMessage(optimisticMessageId, res.json);
|
2022-04-20 03:44:48 +03:00
|
|
|
} else {
|
|
|
|
messages.deleteMessage({
|
|
|
|
id: optimisticMessageId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2022-04-16 04:17:48 +03:00
|
|
|
</script>
|
|
|
|
|
2022-04-15 02:39:13 +03:00
|
|
|
<style>
|
|
|
|
.main-container {
|
2022-04-22 23:01:44 +03:00
|
|
|
background-color: var(--background-color-1);
|
|
|
|
overflow: hidden;
|
2022-04-15 02:39:13 +03:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.top-bar {
|
2022-04-16 04:17:48 +03:00
|
|
|
height: 3.4em;
|
2022-04-15 02:39:13 +03:00
|
|
|
width: 100%;
|
2022-04-16 04:17:48 +03:00
|
|
|
padding: var(--space-xs);
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
border-bottom: 1px solid var(--background-color-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
.channel-heading {
|
|
|
|
margin-left: var(--space-xxs);
|
2022-04-15 02:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
.message-input-container {
|
|
|
|
width: 100%;
|
2022-04-22 23:01:44 +03:00
|
|
|
padding: var(--space-norm);
|
2022-04-16 04:17:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
.message-input {
|
2022-04-17 04:08:40 +03:00
|
|
|
height: 3em;
|
2022-04-16 04:17:48 +03:00
|
|
|
width: 100%;
|
|
|
|
background-color : var(--background-color-2);
|
|
|
|
border: none;
|
|
|
|
color: currentColor;
|
|
|
|
border-radius: var(--radius-md);
|
|
|
|
padding: var(--space-sm);
|
2022-04-15 02:39:13 +03:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="main-container">
|
|
|
|
<div class="top-bar">
|
2022-04-16 04:17:48 +03:00
|
|
|
<HashIcon />
|
2022-04-17 04:08:40 +03:00
|
|
|
<span class="h5 channel-heading">{ channel.name }</span>
|
2022-04-15 02:39:13 +03:00
|
|
|
</div>
|
2022-04-20 03:14:28 +03:00
|
|
|
<Messages channelId="{ channel.id }" />
|
2022-04-15 02:39:13 +03:00
|
|
|
<div class="message-input-container">
|
2022-04-20 03:44:48 +03:00
|
|
|
<input type="text" class="message-input" on:keydown={ onKeydown } bind:value={ messageInput }>
|
2022-04-15 02:39:13 +03:00
|
|
|
</div>
|
|
|
|
</div>
|