mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import { useState, useCallback, useRef, useEffect } from 'react';
|
|
import remarkGfm from 'remark-gfm';
|
|
import remarkMath from 'remark-math';
|
|
import { Copy, Check } from 'lucide';
|
|
import rehypeKatex from 'rehype-katex';
|
|
import supersub from 'remark-supersub';
|
|
import ReactMarkdown from 'react-markdown';
|
|
import rehypeHighlight from 'rehype-highlight';
|
|
import { Button, MorphIcon, TooltipAnchor, useToastContext } from '@librechat/client';
|
|
import { codeNoExecution } from '~/components/Chat/Messages/Content/MarkdownComponents';
|
|
import { PromptVariableGfm } from '../editor/Markdown';
|
|
import { useLocalize } from '~/hooks';
|
|
|
|
interface PromptTextCardProps {
|
|
mainText: string;
|
|
}
|
|
|
|
const PromptTextCard = ({ mainText }: PromptTextCardProps) => {
|
|
const localize = useLocalize();
|
|
const { showToast } = useToastContext();
|
|
const [isCopied, setIsCopied] = useState(false);
|
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
const handleCopy = useCallback(async () => {
|
|
if (isCopied) {
|
|
return;
|
|
}
|
|
try {
|
|
await navigator.clipboard.writeText(mainText);
|
|
setIsCopied(true);
|
|
showToast({
|
|
message: localize('com_ui_copied_to_clipboard'),
|
|
status: 'success',
|
|
});
|
|
timeoutRef.current = setTimeout(() => {
|
|
setIsCopied(false);
|
|
}, 2000);
|
|
} catch {
|
|
showToast({
|
|
message: localize('com_ui_copy_failed'),
|
|
status: 'error',
|
|
});
|
|
}
|
|
}, [mainText, showToast, localize, isCopied]);
|
|
|
|
return (
|
|
<div className="border-border-medium relative flex h-full flex-col rounded-xl border bg-transparent">
|
|
<div className="absolute top-2 right-2 z-10">
|
|
<TooltipAnchor
|
|
description={isCopied ? localize('com_ui_copied') : localize('com_ui_copy')}
|
|
render={
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
onClick={handleCopy}
|
|
className="size-8 gap-1.5"
|
|
aria-label={
|
|
isCopied ? localize('com_ui_copied') : localize('com_ui_copy_to_clipboard')
|
|
}
|
|
aria-live="polite"
|
|
>
|
|
<MorphIcon icon={isCopied ? Check : Copy} className="text-text-secondary size-4" />
|
|
</Button>
|
|
}
|
|
/>
|
|
</div>
|
|
<div className="min-h-0 flex-1 overflow-y-auto p-3">
|
|
<ReactMarkdown
|
|
remarkPlugins={[
|
|
/** @ts-ignore */
|
|
supersub,
|
|
remarkGfm,
|
|
[remarkMath, { singleDollarTextMath: false }],
|
|
]}
|
|
rehypePlugins={[
|
|
/** @ts-ignore */
|
|
[rehypeKatex],
|
|
/** @ts-ignore */
|
|
[rehypeHighlight, { ignoreMissing: true }],
|
|
]}
|
|
/** @ts-ignore */
|
|
components={{ p: PromptVariableGfm, code: codeNoExecution }}
|
|
className="markdown prose light text-text-primary my-1 max-w-none break-words"
|
|
>
|
|
{mainText}
|
|
</ReactMarkdown>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default PromptTextCard;
|