mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-03 19:27:28 +01:00
Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6 Co-authored-by: Claude <noreply@anthropic.com>
81 lines
5.2 KiB
JavaScript
81 lines
5.2 KiB
JavaScript
"use client";
|
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
import { useEffect, useState } from "react";
|
|
import { clockTime, } from "../messenger.js";
|
|
import { MessengerAvatar } from "./MessageAttachments.js";
|
|
import { Sheet } from "./Sheet.js";
|
|
/** "14:02" today, "12 Nov, 14:02" before: a receipt is read at a glance. */
|
|
export function receiptTime(at, locale, timeZone, now = new Date()) {
|
|
const day = (d) => new Intl.DateTimeFormat(locale, {
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
timeZone,
|
|
}).format(d);
|
|
if (day(at) === day(now))
|
|
return clockTime(at, locale, timeZone);
|
|
return new Intl.DateTimeFormat(locale, {
|
|
day: "numeric",
|
|
month: "short",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
timeZone,
|
|
}).format(at);
|
|
}
|
|
/**
|
|
* "Info" on a message: for the viewer's own, who has it on their device and
|
|
* who has read it, each with the time; for an edited message, what it said
|
|
* before. Read on opening, never kept: a receipt changes while you look away.
|
|
*/
|
|
export function MessageInfo({ message, conversation, viewerId, transport, labels, locale, timeZone, onClose, }) {
|
|
const [loaded, setLoaded] = useState({ receipts: null, edits: null, failed: false });
|
|
useEffect(() => {
|
|
if (!message)
|
|
return;
|
|
setLoaded({ receipts: null, edits: null, failed: false });
|
|
const control = new AbortController();
|
|
const own = message.authorId === viewerId;
|
|
Promise.all([
|
|
own && transport.receipts ? transport.receipts(message, { signal: control.signal }) : null,
|
|
message.editedAt && transport.edits
|
|
? transport.edits(message, { signal: control.signal })
|
|
: null,
|
|
])
|
|
.then(([receipts, edits]) => {
|
|
if (control.signal.aborted)
|
|
return;
|
|
setLoaded({
|
|
receipts: receipts?.map((r) => ({
|
|
...r,
|
|
deliveredAt: r.deliveredAt ? new Date(r.deliveredAt) : null,
|
|
readAt: r.readAt ? new Date(r.readAt) : null,
|
|
})) ?? null,
|
|
edits: edits?.map((e) => ({ ...e, replacedAt: new Date(e.replacedAt) })) ?? null,
|
|
failed: false,
|
|
});
|
|
})
|
|
.catch(() => {
|
|
if (!control.signal.aborted)
|
|
setLoaded((l) => ({ ...l, failed: true }));
|
|
});
|
|
return () => control.abort();
|
|
}, [message, viewerId, transport]);
|
|
const person = (actorId) => conversation?.participants.find((p) => p.actorId === actorId);
|
|
const at = (date) => receiptTime(date, locale, timeZone);
|
|
return (_jsx(Sheet, { open: message !== null, onClose: onClose, title: labels.infoTitle, closeLabel: labels.close, children: _jsxs("div", { className: "ck-msgr-info", children: [message?.body && _jsx("p", { className: "ck-msgr-info-quote", children: message.body }), loaded.failed && _jsx("p", { className: "ck-msgr-problem", children: labels.infoFailed }), loaded.receipts && (_jsx("ul", { className: "ck-msgr-info-list", children: loaded.receipts
|
|
// An agent has what it is sent the moment it is stored: a row
|
|
// saying it was "not delivered" would be untrue.
|
|
.filter((receipt) => person(receipt.actorId)?.kind !== "ai")
|
|
.map((receipt) => {
|
|
const who = person(receipt.actorId);
|
|
const name = who?.name ?? "";
|
|
return (_jsxs("li", { className: "ck-msgr-info-row", children: [_jsx(MessengerAvatar, { name: name, src: who?.avatarUrl, size: "sm" }), _jsxs("span", { className: "ck-msgr-info-who", children: [_jsx("span", { className: "ck-msgr-info-name ck-truncate", children: name }), _jsx("span", { className: "ck-msgr-info-when", children: receipt.readAt
|
|
? labels.readAt.replace("{time}", at(receipt.readAt))
|
|
: receipt.deliveredAt
|
|
? labels.deliveredAt.replace("{time}", at(receipt.deliveredAt))
|
|
: labels.notDelivered }), receipt.readAt &&
|
|
receipt.deliveredAt &&
|
|
receipt.deliveredAt.getTime() < receipt.readAt.getTime() && (_jsx("span", { className: "ck-msgr-info-when", children: labels.deliveredAt.replace("{time}", at(receipt.deliveredAt)) }))] })] }, receipt.actorId));
|
|
}) })), loaded.edits && loaded.edits.length > 0 && (_jsxs("section", { className: "ck-msgr-info-edits", "aria-label": labels.editHistory, children: [_jsx("h3", { className: "ck-msgr-info-heading", children: labels.editHistory }), _jsx("ol", { className: "ck-msgr-info-list", children: loaded.edits.map((edit, index) => (_jsxs("li", { className: "ck-msgr-info-edit", children: [_jsx("span", { className: "ck-msgr-info-body", children: edit.body }), _jsx("span", { className: "ck-msgr-info-when", children: labels.editedUntil.replace("{time}", at(edit.replacedAt)) })] }, index))) })] }))] }) }));
|
|
}
|
|
//# sourceMappingURL=MessageInfo.js.map
|