import type { CollabBubbleCard } from '@trek/shared';
import { Check, Clock, Paperclip, TriangleAlert } from 'lucide-react';
import { useTranslation } from '../../../../i18n';
import { useSettingsStore } from '../../../../store/settingsStore';
import { avatarSrc } from '../../../../utils/avatarSrc';
import CollabAssistantBadge from '../../../Collab/CollabAssistantBadge';
import {
ChatAvatar,
ChatBigEmoji,
ChatBubble,
ChatDateSeparator,
ChatReactions,
ChatSenderName,
ChatTime,
} from '../../../Collab/CollabChatBubble';
import { MessageText } from '../../../Collab/CollabChatMessageText';
import { TwemojiImg } from '../../../Collab/CollabChatTwemojiImg';
import { formatChatClockTime, formatChatDateSeparator, isEmojiOnlyText } from '../../../Collab/collabModel';
import { SoftPill } from '../../../Planner/planParts';
import { Tooltip } from '../../../shared/Tooltip';
import type { ChangeState } from '../../assistantParts';
import { useAssistantView } from '../../assistantViewContext';
import { useViewerId } from '../parts/useViewerId';
import { type BubbleSpot, bubbleStatus, isDropped, spotOf } from './collabCardModel';
/**
* Messages of the trip chat drawn as the chat draws them, from the pieces the
* Collab tab uses: the others on one side with their face and name, the
* user's own on the other in the accent, a day between days, the reactions
* floating under a bubble, the time under the last of a run. A read-only
* copy: no hover actions, links read as words, only the tooltips answer. A
* held change marks its bubble: a send says it is not sent yet (then sent or
* not sent), a delete strikes it, a reaction rings the emoji it toggles (and
* keeps a faint pill for one it takes away).
*/
/** One reaction as the chat's floating pill shows it, the names in its tooltip. */
function Reaction({ reaction, held }: { reaction: CollabBubbleCard['reactions'][number]; held: boolean }) {
const names = reaction.names.join(', ');
return (
{reaction.count > 1 && (
{reaction.count}
)}
);
}
/**
* The reaction a held toggle takes away when it was the user's only one on
* that emoji: the server drops its pill, so the card keeps it faint in a
* dashed ring until the change is applied, rather than showing nothing.
*/
function TakenBack({ emoji }: { emoji: string }) {
const { t } = useTranslation();
const label = t('assistant.card.collab.reactionRemoved', { emoji });
return (
);
}
/** A file sent with a message: an image in the bubble, any other file by its name. */
function ChatFile({ file, tripId }: { file: CollabBubbleCard['files'][number]; tripId: number }) {
const { t } = useTranslation();
if (file.image) {
return (
);
}
return (
} className="max-w-full">
{file.name ?? t('collab.chat.attachedImage')}
);
}
/** The time slot under the last bubble of a run: the time, or how a held send stands. */
function Status({ bubble, state }: { bubble: CollabBubbleCard; state: ChangeState | null }) {
const { t } = useTranslation();
const is12h = useSettingsStore((s) => s.settings.time_format === '12h');
const status = bubbleStatus(bubble, state);
const time = bubble.at ? formatChatClockTime(bubble.at, is12h) : null;
if (status === 'none') return null;
if (status === 'waiting') {
return (
{t('assistant.card.collab.notSent')}
);
}
if (status === 'failed') {
return (
{t('assistant.card.collab.failed')}
);
}
return (
{time}
{status === 'sent' && (
)}
);
}
/** A removed message once the removal was applied: the chat's own placeholder line. */
function Removed() {
const { t } = useTranslation();
return (
{t('assistant.card.collab.youDeleted')}
);
}
/** What one bubble of a thread is drawn with. */
interface BubbleProps {
bubble: CollabBubbleCard;
spot: BubbleSpot;
own: boolean;
state: ChangeState | null;
}
/** The face beside someone else's bubble, on the last of their run; smaller on the phone. */
function Face({ bubble, spot }: Pick) {
const { variant } = useAssistantView();
const size = variant === 'fullscreen' ? 24 : 28;
return (
{spot.lastInGroup && (
)}
);
}
/** The bubble itself (or emoji alone, drawn large) with its files. */
function Words({ bubble, spot, own }: Pick) {
const { tripId } = useAssistantView();
if (bubble.files.length === 0 && isEmojiOnlyText(bubble.text)) return {bubble.text};
return (
}
>
{bubble.files.map((file) => (
))}
);
}
/** Everything in the column of one bubble: the name, the assistant mark, the bubble, its reactions and its time. */
function Column({ bubble, spot, own, state }: BubbleProps) {
const { t } = useTranslation();
const { variant } = useAssistantView();
const reacting = bubble.change === 'react' && state !== 'applied';
const held = (emoji: string) => reacting && emoji === bubble.reactEmoji;
const takenBack =
reacting && bubble.reactEmoji && !bubble.reactions.some((r) => r.emoji === bubble.reactEmoji)
? bubble.reactEmoji
: null;
return (
{!own && spot.newGroup &&
{bubble.author.name}}
{bubble.fromAssistant && (
)}
{(bubble.reactions.length > 0 || takenBack) && (
{bubble.reactions.map((reaction) => (
))}
{takenBack && }
)}
{bubble.cut && (
{t('assistant.card.collab.continues')}
)}
{spot.lastInGroup &&
}
);
}
/** One message: the day before it when it changes, then its row on its sender's side. */
function BubbleRow({ bubble, spot, own, state }: BubbleProps) {
const { t, locale } = useTranslation();
const removing = bubble.change === 'remove';
const day = spot.newDay && bubble.at && ;
if (removing && state === 'applied') {
return (
<>
{day}
>
);
}
const dimmed = removing || (bubble.change === 'send' && isDropped(state));
return (
<>
{day}
{!own && }
>
);
}
export default function CollabChatThread({
messages,
state = null,
}: {
messages: readonly CollabBubbleCard[];
/** The decision on the change the thread shows; null for an answer. */
state?: ChangeState | null;
}) {
const viewerId = useViewerId();
return (
{messages.map((bubble, index) => (
))}
);
}