import { AuiIf, ComposerPrimitive, useAui, useAuiState } from '@assistant-ui/react'; import { ArrowUp, CalendarDays, MapPin, Paperclip, Square, X } from 'lucide-react'; import { type ReactNode, useEffect, useRef } from 'react'; import { useTranslation } from '../../i18n'; import { chatOf, useAssistantStore } from '../../store/assistantStore'; import PlaceAvatar from '../shared/PlaceAvatar'; import Tooltip from '../shared/Tooltip'; import AssistantMentionPopover from './AssistantMentionPopover'; import { useAssistantView } from './assistantViewContext'; import { useAssistantAttach } from './useAssistantAttach'; import { useAssistantFocus } from './useAssistantFocus'; import { useAssistantMentions } from './useAssistantMentions'; import { useAssistantSuggestions } from './useAssistantSuggestions'; import { useTypewriterPlaceholder } from './useTypewriterPlaceholder'; /** Send and Stop share one spot; on the phone it is a full thumb's size. */ const ACTION = { window: 'h-9 w-9', fullscreen: 'h-11 w-11', } as const; /** * Where the user writes. One rounded shell with the text and, in the same * spot, Send or Stop. Enter sends, Shift+Enter breaks the line, Escape stops * a running answer. The one chip above the text is the focus the question is * about, and can be removed. `@` at the start of a word opens the trip's days, * places and bookings to name; a pick stays as `@label` in the text, which is * what the question names, and shows as a chip only in the sent bubble. While * that list is open, Enter picks and Escape closes it. While the field is empty, * its placeholder writes out things worth asking about this trip, one after * another. * * The text typed so far lives in the assistant store too, so it survives the * window closing or the phone hopping to the map and back. */ export default function AssistantComposer({ tripId, tripTitle }: { tripId: number; tripTitle: string }) { const { t } = useTranslation(); const { variant } = useAssistantView(); const aui = useAui(); const setDraft = useAssistantStore((s) => s.setDraft); const { label, kind, place, clear } = useAssistantFocus(tripId); const mentions = useAssistantMentions(tripId); const { suggestions } = useAssistantSuggestions(tripId); const empty = useAuiState((s) => s.composer.isEmpty); const { take, busy, accept, canAttach } = useAssistantAttach(tripId); const fileInput = useRef(null); const fallback = t('assistant.composer.placeholder', { trip: tripTitle }); const placeholder = useTypewriterPlaceholder( suggestions.map((s) => s.prompt), fallback, empty ); // Takes back what was typed before the chat was last shown. useEffect(() => { const draft = chatOf(useAssistantStore.getState(), tripId).draft; if (draft) aui.composer.setText(draft); }, [aui, tripId]); const FocusIcon = kind === 'place' ? MapPin : CalendarDays; return ( {label && (
:
)}
{canAttach && ( <> { take(e.target.files); e.target.value = ''; }} /> )} setDraft(tripId, e.target.value)} onPaste={(e) => { if (e.clipboardData.files.length > 0 && take(e.clipboardData.files)) e.preventDefault(); }} className="max-h-48 min-h-[36px] flex-1 resize-none bg-transparent px-1.5 py-1.5 text-body text-content outline-none placeholder:text-content-faint disabled:cursor-not-allowed dark:bg-transparent" /> s.thread.isRunning}> !s.thread.isRunning}>
); } /** One thing the question is about, above the text, with the cross that takes it back. */ function ContextChip({ label, icon, round, onRemove, }: { label: string; icon: ReactNode; /** A place's picture fills the chip's round end, so the chip starts closer to it. */ round: boolean; onRemove: () => void; }) { const { t } = useTranslation(); return ( {icon} {label} ); } function SendButton({ size }: { size: string }) { const { t } = useTranslation(); // Sending waits while a proposed change is undecided; the tooltip says so. const blocked = useAuiState((s) => s.thread.isSendDisabled); const button = ( ); // A disabled button gets no pointer events, so the tooltip that says why hangs on a wrapper. return blocked ? ( {button} ) : ( button ); }