Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { useAtomValue } from 'jotai';
import { useRecoilValue } from 'recoil';
import {
SubRow,
messageFooterClasses,
useQueuedTurnPortal,
MessageRow,
} from '@librechat/chat/components';
import {
hasRevealSuccessor,
buildRevealedMessage,
revealedQueuedTurnFamily,
} from '@librechat/chat/react';
import type { TMessage } from 'librechat-data-provider';
import type { RefObject } from 'react';
import type { TMessageIcon } from '~/common';
import { getHeaderPrefixForScreenReader, getMessageAriaLabel } from '~/utils';
import MessageIcon from '~/components/Chat/Messages/MessageIcon';
import { useAuthContext } from '~/hooks/AuthContext';
import MarkdownLite from './Content/MarkdownLite';
import { useChatContext } from '~/Providers';
import Container from './Content/Container';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
import store from '~/store';
/** How far above the end of the thread a reader can be and still be handed
* the next turn, matching the scroll button's own near-bottom band. */
const FOLLOW_THRESHOLD_PX = 160;
/**
* The queued follow-up the server is about to admit, drawn as the user turn
* after the completed response it follows. It is not persisted: its actions
* come from the composer that owns the queue, and it leaves as soon as the
* thread gains a turn after that response.
*/
export default function PendingTurn({
scrollableRef,
messages,
maximizeChatSpace = false,
}: {
scrollableRef?: RefObject<HTMLDivElement | null>;
messages?: TMessage[] | null;
maximizeChatSpace?: boolean;
}) {
const localize = useLocalize();
const { user } = useAuthContext();
const { conversation, latestMessageId } = useChatContext();
const conversationId = conversation?.conversationId ?? '';
const reveal = useAtomValue(revealedQueuedTurnFamily(conversationId));
const setPortalTarget = useQueuedTurnPortal()?.setTarget;
const clientRequestId = reveal?.clientRequestId;
const usernameDisplay = useRecoilValue(store.UsernameDisplay);
const enableUserMsgMarkdown = useRecoilValue(store.enableUserMsgMarkdown);
const rowRef = useRef<HTMLDivElement | null>(null);
const message = useMemo(
() => (reveal == null ? null : buildRevealedMessage(reveal, conversationId)),
[reveal, conversationId],
);
const successorSeen = useMemo(
() => reveal != null && hasRevealSuccessor(messages ?? [], reveal),
[messages, reveal],
);
const shown =
message != null &&
reveal != null &&
reveal.parentMessageId === latestMessageId &&
!successorSeen;
const setActionsTarget = useCallback(
(element: HTMLSpanElement | null) => {
setPortalTarget?.(
element == null || clientRequestId == null
? null
: { element, conversationId, clientRequestId },
);
},
[setPortalTarget, conversationId, clientRequestId],
);
/** A reader resting at the end of the thread was following the response;
* bring the turn that replaces it into view the way its streaming did.
* Anyone reading further up is left where they are. */
useEffect(() => {
const row = rowRef.current;
const scrollEl = scrollableRef?.current;
if (!shown || row == null || scrollEl == null) {
return;
}
const distanceFromEnd =
scrollEl.scrollHeight - scrollEl.scrollTop - scrollEl.clientHeight - row.offsetHeight;
if (distanceFromEnd > FOLLOW_THRESHOLD_PX) {
return;
}
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches === true;
row.scrollIntoView({ block: 'end', behavior: reduceMotion ? 'auto' : 'smooth' });
}, [shown, scrollableRef]);
if (!shown) {
return null;
}
const label = usernameDisplay
? (user?.name ?? '') || (user?.username ?? '')
: localize('com_user_message');
const iconData: TMessageIcon = {
endpoint: conversation?.endpoint,
model: conversation?.model,
modelLabel: label,
isCreatedByUser: true,
};
return (
<div
ref={rowRef}
className="text-text-primary w-full border-0 bg-transparent"
data-testid="pending-turn"
>
<div className="m-auto justify-center px-4 py-3 sm:px-0">
<MessageRow
message={message}
icon={<MessageIcon iconData={iconData} />}
label={label}
timestamp={reveal.revealedAt}
ariaLabel={getMessageAriaLabel(message, localize)}
headerPrefix={getHeaderPrefixForScreenReader(message, localize)}
isCreatedByUser
fullWidth={maximizeChatSpace}
footer={
<SubRow
classes={cn(messageFooterClasses, 'flex-wrap items-center justify-end gap-1.5')}
>
<span className="text-text-secondary text-xs" role="status">
{localize('com_ui_queued_turn_starting')}
</span>
<span ref={setActionsTarget} className="flex items-center gap-1" />
</SubRow>
}
>
<Container message={message}>
<div
className={cn(
'markdown prose message-content light text-text-primary w-full break-words',
!enableUserMsgMarkdown && 'whitespace-pre-wrap',
)}
>
{enableUserMsgMarkdown ? <MarkdownLite content={reveal.text} /> : reveal.text}
</div>
</Container>
</MessageRow>
</div>
</div>
);
}