Files
archie/ui/src/chat/ChatDock.vue
T

109 lines
4.0 KiB
Vue

<script setup lang="ts">
import { ChevronDown, ChevronUp } from "@lucide/vue";
import { onClickOutside } from "@vueuse/core";
import { onUnmounted, ref, watch } from "vue";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import ChatBar from "./ChatBar.vue";
import ChatComposer from "./ChatComposer.vue";
import ChatTranscript from "./ChatTranscript.vue";
import { composerText, escapeOwnedByLayer, isSending, useChat } from "./state";
/**
* Chat is a bar docked at the bottom of every page, not a route: the operator
* asks Archie about the page they are already on, so navigating must not reset
* it, and it must not replace the work that prompted the question. The agent
* is told which page that is.
*
* It is the composer alone until the operator focuses it or a turn starts, then
* the conversation opens above it. Hiding the conversation leaves the bar. It is
* deliberately not a modal: no scrim, no scroll lock, no focus trap.
*
* It stays mounted, so the session and any turn in flight survive a navigation.
* It is mounted outside the app shell: `.shell`'s backdrop-filter makes it a
* containing block for `position: fixed`, so a bar nested inside would anchor to
* the shell and ride off the bottom of a long page.
*/
useChat();
const expanded = ref(false);
const composer = ref<InstanceType<typeof ChatComposer> | null>(null);
const dock = ref<HTMLElement | null>(null);
// Selects and popovers inside the bar render in a portal, so a click on one is
// outside the bar in the DOM but not for the operator.
onClickOutside(dock, () => (expanded.value = false), {
ignore: ['[data-slot="select-content"]', '[data-slot="popover-content"]', '[data-slot="dropdown-menu-content"]'],
});
watch(isSending, (sending) => {
if (sending) expanded.value = true;
});
function onKeydown(event: KeyboardEvent): void {
// A focused control (the composer dismissing its palette, or the topbar
// search) already handled Escape via preventDefault; collapsing too would
// make that menu impossible to dismiss on its own. A popover portalled out
// of the bar owns Escape by being open, which the flag reports.
if (event.defaultPrevented || event.key !== "Escape" || escapeOwnedByLayer()) return;
expanded.value = false;
}
watch(expanded, (open) => {
if (open) document.addEventListener("keydown", onKeydown);
else document.removeEventListener("keydown", onKeydown);
});
onUnmounted(() => document.removeEventListener("keydown", onKeydown));
function usePrompt(text: string): void {
composerText.value = text;
composer.value?.focus();
}
</script>
<template>
<div class="pointer-events-none fixed inset-x-0 bottom-4 z-40 flex justify-center px-3 max-md:bottom-2">
<aside
ref="dock"
aria-label="Chat with Archie"
:class="
cn(
'pointer-events-auto flex w-full max-w-xl flex-col rounded-lg border transition-[border-color,background-color,box-shadow] duration-200',
expanded ? 'border-border-strong bg-popover shadow-[var(--shadow-md)]' : 'border-transparent bg-transparent',
)
"
>
<ChatBar v-if="expanded">
<Button
variant="ghost"
size="icon-sm"
class="flex-none"
aria-label="Hide conversation"
title="Hide conversation"
@click="expanded = false"
>
<ChevronDown />
</Button>
</ChatBar>
<div v-if="expanded" class="flex h-[min(28rem,55dvh)] min-h-0 flex-col">
<ChatTranscript @prompt="usePrompt" />
</div>
<div @focusin="expanded = true">
<ChatComposer ref="composer" :open="false" :bordered="expanded" :class="!expanded && 'border-t-0!'">
<template v-if="!expanded" #actions>
<Button
variant="ghost"
size="icon-sm"
aria-label="Show conversation"
title="Show conversation"
@click="expanded = true"
>
<ChevronUp />
</Button>
</template>
</ChatComposer>
</div>
</aside>
</div>
</template>