292 lines
12 KiB
TypeScript
292 lines
12 KiB
TypeScript
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 <FeatherImage />;
|
|
if (attachment.kind === "voice") return <FeatherMessageSquare />;
|
|
return <FeatherFile />;
|
|
}
|
|
|
|
function ThreadMessageAttachments({ attachments }: { readonly attachments: readonly ChatAttachmentVM[] }): React.JSX.Element | null {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
if (attachments.length === 0) return null;
|
|
return (
|
|
<ButtonGroup align="stack" data-ui-id="ui.chat.messageFeature.attachments">
|
|
{attachments.map((attachment) =>
|
|
attachment.scanStatus === "blocked" ? (
|
|
<Alert key={attachment.id} variant="error" title={t("chat.attachments.blocked")} notDismissible />
|
|
) : attachment.scanStatus === "pending" ? (
|
|
<Alert key={attachment.id} variant="neutral" title={attachment.fileName} description={t("chat.attachments.pending")} notDismissible />
|
|
) : (
|
|
<Card key={attachment.id} size="small" contentOverflow="wrap">
|
|
{attachment.kind === "image" && attachment.previewHref ? (
|
|
<a href={attachment.downloadHref} target="_blank" rel="noreferrer">
|
|
<img
|
|
src={attachment.previewHref}
|
|
alt={attachment.altText ?? attachment.fileName}
|
|
className="max-h-64 w-full rounded-md object-contain"
|
|
loading="lazy"
|
|
/>
|
|
</a>
|
|
) : null}
|
|
{attachment.kind === "voice" ? (
|
|
// Voice Notes intentionally have no transcript in AK-P15b v1.
|
|
// eslint-disable-next-line jsx-a11y/media-has-caption
|
|
<audio controls preload="metadata" src={attachment.downloadHref} className="w-full">
|
|
<a href={attachment.downloadHref}>{attachment.fileName}</a>
|
|
</audio>
|
|
) : null}
|
|
<a href={attachment.downloadHref} target="_blank" rel="noreferrer">
|
|
<ButtonGroup>
|
|
<BadgeFeedback farbe="ghost-neutral">{attachmentIcon(attachment)}</BadgeFeedback>
|
|
<span>{attachment.fileName}</span>
|
|
<BadgeFeedback farbe="ghost-neutral" size="small">
|
|
{formatters.number(attachment.sizeBytes, { style: "unit", unit: "byte", unitDisplay: "narrow" })}
|
|
</BadgeFeedback>
|
|
</ButtonGroup>
|
|
</a>
|
|
</Card>
|
|
)
|
|
)}
|
|
</ButtonGroup>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<section id={`message-${message.id}`} tabIndex={-1} className="scroll-mt-24 rounded-lg outline-none target:ring-2 target:ring-brand-primary">
|
|
<Card data-message-id={message.id} data-ui-id="ui.chat.messageFeature.message">
|
|
<ButtonGroup align="stack">
|
|
<ButtonGroup>
|
|
<Avatar size="small" image={message.authorAvatarUrl ?? undefined}>
|
|
{initials(message.authorLabel)}
|
|
</Avatar>
|
|
<span>{message.authorLabel}</span>
|
|
<BadgeFeedback farbe="ghost-neutral" size="small">
|
|
{formatters.date(message.createdAtIso, { hour: "2-digit", minute: "2-digit" })}
|
|
</BadgeFeedback>
|
|
{message.pinned ? (
|
|
<BadgeFeedback farbe="subtle-information" size="small">
|
|
<FeatherPin aria-hidden="true" /> {t("chat.message.pinned")}
|
|
</BadgeFeedback>
|
|
) : null}
|
|
</ButtonGroup>
|
|
|
|
{message.deleted ? (
|
|
<Alert variant="neutral" title={t("chat.message.deleted")} notDismissible />
|
|
) : renderedBody ? (
|
|
<div data-ui-id="ui.chat.messageFeature.body" dangerouslySetInnerHTML={{ __html: renderSanitizedChatMarkdown(renderedBody) }} />
|
|
) : null}
|
|
|
|
{!message.deleted && message.mentions.length > 0 ? (
|
|
<ButtonGroup aria-label={t("chat.mention.label")}>
|
|
{message.mentions.map((mention) =>
|
|
mention.href ? (
|
|
<Link key={`${mention.kind}:${mention.id}`} to={mention.href} aria-label={mention.label}>
|
|
<BadgeFeedback farbe="subtle-information">@{mention.label}</BadgeFeedback>
|
|
</Link>
|
|
) : (
|
|
<BadgeFeedback key={`${mention.kind}:${mention.id}`} farbe="subtle-information">
|
|
@{mention.label}
|
|
</BadgeFeedback>
|
|
)
|
|
)}
|
|
</ButtonGroup>
|
|
) : null}
|
|
|
|
{!message.deleted ? <ThreadMessageAttachments attachments={message.attachments} /> : null}
|
|
|
|
{featureErrorKey ? (
|
|
<ButtonGroup align="stack" data-ui-id="ui.chat.messageFeature.error">
|
|
<Alert variant="error" title={t(featureErrorKey)} notDismissible />
|
|
{actions.onRetryFeature ? (
|
|
<Button variant="tertiary" size="small" onClick={() => actions.onRetryFeature?.(message.id)}>
|
|
{t("shared.retry")}
|
|
</Button>
|
|
) : null}
|
|
</ButtonGroup>
|
|
) : null}
|
|
|
|
{message.reactions.length > 0 ? (
|
|
<ButtonGroup aria-label={t("chat.reaction.summaryAria")}>
|
|
{message.reactions.map((reaction) => (
|
|
<Button
|
|
key={reaction.emoji}
|
|
variant={reaction.reactedByViewer ? "secondary" : "tertiary"}
|
|
size="small"
|
|
onClick={() => actions.onToggleReaction(message.id, reaction.emoji, !reaction.reactedByViewer)}
|
|
disabled={!message.canReact}
|
|
>
|
|
{reaction.emoji} {reaction.count}
|
|
</Button>
|
|
))}
|
|
</ButtonGroup>
|
|
) : null}
|
|
|
|
{message.canReact || message.canPin || message.canEdit || message.canDelete ? (
|
|
<ButtonGroup>
|
|
{message.canReact
|
|
? QUICK_REACTIONS.map((emoji) => {
|
|
const active = message.reactions.some((reaction) => reaction.emoji === emoji && reaction.reactedByViewer);
|
|
return (
|
|
<Button
|
|
key={emoji}
|
|
variant="ghost"
|
|
size="small"
|
|
icon={<FeatherSmile />}
|
|
aria-label={t(active ? "chat.reaction.remove" : "chat.reaction.add", { emoji })}
|
|
onClick={() => actions.onToggleReaction(message.id, emoji, !active)}
|
|
>
|
|
{emoji}
|
|
</Button>
|
|
);
|
|
})
|
|
: null}
|
|
{message.canPin ? (
|
|
<Button
|
|
variant="tertiary"
|
|
size="small"
|
|
icon={<FeatherPin />}
|
|
onClick={() => actions.onTogglePin(message.id, !message.pinned)}
|
|
>
|
|
{t(message.pinned ? "chat.pinned.unpin" : "chat.pinned.pin")}
|
|
</Button>
|
|
) : null}
|
|
{message.canEdit && actions.onEditMessage ? (
|
|
<Button
|
|
variant="tertiary"
|
|
size="small"
|
|
icon={<FeatherEdit3 />}
|
|
onClick={() => {
|
|
setEditDraft(message.bodyMarkdown ?? "");
|
|
setEditOpen(true);
|
|
}}
|
|
>
|
|
{t("chat.message.edit")}
|
|
</Button>
|
|
) : null}
|
|
{message.canDelete && actions.onDeleteMessage ? (
|
|
<Button variant="tertiary" size="small" icon={<FeatherTrash2 />} onClick={() => setDeleteOpen(true)}>
|
|
{t("chat.message.delete")}
|
|
</Button>
|
|
) : null}
|
|
</ButtonGroup>
|
|
) : null}
|
|
</ButtonGroup>
|
|
</Card>
|
|
|
|
<Dialog open={editOpen} onOpenChange={(open: boolean) => setEditOpen(open)}>
|
|
<Dialog.Content className="w-[520px] max-w-[90vw] p-4">
|
|
<ButtonGroup align="stack">
|
|
<DialogTitle className="text-heading-0 font-heading-0 text-text-neutral-primary">{t("chat.message.editTitle")}</DialogTitle>
|
|
<TextArea label={t("chat.message.editTitle")} showLabel={false}>
|
|
<TextArea.Input value={editDraft} placeholder={t("chat.message.editPlaceholder")} onChange={(event) => setEditDraft(event.target.value)} />
|
|
</TextArea>
|
|
<ButtonGroup>
|
|
<Button variant="tertiary" onClick={() => setEditOpen(false)}>{t("chat.message.cancel")}</Button>
|
|
<Button
|
|
variant="primary"
|
|
onClick={() => {
|
|
actions.onEditMessage?.(message.id, editDraft);
|
|
setEditOpen(false);
|
|
}}
|
|
disabled={editDraft.trim().length === 0 || editDraft === message.bodyMarkdown}
|
|
>
|
|
{t("chat.message.save")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
</ButtonGroup>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
|
|
<Dialog open={deleteOpen} onOpenChange={(open: boolean) => setDeleteOpen(open)}>
|
|
<Dialog.Content className="w-[420px] max-w-[90vw] p-4">
|
|
<ButtonGroup align="stack">
|
|
<DialogTitle className="text-heading-0 font-heading-0 text-text-neutral-primary">{t("chat.message.deleteConfirmTitle")}</DialogTitle>
|
|
<DialogDescription className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
|
|
{t("chat.message.deleteConfirmBody")}
|
|
</DialogDescription>
|
|
<ButtonGroup>
|
|
<Button variant="tertiary" onClick={() => setDeleteOpen(false)}>{t("chat.message.cancel")}</Button>
|
|
<Button
|
|
variant="destructive"
|
|
onClick={() => {
|
|
actions.onDeleteMessage?.(message.id);
|
|
setDeleteOpen(false);
|
|
}}
|
|
>
|
|
{t("chat.message.delete")}
|
|
</Button>
|
|
</ButtonGroup>
|
|
</ButtonGroup>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
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("") || "?"
|
|
);
|
|
}
|