2019-12-31 16:21:34 +00:00
|
|
|
<template>
|
|
|
|
<div id="chat-container" class="window">
|
|
|
|
<div
|
|
|
|
id="chat"
|
|
|
|
:class="{
|
2022-06-19 00:25:21 +00:00
|
|
|
'time-seconds': store.state.settings.showSeconds,
|
|
|
|
'time-12h': store.state.settings.use12hClock,
|
2019-12-31 16:21:34 +00:00
|
|
|
}"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="chat-view"
|
|
|
|
data-type="search-results"
|
|
|
|
aria-label="Search results"
|
|
|
|
role="tabpanel"
|
|
|
|
>
|
2022-06-19 00:25:21 +00:00
|
|
|
<div v-if="network && channel" class="header">
|
2019-12-31 16:21:34 +00:00
|
|
|
<SidebarToggle />
|
|
|
|
<span class="title"
|
2021-04-29 23:07:31 +00:00
|
|
|
>Searching in <span class="channel-name">{{ channel.name }}</span> for</span
|
2019-12-31 16:21:34 +00:00
|
|
|
>
|
2022-06-19 00:25:21 +00:00
|
|
|
<span class="topic">{{ route.query.q }}</span>
|
2019-12-31 16:21:34 +00:00
|
|
|
<MessageSearchForm :network="network" :channel="channel" />
|
2021-04-29 23:49:49 +00:00
|
|
|
<button
|
|
|
|
class="close"
|
|
|
|
aria-label="Close search window"
|
|
|
|
title="Close search window"
|
|
|
|
@click="closeSearch"
|
|
|
|
/>
|
2019-12-31 16:21:34 +00:00
|
|
|
</div>
|
2022-06-19 00:25:21 +00:00
|
|
|
<div v-if="network && channel" class="chat-content">
|
2019-12-31 16:21:34 +00:00
|
|
|
<div ref="chat" class="chat" tabindex="-1">
|
|
|
|
<div v-show="moreResultsAvailable" class="show-more">
|
|
|
|
<button
|
|
|
|
ref="loadMoreButton"
|
|
|
|
:disabled="
|
2022-06-19 00:25:21 +00:00
|
|
|
store.state.messageSearchInProgress || !store.state.isConnected
|
2019-12-31 16:21:34 +00:00
|
|
|
"
|
|
|
|
class="btn"
|
|
|
|
@click="onShowMoreClick"
|
|
|
|
>
|
2022-06-19 00:25:21 +00:00
|
|
|
<span v-if="store.state.messageSearchInProgress">Loading…</span>
|
2019-12-31 16:21:34 +00:00
|
|
|
<span v-else>Show older messages</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
2020-04-23 16:50:45 +00:00
|
|
|
<div
|
2022-06-19 00:25:21 +00:00
|
|
|
v-if="store.state.messageSearchInProgress && !offset"
|
2020-04-23 16:50:45 +00:00
|
|
|
class="search-status"
|
|
|
|
>
|
2020-03-07 11:01:22 +00:00
|
|
|
Searching…
|
2019-12-31 16:21:34 +00:00
|
|
|
</div>
|
2020-04-23 16:50:45 +00:00
|
|
|
<div v-else-if="!messages.length && !offset" class="search-status">
|
2019-12-31 16:21:34 +00:00
|
|
|
No results found.
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
class="messages"
|
|
|
|
role="log"
|
|
|
|
aria-live="polite"
|
|
|
|
aria-relevant="additions"
|
|
|
|
>
|
2022-06-19 00:25:21 +00:00
|
|
|
<div
|
|
|
|
v-for="(message, id) in messages"
|
|
|
|
:key="message.id"
|
|
|
|
class="result"
|
|
|
|
@click="jump(message, id)"
|
|
|
|
>
|
|
|
|
<DateMarker
|
|
|
|
v-if="shouldDisplayDateMarker(message, id)"
|
|
|
|
:key="message.id + '-date'"
|
|
|
|
:message="message"
|
|
|
|
/>
|
|
|
|
<Message
|
|
|
|
:key="message.id"
|
|
|
|
:channel="channel"
|
|
|
|
:network="network"
|
|
|
|
:message="message"
|
|
|
|
:data-id="message.id"
|
|
|
|
/>
|
|
|
|
</div>
|
2019-12-31 16:21:34 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2021-04-12 23:06:30 +00:00
|
|
|
<style>
|
|
|
|
.channel-name {
|
|
|
|
font-weight: 700;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
<script lang="ts">
|
2019-12-31 16:21:34 +00:00
|
|
|
import socket from "../../js/socket";
|
2021-11-18 21:31:20 +00:00
|
|
|
import eventbus from "../../js/eventbus";
|
2019-12-31 16:21:34 +00:00
|
|
|
|
|
|
|
import SidebarToggle from "../SidebarToggle.vue";
|
|
|
|
import Message from "../Message.vue";
|
|
|
|
import MessageSearchForm from "../MessageSearchForm.vue";
|
|
|
|
import DateMarker from "../DateMarker.vue";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {watch, computed, defineComponent, nextTick, ref, onMounted, onUnmounted} from "vue";
|
|
|
|
import type {ClientMessage} from "../../js/types";
|
|
|
|
|
|
|
|
import {useStore} from "../../js/store";
|
|
|
|
import {useRoute, useRouter} from "vue-router";
|
|
|
|
import {switchToChannel} from "../../js/router";
|
2019-12-31 16:21:34 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default defineComponent({
|
2019-12-31 16:21:34 +00:00
|
|
|
name: "SearchResults",
|
|
|
|
components: {
|
|
|
|
SidebarToggle,
|
|
|
|
Message,
|
|
|
|
DateMarker,
|
|
|
|
MessageSearchForm,
|
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
setup() {
|
|
|
|
const store = useStore();
|
|
|
|
const route = useRoute();
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const chat = ref<HTMLDivElement>();
|
|
|
|
|
|
|
|
const loadMoreButton = ref<HTMLButtonElement>();
|
|
|
|
|
|
|
|
const offset = ref(0);
|
|
|
|
const moreResultsAvailable = ref(false);
|
|
|
|
const oldScrollTop = ref(0);
|
|
|
|
const oldChatHeight = ref(0);
|
|
|
|
|
|
|
|
const messages = computed(() => {
|
2022-11-12 15:48:01 +00:00
|
|
|
const results = store.state.messageSearchResults?.results;
|
|
|
|
|
|
|
|
if (!results) {
|
2019-12-31 16:21:34 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-11-12 15:48:01 +00:00
|
|
|
return results;
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const chan = computed(() => {
|
|
|
|
const chanId = parseInt(String(route.params.id || ""), 10);
|
|
|
|
return store.getters.findChannel(chanId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const network = computed(() => {
|
|
|
|
if (!chan.value) {
|
2019-12-31 16:21:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
return chan.value.network;
|
|
|
|
});
|
|
|
|
|
|
|
|
const channel = computed(() => {
|
|
|
|
if (!chan.value) {
|
2019-12-31 16:21:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
return chan.value.channel;
|
|
|
|
});
|
|
|
|
|
|
|
|
const setActiveChannel = () => {
|
|
|
|
if (!chan.value) {
|
|
|
|
return;
|
2020-04-23 16:50:45 +00:00
|
|
|
}
|
2021-11-18 21:31:20 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
store.commit("activeChannel", chan.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeSearch = () => {
|
|
|
|
if (!channel.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switchToChannel(channel.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const shouldDisplayDateMarker = (message: ClientMessage, id: number) => {
|
|
|
|
const previousMessage = messages.value[id - 1];
|
2019-12-31 16:21:34 +00:00
|
|
|
|
|
|
|
if (!previousMessage) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(previousMessage.time).getDay() !== new Date(message.time).getDay();
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
2022-11-12 22:51:07 +00:00
|
|
|
const clearSearchState = () => {
|
2022-06-19 00:25:21 +00:00
|
|
|
offset.value = 0;
|
2022-11-12 22:51:07 +00:00
|
|
|
store.commit("messageSearchInProgress", false);
|
|
|
|
store.commit("messageSearchResults", null);
|
|
|
|
};
|
2020-04-23 16:50:45 +00:00
|
|
|
|
2022-11-12 22:51:07 +00:00
|
|
|
const doSearch = () => {
|
|
|
|
clearSearchState(); // this is a new search, so we need to clear anything before that
|
2019-12-31 16:21:34 +00:00
|
|
|
socket.emit("search", {
|
2022-06-19 00:25:21 +00:00
|
|
|
networkUuid: network.value?.uuid,
|
|
|
|
channelName: channel.value?.name,
|
|
|
|
searchTerm: String(route.query.q || ""),
|
|
|
|
offset: offset.value,
|
2019-12-31 16:21:34 +00:00
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const onShowMoreClick = () => {
|
|
|
|
if (!chat.value) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-23 16:50:45 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
offset.value += 100;
|
|
|
|
store.commit("messageSearchInProgress", true);
|
|
|
|
|
|
|
|
oldScrollTop.value = chat.value.scrollTop;
|
|
|
|
oldChatHeight.value = chat.value.scrollHeight;
|
2020-04-23 16:50:45 +00:00
|
|
|
|
2019-12-31 16:21:34 +00:00
|
|
|
socket.emit("search", {
|
2022-06-19 00:25:21 +00:00
|
|
|
networkUuid: network.value?.uuid,
|
|
|
|
channelName: channel.value?.name,
|
|
|
|
searchTerm: String(route.query.q || ""),
|
2022-11-12 22:16:16 +00:00
|
|
|
offset: offset.value,
|
2019-12-31 16:21:34 +00:00
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const jumpToBottom = async () => {
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
const el = chat.value;
|
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.scrollTop = el.scrollHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
const jump = (message: ClientMessage, id: number) => {
|
2021-01-26 22:51:22 +00:00
|
|
|
// TODO: Implement jumping to messages!
|
|
|
|
// This is difficult because it means client will need to handle a potentially nonlinear message set
|
|
|
|
// (loading IntersectionObserver both before AND after the messages)
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => route.params.id,
|
|
|
|
() => {
|
|
|
|
doSearch();
|
|
|
|
setActiveChannel();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => route.query,
|
|
|
|
() => {
|
|
|
|
doSearch();
|
|
|
|
setActiveChannel();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(messages, async () => {
|
|
|
|
moreResultsAvailable.value = !!(
|
|
|
|
messages.value.length && !(messages.value.length % 100)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!offset.value) {
|
|
|
|
await jumpToBottom();
|
|
|
|
} else {
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
const el = chat.value;
|
|
|
|
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentChatHeight = el.scrollHeight;
|
|
|
|
el.scrollTop = oldScrollTop.value + currentChatHeight - oldChatHeight.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
setActiveChannel();
|
|
|
|
doSearch();
|
|
|
|
|
|
|
|
eventbus.on("escapekey", closeSearch);
|
|
|
|
eventbus.on("re-search", doSearch);
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
eventbus.off("escapekey", closeSearch);
|
|
|
|
eventbus.off("re-search", doSearch);
|
2022-11-12 22:51:07 +00:00
|
|
|
clearSearchState();
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
chat,
|
|
|
|
loadMoreButton,
|
|
|
|
messages,
|
|
|
|
moreResultsAvailable,
|
|
|
|
network,
|
|
|
|
channel,
|
|
|
|
route,
|
|
|
|
offset,
|
|
|
|
store,
|
|
|
|
setActiveChannel,
|
|
|
|
closeSearch,
|
|
|
|
shouldDisplayDateMarker,
|
|
|
|
doSearch,
|
|
|
|
onShowMoreClick,
|
|
|
|
jumpToBottom,
|
|
|
|
jump,
|
|
|
|
};
|
2019-12-31 16:21:34 +00:00
|
|
|
},
|
2022-06-19 00:25:21 +00:00
|
|
|
});
|
2019-12-31 16:21:34 +00:00
|
|
|
</script>
|