48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { computed, ref, watch } from "vue";
|
|
|
|
import { isLoadingSession, messages, type ChatMessage } from "./state";
|
|
|
|
/**
|
|
* Counts the replies the operator has not seen. A reply is seen once the
|
|
* conversation is open and scrolled to its end; one that lands before that,
|
|
* such as a finished task's outcome while the dock is collapsed or the reader
|
|
* is scrolled up, adds to the count until then.
|
|
*
|
|
* It counts replies by position, not by id: a streamed reply is appended
|
|
* locally without an id and a later reload returns it with one, and that must
|
|
* not count twice.
|
|
*/
|
|
const dockOpen = ref(false);
|
|
const atEnd = ref(false);
|
|
const seen = ref(0);
|
|
|
|
function isReply(message: ChatMessage): boolean {
|
|
// Cards the operator's own command produced are not something Archie said.
|
|
return (message.from ?? message.From ?? "") !== "web" && !message.update && !message.choose;
|
|
}
|
|
|
|
const total = computed(() => messages.value.filter(isReply).length);
|
|
const viewing = computed(() => dockOpen.value && atEnd.value);
|
|
|
|
export const unseenCount = computed(() => Math.max(0, total.value - seen.value));
|
|
|
|
// Opening or switching a conversation loads it, which is not news; a reload
|
|
// that drops a message must also lower the mark so the next reply still counts.
|
|
watch(
|
|
[total, viewing, isLoadingSession],
|
|
([count, watching, loading], [, , wasLoading]) => {
|
|
if (watching || loading || wasLoading || count < seen.value) seen.value = count;
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
/** The dock reports whether the conversation is open above the composer. */
|
|
export function setDockOpen(open: boolean): void {
|
|
dockOpen.value = open;
|
|
}
|
|
|
|
/** The scroller's jump button reports whether the reader is at the end. */
|
|
export function setAtEnd(end: boolean): void {
|
|
atEnd.value = end;
|
|
}
|