2022-04-23 01:06:04 +03:00
|
|
|
<script>
|
2022-04-27 20:12:04 +03:00
|
|
|
import { SendIcon } from "svelte-feather-icons";
|
2022-04-28 18:48:44 +03:00
|
|
|
import request from "../request";
|
2022-04-26 03:09:16 +03:00
|
|
|
import { apiRoute } from "../storage";
|
2022-04-27 20:12:04 +03:00
|
|
|
import { messagesStoreProvider, overlayStore, smallViewport, userInfoStore } from "../stores";
|
2022-04-23 01:06:04 +03:00
|
|
|
|
|
|
|
export let channel;
|
|
|
|
let messageInput = "";
|
|
|
|
|
|
|
|
$: messages = messagesStoreProvider.getStore(channel.id);
|
|
|
|
|
2022-04-27 20:12:04 +03:00
|
|
|
const sendMessage = async () => {
|
2022-04-23 01:06:04 +03:00
|
|
|
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,
|
|
|
|
created_at: Date.now().toString(),
|
|
|
|
_isPending: true
|
|
|
|
};
|
|
|
|
messages.addMessage(optimisticMessage);
|
|
|
|
messageInput = "";
|
|
|
|
|
|
|
|
const res = await request("POST", apiRoute(`channels/${channel.id}/messages`), true, {
|
|
|
|
content: optimisticMessage.content
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.success && res.ok) {
|
|
|
|
messages.setMessage(optimisticMessageId, res.json);
|
|
|
|
} else {
|
|
|
|
messages.deleteMessage({
|
|
|
|
id: optimisticMessageId
|
|
|
|
});
|
2022-04-26 03:47:51 +03:00
|
|
|
overlayStore.open("toast", {
|
|
|
|
message: "Couldn't send message"
|
|
|
|
});
|
2022-04-23 01:06:04 +03:00
|
|
|
}
|
|
|
|
};
|
2022-04-27 20:12:04 +03:00
|
|
|
|
|
|
|
const onKeydown = async (e) => {
|
|
|
|
if (e.code !== "Enter")
|
|
|
|
return;
|
|
|
|
|
|
|
|
await sendMessage();
|
|
|
|
};
|
2022-04-23 01:06:04 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.message-input-container {
|
2022-04-27 20:12:04 +03:00
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
2022-04-23 01:06:04 +03:00
|
|
|
width: 100%;
|
|
|
|
padding: var(--space-norm);
|
|
|
|
}
|
|
|
|
|
|
|
|
.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);
|
|
|
|
font-size: inherit;
|
|
|
|
line-height: inherit;
|
|
|
|
}
|
|
|
|
|
|
|
|
.message-input::placeholder {
|
|
|
|
color: var(--foreground-color-3);
|
|
|
|
}
|
2022-04-27 20:12:04 +03:00
|
|
|
|
|
|
|
.send-button {
|
|
|
|
margin-left: var(--space-sm);
|
|
|
|
}
|
2022-04-23 01:06:04 +03:00
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="message-input-container">
|
2022-04-27 05:03:47 +03:00
|
|
|
<input
|
|
|
|
placeholder={`Send something interesting to #${channel.name}`}
|
|
|
|
type="text"
|
|
|
|
class="message-input"
|
|
|
|
on:keydown={ onKeydown }
|
|
|
|
bind:value={ messageInput }
|
|
|
|
>
|
2022-04-27 20:12:04 +03:00
|
|
|
{#if $smallViewport}
|
|
|
|
<button class="icon-button send-button" on:click="{ sendMessage }">
|
|
|
|
<SendIcon />
|
|
|
|
</button>
|
|
|
|
{/if}
|
2022-04-23 01:06:04 +03:00
|
|
|
</div>
|