mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
chore: refactor cursor blink, debugging
This commit is contained in:
parent
84b104e65f
commit
8c6340aed0
3 changed files with 121 additions and 78 deletions
|
|
@ -1,10 +1,9 @@
|
||||||
import React, { useState, useEffect, useRef } from 'react';
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||||
import TextWrapper from './TextWrapper';
|
import TextWrapper from './TextWrapper';
|
||||||
import MultiMessage from './MultiMessage';
|
import MultiMessage from './MultiMessage';
|
||||||
import { useSelector, useDispatch } from 'react-redux';
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
import HoverButtons from './HoverButtons';
|
import HoverButtons from './HoverButtons';
|
||||||
import SiblingSwitch from './SiblingSwitch';
|
import SiblingSwitch from './SiblingSwitch';
|
||||||
import Spinner from '../svg/Spinner';
|
|
||||||
import { setError } from '~/store/convoSlice';
|
import { setError } from '~/store/convoSlice';
|
||||||
import { setMessages } from '~/store/messageSlice';
|
import { setMessages } from '~/store/messageSlice';
|
||||||
import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
||||||
|
|
@ -22,21 +21,28 @@ export default function Message({
|
||||||
siblingCount,
|
siblingCount,
|
||||||
setSiblingIdx
|
setSiblingIdx
|
||||||
}) {
|
}) {
|
||||||
const { isSubmitting, model, chatGptLabel, promptPrefix } = useSelector((state) => state.submit);
|
const { isSubmitting, model, chatGptLabel, promptPrefix } = useSelector(
|
||||||
|
(state) => state.submit
|
||||||
|
);
|
||||||
const [abortScroll, setAbort] = useState(false);
|
const [abortScroll, setAbort] = useState(false);
|
||||||
const { sender, text, isCreatedByUser, error, submitting } = message
|
const { sender, text, isCreatedByUser, error, submitting } = message;
|
||||||
const textEditor = useRef(null)
|
const textEditor = useRef(null);
|
||||||
const convo = useSelector((state) => state.convo);
|
const convo = useSelector((state) => state.convo);
|
||||||
const { initial } = useSelector((state) => state.models);
|
const { initial } = useSelector((state) => state.models);
|
||||||
const { error: convoError } = convo;
|
const { error: convoError } = convo;
|
||||||
const last = !message?.children?.length
|
const last = !message?.children?.length;
|
||||||
|
|
||||||
const edit = message.messageId == currentEditId;
|
const edit = message.messageId == currentEditId;
|
||||||
|
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
// const notUser = !isCreatedByUser; // sender.toLowerCase() !== 'user';
|
// const notUser = !isCreatedByUser; // sender.toLowerCase() !== 'user';
|
||||||
const blinker = submitting && isSubmitting && last && !isCreatedByUser;
|
const blinker = submitting && isSubmitting && last && !isCreatedByUser;
|
||||||
|
const generateCursor = useCallback(() => {
|
||||||
|
if (!blinker) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return <span className="result-streaming">█</span>;
|
||||||
|
}, [blinker]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (blinker && !abortScroll) {
|
if (blinker && !abortScroll) {
|
||||||
|
|
@ -45,15 +51,10 @@ export default function Message({
|
||||||
}, [isSubmitting, text, blinker, scrollToBottom, abortScroll]);
|
}, [isSubmitting, text, blinker, scrollToBottom, abortScroll]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (last)
|
if (last) dispatch(setConversation({ parentMessageId: message?.messageId }));
|
||||||
dispatch(setConversation({parentMessageId: message?.messageId}))
|
}, [last]);
|
||||||
}, [last, ])
|
|
||||||
|
|
||||||
// if (sender === '') {
|
const enterEdit = (cancel) => setCurrentEditId(cancel ? -1 : message.messageId);
|
||||||
// return <Spinner />;
|
|
||||||
// }
|
|
||||||
|
|
||||||
const enterEdit = (cancel) => setCurrentEditId(cancel?-1:message.messageId)
|
|
||||||
|
|
||||||
const handleWheel = () => {
|
const handleWheel = () => {
|
||||||
if (blinker) {
|
if (blinker) {
|
||||||
|
|
@ -68,16 +69,23 @@ export default function Message({
|
||||||
'w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 bg-white dark:text-gray-100 group dark:bg-gray-800'
|
'w-full border-b border-black/10 dark:border-gray-900/50 text-gray-800 bg-white dark:text-gray-100 group dark:bg-gray-800'
|
||||||
};
|
};
|
||||||
|
|
||||||
const icon = getIconOfModel({ sender, isCreatedByUser, model, chatGptLabel, promptPrefix, error });
|
const icon = getIconOfModel({
|
||||||
|
sender,
|
||||||
|
isCreatedByUser,
|
||||||
|
model,
|
||||||
|
chatGptLabel,
|
||||||
|
promptPrefix,
|
||||||
|
error
|
||||||
|
});
|
||||||
|
|
||||||
if (!isCreatedByUser)
|
if (!isCreatedByUser)
|
||||||
props.className =
|
props.className =
|
||||||
'w-full border-b border-black/10 bg-gray-50 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group bg-gray-100 dark:bg-[#444654]';
|
'w-full border-b border-black/10 bg-gray-50 dark:border-gray-900/50 text-gray-800 dark:text-gray-100 group bg-gray-100 dark:bg-[#444654]';
|
||||||
|
|
||||||
const wrapText = (text) => <TextWrapper text={text} />;
|
// const wrapText = (text) => <TextWrapper text={text} generateCursor={generateCursor}/>;
|
||||||
|
|
||||||
const resubmitMessage = () => {
|
const resubmitMessage = () => {
|
||||||
const text = textEditor.current.innerText
|
const text = textEditor.current.innerText;
|
||||||
|
|
||||||
if (convoError) {
|
if (convoError) {
|
||||||
dispatch(setError(false));
|
dispatch(setError(false));
|
||||||
|
|
@ -91,13 +99,22 @@ export default function Message({
|
||||||
const fakeMessageId = crypto.randomUUID();
|
const fakeMessageId = crypto.randomUUID();
|
||||||
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
||||||
const currentMsg = {
|
const currentMsg = {
|
||||||
sender: 'User', text: text.trim(), current: true, isCreatedByUser: true,
|
sender: 'User',
|
||||||
|
text: text.trim(),
|
||||||
|
current: true,
|
||||||
|
isCreatedByUser: true,
|
||||||
parentMessageId: message?.parentMessageId,
|
parentMessageId: message?.parentMessageId,
|
||||||
conversationId: message?.conversationId,
|
conversationId: message?.conversationId,
|
||||||
messageId: fakeMessageId };
|
messageId: fakeMessageId
|
||||||
|
};
|
||||||
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
||||||
|
|
||||||
const initialResponse = { sender, text: '', parentMessageId: fakeMessageId, submitting: true };
|
const initialResponse = {
|
||||||
|
sender,
|
||||||
|
text: '',
|
||||||
|
parentMessageId: fakeMessageId,
|
||||||
|
submitting: true
|
||||||
|
};
|
||||||
|
|
||||||
dispatch(setSubmitState(true));
|
dispatch(setSubmitState(true));
|
||||||
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
||||||
|
|
@ -109,18 +126,18 @@ export default function Message({
|
||||||
...currentMsg,
|
...currentMsg,
|
||||||
model,
|
model,
|
||||||
chatGptLabel,
|
chatGptLabel,
|
||||||
promptPrefix,
|
promptPrefix
|
||||||
},
|
},
|
||||||
messages: messages,
|
messages: messages,
|
||||||
currentMsg,
|
currentMsg,
|
||||||
initialResponse,
|
initialResponse,
|
||||||
sender,
|
sender
|
||||||
};
|
};
|
||||||
console.log('User Input:', currentMsg?.text);
|
console.log('User Input:', currentMsg?.text);
|
||||||
// handleSubmit(submission);
|
// handleSubmit(submission);
|
||||||
dispatch(setSubmission(submission));
|
dispatch(setSubmission(submission));
|
||||||
|
|
||||||
setSiblingIdx(siblingCount - 1)
|
setSiblingIdx(siblingCount - 1);
|
||||||
enterEdit(true);
|
enterEdit(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -131,42 +148,48 @@ export default function Message({
|
||||||
onWheel={handleWheel}
|
onWheel={handleWheel}
|
||||||
>
|
>
|
||||||
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
||||||
|
|
||||||
<div className="relative flex h-[30px] w-[30px] flex-col items-end text-right text-xs md:text-sm">
|
<div className="relative flex h-[30px] w-[30px] flex-col items-end text-right text-xs md:text-sm">
|
||||||
{typeof icon === 'string' && icon.match(/[^\u0000-\u007F]+/) ? (
|
{typeof icon === 'string' && icon.match(/[^\u0000-\u007F]+/) ? (
|
||||||
<span className=" direction-rtl w-40 overflow-x-scroll">{icon}</span>
|
<span className=" direction-rtl w-40 overflow-x-scroll">{icon}</span>
|
||||||
) : (
|
) : (
|
||||||
icon
|
icon
|
||||||
)}
|
)}
|
||||||
<SiblingSwitch siblingIdx={siblingIdx} siblingCount={siblingCount} setSiblingIdx={setSiblingIdx} />
|
<SiblingSwitch
|
||||||
|
siblingIdx={siblingIdx}
|
||||||
|
siblingCount={siblingCount}
|
||||||
|
setSiblingIdx={setSiblingIdx}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
||||||
<div className="flex flex-grow flex-col gap-3">
|
<div className="flex flex-grow flex-col gap-3">
|
||||||
{error ? (
|
{error ? (
|
||||||
<div className="flex flex min-h-[20px] flex-col flex-grow items-start gap-4 gap-2 whitespace-pre-wrap text-red-500">
|
<div className="flex flex min-h-[20px] flex-grow flex-col items-start gap-4 gap-2 whitespace-pre-wrap text-red-500">
|
||||||
<div className="rounded-md border border-red-500 bg-red-500/10 py-2 px-3 text-sm text-gray-600 dark:text-gray-100">
|
<div className="rounded-md border border-red-500 bg-red-500/10 py-2 px-3 text-sm text-gray-600 dark:text-gray-100">
|
||||||
{`An error occurred. Please try again in a few moments.\n\nError message: ${text}`}
|
{`An error occurred. Please try again in a few moments.\n\nError message: ${text}`}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
) :
|
) : edit ? (
|
||||||
edit ? (
|
<div className="flex min-h-[20px] flex-grow flex-col items-start gap-4 whitespace-pre-wrap">
|
||||||
<div className="flex min-h-[20px] flex-col flex-grow items-start gap-4 whitespace-pre-wrap">
|
|
||||||
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
||||||
|
|
||||||
<div className="markdown prose dark:prose-invert light w-full break-words border-none focus:outline-none"
|
<div
|
||||||
contentEditable={true} ref={textEditor} suppressContentEditableWarning={true}>
|
className="markdown prose dark:prose-invert light w-full break-words border-none focus:outline-none"
|
||||||
|
contentEditable={true}
|
||||||
|
ref={textEditor}
|
||||||
|
suppressContentEditableWarning={true}
|
||||||
|
>
|
||||||
{text}
|
{text}
|
||||||
</div>
|
</div>
|
||||||
<div className="text-center mt-2 flex w-full justify-center">
|
<div className="mt-2 flex w-full justify-center text-center">
|
||||||
<button
|
<button
|
||||||
className="btn relative btn-primary mr-2"
|
className="btn btn-primary relative mr-2"
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
onClick={resubmitMessage}
|
onClick={resubmitMessage}
|
||||||
>
|
>
|
||||||
Save & Submit
|
Save & Submit
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="btn relative btn-neutral"
|
className="btn btn-neutral relative"
|
||||||
onClick={() => enterEdit(true)}
|
onClick={() => enterEdit(true)}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
|
|
@ -174,16 +197,26 @@ export default function Message({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex min-h-[20px] flex-col flex-grow items-start gap-4 whitespace-pre-wrap">
|
<div className="flex min-h-[20px] flex-grow flex-col items-start gap-4 whitespace-pre-wrap">
|
||||||
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
{/* <div className={`${blinker ? 'result-streaming' : ''} markdown prose dark:prose-invert light w-full break-words`}> */}
|
||||||
<div className="markdown prose dark:prose-invert light w-full break-words">
|
<div className="markdown prose dark:prose-invert light w-full break-words">
|
||||||
{!isCreatedByUser ? wrapText(text) : text}
|
{!isCreatedByUser ? (
|
||||||
{blinker && <span className="result-streaming">█</span>}
|
<TextWrapper
|
||||||
|
text={text}
|
||||||
|
generateCursor={generateCursor}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
text
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<HoverButtons model={model} visible={!error && isCreatedByUser && !edit} onClick={() => enterEdit()}/>
|
<HoverButtons
|
||||||
|
model={model}
|
||||||
|
visible={!error && isCreatedByUser && !edit}
|
||||||
|
onClick={() => enterEdit()}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,9 @@ const inLineWrap = (parts) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function TextWrapper({ text }) {
|
export default function TextWrapper({ text, generateCursor }) {
|
||||||
let embedTest = false;
|
let embedTest = false;
|
||||||
|
let result = null;
|
||||||
|
|
||||||
// to match unenclosed code blocks
|
// to match unenclosed code blocks
|
||||||
if (text.match(/```/g)?.length === 1) {
|
if (text.match(/```/g)?.length === 1) {
|
||||||
|
|
@ -137,13 +138,23 @@ export default function TextWrapper({ text }) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return <>{codeParts}</>; // return the wrapped text
|
// return <>{codeParts}</>; // return the wrapped text
|
||||||
|
result = <>{codeParts}</>;
|
||||||
} else if (text.match(markupRegex)) {
|
} else if (text.match(markupRegex)) {
|
||||||
// map over the parts and wrap any text between tildes with <code> tags
|
// map over the parts and wrap any text between tildes with <code> tags
|
||||||
const parts = text.split(markupRegex);
|
const parts = text.split(markupRegex);
|
||||||
const codeParts = inLineWrap(parts);
|
const codeParts = inLineWrap(parts);
|
||||||
return <Markdown options={mdOptions}>{codeParts}</Markdown>; // return the wrapped text
|
// return <>{codeParts}</>; // return the wrapped text
|
||||||
|
result = <>{codeParts}</>;
|
||||||
} else {
|
} else {
|
||||||
return <Markdown options={mdOptions}>{text}</Markdown>;
|
// return <Markdown options={mdOptions}>{text}</Markdown>;
|
||||||
|
result = <Markdown options={mdOptions}>{text}</Markdown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{result}
|
||||||
|
{(<>{generateCursor()}</>)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1251,7 +1251,6 @@ html {
|
||||||
vertical-align: baseline;
|
vertical-align: baseline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* .result-streaming>:not(ol):not(ul):not(pre):last-child:after,
|
/* .result-streaming>:not(ol):not(ul):not(pre):last-child:after,
|
||||||
.result-streaming>ol:last-child li:last-child:after,
|
.result-streaming>ol:last-child li:last-child:after,
|
||||||
.result-streaming>pre:last-child code:after,
|
.result-streaming>pre:last-child code:after,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue