import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router";
import { Description as DialogDescription, Title as DialogTitle } from "@radix-ui/react-dialog";
import { FeatherEdit3, FeatherFile, FeatherImage, FeatherMessageSquare, FeatherPin, FeatherSmile, FeatherTrash2 } from "@subframe/core";
import type { ChatAttachmentVM, ChatFeatureMessageVM } from "@hivegrid/view-models";
import { useLocaleFormatters } from "~/locale/format";
import { renderSanitizedChatMarkdown } from "~/utils/html-sanitizer";
import { Alert } from "~/ui/components/Alert";
import { Avatar } from "~/ui/components/Avatar";
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 { Dialog } from "~/ui/components/Dialog";
import { TextArea } from "~/ui/components/TextArea";
const QUICK_REACTIONS = ["👍", "❤️", "🎉"] as const;
function attachmentIcon(attachment: ChatAttachmentVM): React.JSX.Element {
if (attachment.kind === "image") return ;
if (attachment.kind === "voice") return ;
return ;
}
function ThreadMessageAttachments({ attachments }: { readonly attachments: readonly ChatAttachmentVM[] }): React.JSX.Element | null {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
if (attachments.length === 0) return null;
return (
{attachments.map((attachment) =>
attachment.scanStatus === "blocked" ? (
) : attachment.scanStatus === "pending" ? (
) : (
{attachment.kind === "image" && attachment.previewHref ? (
) : null}
{attachment.kind === "voice" ? (
// Voice Notes intentionally have no transcript in AK-P15b v1.
// eslint-disable-next-line jsx-a11y/media-has-caption
) : null}
{attachmentIcon(attachment)}
{attachment.fileName}
{formatters.number(attachment.sizeBytes, { style: "unit", unit: "byte", unitDisplay: "narrow" })}
)
)}
);
}
export function ChatMessageFeatureCard({
message,
featureErrorKey,
actions,
}: {
readonly message: ChatFeatureMessageVM;
readonly featureErrorKey?: string;
readonly actions: {
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 onRetryFeature?: (messageId: string) => void;
};
}): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
const [editOpen, setEditOpen] = useState(false);
const [deleteOpen, setDeleteOpen] = useState(false);
const [editDraft, setEditDraft] = useState(message.bodyMarkdown ?? "");
const renderedBody = message.bodyMarkdown ? replaceMentionTokensWithLabels(message.bodyMarkdown, message.mentions) : null;
return (
{initials(message.authorLabel)}
{message.authorLabel}
{formatters.date(message.createdAtIso, { hour: "2-digit", minute: "2-digit" })}
{message.pinned ? (
{t("chat.message.pinned")}
) : null}
{message.deleted ? (
) : renderedBody ? (
) : null}
{!message.deleted && message.mentions.length > 0 ? (
{message.mentions.map((mention) =>
mention.href ? (
@{mention.label}
) : (
@{mention.label}
)
)}
) : null}
{!message.deleted ? : null}
{featureErrorKey ? (
{actions.onRetryFeature ? (
) : null}
) : null}
{message.reactions.length > 0 ? (
{message.reactions.map((reaction) => (
))}
) : null}
{message.canReact || message.canPin || message.canEdit || message.canDelete ? (
{message.canReact
? QUICK_REACTIONS.map((emoji) => {
const active = message.reactions.some((reaction) => reaction.emoji === emoji && reaction.reactedByViewer);
return (
}
aria-label={t(active ? "chat.reaction.remove" : "chat.reaction.add", { emoji })}
onClick={() => actions.onToggleReaction(message.id, emoji, !active)}
>
{emoji}
);
})
: null}
{message.canPin ? (
}
onClick={() => actions.onTogglePin(message.id, !message.pinned)}
>
{t(message.pinned ? "chat.pinned.unpin" : "chat.pinned.pin")}
) : null}
{message.canEdit && actions.onEditMessage ? (
}
onClick={() => {
setEditDraft(message.bodyMarkdown ?? "");
setEditOpen(true);
}}
>
{t("chat.message.edit")}
) : null}
{message.canDelete && actions.onDeleteMessage ? (
} onClick={() => setDeleteOpen(true)}>
{t("chat.message.delete")}
) : null}
) : null}
);
}
function replaceMentionTokensWithLabels(
markdown: string,
mentions: ChatFeatureMessageVM["mentions"]
): string {
const labels = new Map(mentions.map((mention) => [`${mention.kind}\u0000${mention.id}`, mention.label]));
return markdown.replace(/<@(user|project|contact|company):([A-Za-z0-9._-]{1,500})>/gu, (_token, kind: string, id: string) => {
const label = labels.get(`${kind}\u0000${id}`) ?? "@";
return `@${escapeMarkdownText(label)}`;
});
}
function escapeMarkdownText(value: string): string {
return value.replace(/([\\`*_{}\[\]()<>#+\-.!|])/gu, "\\$1");
}
function initials(label: string): string {
return (
label
.trim()
.split(/\s+/)
.slice(0, 2)
.map((part) => part[0]?.toLocaleUpperCase() ?? "")
.join("") || "?"
);
}