2020-12-17 14:50:12 +00:00
|
|
|
<!--
|
|
|
|
- Copyright 2020 James Lyne
|
|
|
|
-
|
|
|
|
- Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
- you may not use this file except in compliance with the License.
|
|
|
|
- You may obtain a copy of the License at
|
|
|
|
-
|
|
|
|
- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
-
|
|
|
|
- Unless required by applicable law or agreed to in writing, software
|
|
|
|
- distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
- See the License for the specific language governing permissions and
|
|
|
|
- limitations under the License.
|
|
|
|
-->
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<section class="chat">
|
|
|
|
<ul class="chat__messages">
|
2020-12-31 22:29:43 +00:00
|
|
|
<ChatMessage v-for="message in messages" :key="message.timestamp" :message="message"></ChatMessage>
|
|
|
|
<li v-if="!messages.length" class="message message--skeleton">No chat messages yet...</li>
|
2020-12-17 14:50:12 +00:00
|
|
|
</ul>
|
2021-01-07 22:40:05 +00:00
|
|
|
<form v-if="sendingEnabled" class="chat__form" @submit.prevent="sendMessage">
|
|
|
|
<div v-if="sendingError" class="chat__error">{{ sendingError }}</div>
|
|
|
|
<input ref="chatInput" v-model="enteredMessage" class="chat__input" type="text" :maxlength="messageMaxLength"
|
|
|
|
placeholder="Type your chat message here..." :disabled="sendingMessage">
|
|
|
|
<button class="chat__send" :disabled="!enteredMessage || sendingMessage">Send</button>
|
|
|
|
</form>
|
2020-12-17 14:50:12 +00:00
|
|
|
</section>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-01-07 22:40:05 +00:00
|
|
|
import {defineComponent, ref, computed, watch} from "@vue/runtime-core";
|
2020-12-17 14:50:12 +00:00
|
|
|
import {useStore} from "@/store";
|
2020-12-31 13:05:54 +00:00
|
|
|
import ChatMessage from "@/components/chat/ChatMessage.vue";
|
2021-01-07 22:40:05 +00:00
|
|
|
import {ActionTypes} from "@/store/action-types";
|
|
|
|
import ChatError from "@/errors/ChatError";
|
2020-12-17 14:50:12 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2020-12-31 13:05:54 +00:00
|
|
|
components: {
|
|
|
|
ChatMessage
|
|
|
|
},
|
2020-12-17 14:50:12 +00:00
|
|
|
setup() {
|
|
|
|
const store = useStore(),
|
2021-01-06 16:10:56 +00:00
|
|
|
componentSettings = computed(() => store.state.components.chatBox),
|
2021-01-07 22:40:05 +00:00
|
|
|
chatBoxVisible = computed(() => store.state.ui.visibleElements.has('chat')),
|
|
|
|
|
|
|
|
sendingEnabled = computed(() => {
|
|
|
|
return store.state.components.chatSending &&
|
|
|
|
(!store.state.components.chatSending.loginRequired || store.state.loggedIn);
|
|
|
|
}),
|
|
|
|
messageMaxLength = computed(() => store.state.components.chatSending?.maxLength),
|
|
|
|
|
|
|
|
chatInput = ref<HTMLInputElement | null>(null),
|
|
|
|
enteredMessage = ref<string>(""),
|
|
|
|
sendingMessage = ref<boolean>(false),
|
|
|
|
sendingError = ref<string | null>(null),
|
|
|
|
|
2021-01-06 16:10:56 +00:00
|
|
|
messages = computed(() => {
|
|
|
|
if(componentSettings.value!.messageHistory) {
|
2021-01-07 22:39:26 +00:00
|
|
|
return store.state.chat.messages.slice(0, componentSettings.value!.messageHistory);
|
2021-01-06 16:10:56 +00:00
|
|
|
} else {
|
|
|
|
return store.state.chat.messages;
|
|
|
|
}
|
2021-01-07 22:40:05 +00:00
|
|
|
}),
|
|
|
|
|
|
|
|
sendMessage = () => {
|
|
|
|
const message = enteredMessage.value.trim().substring(0, messageMaxLength.value);
|
|
|
|
|
|
|
|
if(!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendingMessage.value = true;
|
|
|
|
sendingError.value = null;
|
|
|
|
|
|
|
|
store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message).then(() => {
|
|
|
|
enteredMessage.value = "";
|
|
|
|
sendingError.value = null;
|
|
|
|
}).catch(e => {
|
|
|
|
if(e instanceof ChatError) {
|
|
|
|
sendingError.value = e.message;
|
|
|
|
} else {
|
|
|
|
sendingError.value = `An unexpected error occurred. See console for details.`;
|
|
|
|
}
|
|
|
|
}).finally(() => {
|
|
|
|
sendingMessage.value = false;
|
|
|
|
|
|
|
|
requestAnimationFrame(() => chatInput.value!.focus());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
watch(chatBoxVisible, newValue => {
|
|
|
|
if(newValue && sendingEnabled.value) {
|
|
|
|
requestAnimationFrame(() => chatInput.value!.focus());
|
|
|
|
}
|
|
|
|
});
|
2020-12-17 14:50:12 +00:00
|
|
|
|
|
|
|
return {
|
2021-01-07 22:40:05 +00:00
|
|
|
chatInput,
|
|
|
|
enteredMessage,
|
|
|
|
sendMessage,
|
2020-12-31 22:29:43 +00:00
|
|
|
messages,
|
2021-01-07 22:40:05 +00:00
|
|
|
sendingEnabled,
|
|
|
|
sendingMessage,
|
|
|
|
sendingError,
|
|
|
|
messageMaxLength
|
2020-12-17 14:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2020-12-31 13:05:54 +00:00
|
|
|
@import '../scss/variables';
|
|
|
|
@import '../scss/placeholders';
|
2020-12-17 14:50:12 +00:00
|
|
|
|
2020-12-31 13:05:54 +00:00
|
|
|
.chat {
|
|
|
|
@extend %panel;
|
|
|
|
position: absolute;
|
|
|
|
bottom: 7rem;
|
|
|
|
left: 7rem;
|
|
|
|
width: 50rem;
|
|
|
|
max-width: calc(100% - 8rem);
|
|
|
|
max-height: 20rem;
|
|
|
|
display: flex;
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
|
|
.chat__messages {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column-reverse;
|
|
|
|
list-style: none;
|
|
|
|
overflow: auto;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
|
|
|
|
.message {
|
|
|
|
font-size: 1.6rem;
|
2020-12-31 22:29:43 +00:00
|
|
|
line-height: 1.9rem;
|
2020-12-31 13:05:54 +00:00
|
|
|
|
|
|
|
& + .message {
|
|
|
|
margin-bottom: 0.5rem
|
|
|
|
}
|
2020-12-31 22:29:43 +00:00
|
|
|
|
|
|
|
&.message--skeleton {
|
|
|
|
font-style: italic;
|
|
|
|
color: #aaaaaa;
|
|
|
|
}
|
2020-12-31 13:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 22:40:05 +00:00
|
|
|
.chat__form {
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
align-items: stretch;
|
|
|
|
margin: 1.5rem -1.5rem -1.5rem;
|
|
|
|
|
|
|
|
.chat__input {
|
|
|
|
border-bottom-left-radius: $global-border-radius;
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat__send {
|
|
|
|
padding-left: 1rem;
|
|
|
|
padding-right: 1rem;
|
|
|
|
border-radius: 0 0 $global-border-radius 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.chat__error {
|
|
|
|
background-color: #771616;
|
|
|
|
color: #eeeeee;
|
|
|
|
font-size: 1.6rem;
|
|
|
|
padding: 0.5rem 1rem;
|
|
|
|
line-height: 2rem;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:05:54 +00:00
|
|
|
@media (max-width: 25rem), (max-height: 30rem) {
|
|
|
|
bottom: 6.5rem;
|
|
|
|
left: 6.5rem;
|
|
|
|
max-width: calc(100% - 7rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 20rem) {
|
|
|
|
.chat__messages .message + .message {
|
|
|
|
margin-bottom: 0.7rem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 14:50:12 +00:00
|
|
|
</style>
|