LiveAtlas/src/components/ChatBox.vue

216 lines
6.0 KiB
Vue
Raw Normal View History

2020-12-17 14:50:12 +00:00
<!--
2021-07-25 14:12:40 +00:00
- Copyright 2021 James Lyne
2020-12-17 14:50:12 +00:00
-
2021-07-25 14:12:40 +00:00
- 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
2020-12-17 14:50:12 +00:00
-
2021-07-25 14:12:40 +00:00
- http://www.apache.org/licenses/LICENSE-2.0
2020-12-17 14:50:12 +00:00
-
2021-07-25 14:12:40 +00:00
- 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.
2020-12-17 14:50:12 +00:00
-->
<template>
<section class="chat">
<ul class="chat__messages" role="log" aria-live="polite" aria-relevant="additions">
2021-05-20 15:13:25 +00:00
<ChatMessage v-for="message in chatMessages" :key="message.timestamp" :message="message"></ChatMessage>
<li v-if="!chatMessages.length" class="message message--skeleton" role="none">{{ messageNoMessages }}</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 role="alert" v-if="sendingError" class="chat__error">{{ sendingError }}</div>
2021-05-20 15:13:25 +00:00
<input ref="chatInput" v-model="enteredMessage" class="chat__input" type="text" :maxlength="maxMessageLength"
2021-05-20 15:39:41 +00:00
:placeholder="messagePlaceholder" :disabled="sendingMessage">
2021-05-20 15:13:25 +00:00
<button class="chat__send" :disabled="!enteredMessage || sendingMessage">{{ messageSend }}</button>
2021-01-07 22:40:05 +00:00
</form>
<button type="button" v-if="loginRequired" class="chat__login" @click="login">{{ messageLogin }}</button>
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";
import {MutationTypes} from "@/store/mutation-types";
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(),
componentSettings = computed(() => store.state.components.chatBox),
2021-01-07 22:40:05 +00:00
chatBoxVisible = computed(() => store.state.ui.visibleElements.has('chat')),
2021-01-24 22:35:59 +00:00
loginRequired = computed(() => {
return store.state.components.chatSending && store.state.components.chatSending.loginRequired
&& !store.state.loggedIn;
2021-01-07 22:40:05 +00:00
}),
2021-01-24 22:35:59 +00:00
sendingEnabled = computed(() => store.state.components.chatSending && !loginRequired.value),
2021-05-20 15:13:25 +00:00
maxMessageLength = computed(() => store.state.components.chatSending?.maxLength),
2021-01-07 22:40:05 +00:00
chatInput = ref<HTMLInputElement | null>(null),
enteredMessage = ref<string>(""),
sendingMessage = ref<boolean>(false),
sendingError = ref<string | null>(null),
2021-05-20 15:13:25 +00:00
chatMessages = computed(() => {
if(componentSettings.value!.messageHistory) {
2021-01-07 22:39:26 +00:00
return store.state.chat.messages.slice(0, componentSettings.value!.messageHistory);
} else {
return store.state.chat.messages;
}
2021-01-07 22:40:05 +00:00
}),
2021-05-20 15:13:25 +00:00
messageSend = computed(() => store.state.messages.chatSend),
2021-05-20 15:39:41 +00:00
messagePlaceholder = computed(() => store.state.messages.chatPlaceholder),
2021-05-20 15:13:25 +00:00
messageNoMessages = computed(() => store.state.messages.chatNoMessages),
messageLogin = computed(() => store.state.messages.chatLogin),
2021-05-20 15:13:25 +00:00
2021-05-18 17:24:35 +00:00
sendMessage = async () => {
2021-05-20 15:13:25 +00:00
const message = enteredMessage.value.trim().substring(0, maxMessageLength.value);
2021-01-07 22:40:05 +00:00
if(!message) {
return;
}
sendingMessage.value = true;
sendingError.value = null;
2021-05-18 17:41:13 +00:00
2021-05-18 17:24:35 +00:00
try {
await store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message);
2021-01-07 22:40:05 +00:00
enteredMessage.value = "";
sendingError.value = null;
2021-05-18 17:24:35 +00:00
} catch(e) {
2021-01-07 22:40:05 +00:00
if(e instanceof ChatError) {
sendingError.value = e.message;
} else {
2021-05-20 15:13:25 +00:00
sendingError.value = store.state.messages.chatErrorUnknown;
2021-01-07 22:40:05 +00:00
}
2021-05-18 17:24:35 +00:00
} finally {
2021-01-07 22:40:05 +00:00
sendingMessage.value = false;
requestAnimationFrame(() => chatInput.value!.focus());
2021-05-18 17:24:35 +00:00
}
},
login = () => store.commit(MutationTypes.SHOW_UI_MODAL, 'login');
2021-01-07 22:40:05 +00:00
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,
login,
2021-05-20 15:13:25 +00:00
chatMessages,
2021-01-24 22:35:59 +00:00
loginRequired,
2021-01-07 22:40:05 +00:00
sendingEnabled,
sendingMessage,
sendingError,
2021-05-20 15:13:25 +00:00
maxMessageLength,
messageLogin,
messageSend,
messageNoMessages,
2021-05-20 15:39:41 +00:00
messagePlaceholder,
2020-12-17 14:50:12 +00:00
}
}
})
</script>
<style lang="scss">
2020-12-31 13:05:54 +00:00
@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: calc((var(--ui-element-spacing) * 2) + var(--ui-button-size));
left: calc((var(--ui-element-spacing) * 2) + var(--ui-button-size));
2020-12-31 13:05:54 +00:00
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;
2021-05-15 22:24:29 +00:00
color: var(--text-subtle);
2020-12-31 22:29:43 +00:00
}
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 {
2021-05-15 22:24:29 +00:00
border-bottom-left-radius: var(--border-radius);
2021-01-07 22:40:05 +00:00
flex-grow: 1;
}
.chat__send {
padding-left: 1rem;
padding-right: 1rem;
2021-05-15 22:24:29 +00:00
border-radius: 0 0 var(--border-radius) 0;
2021-01-07 22:40:05 +00:00
}
.chat__error {
2021-05-15 22:24:29 +00:00
background-color: var(--background-error);
color: var(--text-emphasis);
2021-01-07 22:40:05 +00:00
font-size: 1.6rem;
padding: 0.5rem 1rem;
line-height: 2rem;
width: 100%;
}
}
2021-01-24 22:35:59 +00:00
.chat__login {
font-size: 1.6rem;
padding: 1.2rem;
2021-05-15 22:24:29 +00:00
background-color: var(--background-light);
color: var(--text-subtle);
2021-01-24 22:35:59 +00:00
margin: 1.5rem -1.5rem -1.5rem;
text-align: left;
border-top-left-radius: 0;
border-top-right-radius: 0;
2021-01-24 22:35:59 +00:00
}
@media (max-width: 400px), (max-height: 480px) {
2020-12-31 13:05:54 +00:00
max-width: calc(100% - 7rem);
}
@media (max-width: 320px) {
2020-12-31 13:05:54 +00:00
.chat__messages .message + .message {
margin-bottom: 0.7rem;
}
}
}
2021-07-25 14:12:40 +00:00
</style>