import { useRef } from "react"; import { useTranslation } from "react-i18next"; import { FeatherArrowLeft, FeatherMessageSquare, FeatherSend } from "@subframe/core"; import type { ChatMentionAutocompleteVM, ChatMentionSuggestionVM, ChatThreadVM } from "@hivegrid/view-models"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { ButtonGroup } from "~/ui/components/ButtonGroup"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { LoadingIndicatorSkeleton } from "~/ui/components/LoadingIndicatorSkeleton"; import { TextArea } from "~/ui/components/TextArea"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; import { ChatMessageFeatureCard } from "./ChatMessageFeatureCard"; import { ChatMentionAutocomplete } from "./ChatMentionAutocomplete"; export type ChatOptimisticThreadReply = Readonly<{ clientMessageId: string; authorLabel: string; bodyMarkdown: string; createdAtIso: string; deliveryState: "sending" | "sent" | "failed"; failureReasonKey: string | null; }>; export function ChatThreadPage({ vm, draft, busy, errorKey, featureErrorKeys, mentionAutocomplete, optimisticReplies = [], actions, }: { readonly vm: ChatThreadVM; readonly draft: string; readonly busy: boolean; readonly errorKey: string | null; readonly featureErrorKeys?: Readonly>; readonly optimisticReplies?: readonly ChatOptimisticThreadReply[]; readonly mentionAutocomplete?: { readonly vm: ChatMentionAutocompleteVM | null; readonly open: boolean; readonly loading: boolean; readonly activeIndex: number; readonly listboxId: string; }; readonly actions: { readonly onBack: (href: string) => void; readonly onDraftChange: (value: string, caret: number) => void; readonly onSendReply: (value: string) => void; readonly onToggleReaction: (messageId: string, emoji: string, active: boolean) => void; readonly onTogglePin: (messageId: string, pinned: boolean) => void; readonly onEditMessage: (messageId: string, body: string) => void; readonly onDeleteMessage: (messageId: string) => void; readonly onMentionActiveIndexChange?: (index: number) => void; readonly onMentionSelect?: (suggestion: ChatMentionSuggestionVM) => number | null; readonly onMentionKeyDown?: (key: "ArrowDown" | "ArrowUp" | "Enter" | "Escape") => boolean; readonly onRetryReply?: (clientMessageId: string) => void; readonly onDiscardReply?: (clientMessageId: string) => void; readonly onRetryFeature?: (messageId: string) => void; readonly onRetry: () => void; }; }): React.JSX.Element { const { t } = useTranslation(); const data = vm.status === "ready" ? vm.data : null; const composerRef = useRef(null); function selectMention(suggestion: ChatMentionSuggestionVM): void { const nextCaret = actions.onMentionSelect?.(suggestion) ?? null; if (nextCaret === null) return; requestAnimationFrame(() => { composerRef.current?.focus(); composerRef.current?.setSelectionRange(nextCaret, nextCaret); }); } function sendDraft(): void { const body = draft.trim(); if (!body || busy) return; actions.onSendReply(body); requestAnimationFrame(() => composerRef.current?.focus()); } return ( } title={data ? t("chat.thread.title", { channel: data.channelTitle }) : t("chat.thread.titleFallback")} subtitle={data ? t("chat.thread.replyCount", { count: data.replyCount }) : undefined} actions={ } /> {vm.status === "loading" || vm.status === "idle" ? ( ) : vm.status === "error" ? ( {t("shared.retry")}} /> ) : !data ? ( ) : ( {errorKey ? : null} {data.replies.length === 0 && optimisticReplies.length === 0 ? ( ) : ( <> {data.replies.map((reply) => ( ))} {optimisticReplies.map((reply) => ( {reply.authorLabel} {t(`chat.message.delivery.${reply.deliveryState}`)}

{reply.bodyMarkdown}

{reply.deliveryState === "failed" ? ( {t(reply.failureReasonKey ?? "chat.message.sendFailed")} ) : null}
))} )} {data.canReply ? ( {mentionAutocomplete ? ( undefined), }} /> ) : null} ) : ( )}
)}
); }