Skip to content

Commit a4f2e27

Browse files
authored
Merge pull request #4 from CodeSignal/initial-message
Add the ability to specify the initial message in a new chat
2 parents aa973fb + eb2d50f commit a4f2e27

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

client/src/components/Chat/ChatView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { memo, useCallback } from 'react';
22
import { useRecoilValue } from 'recoil';
33
import { useForm } from 'react-hook-form';
4-
import { useParams } from 'react-router-dom';
4+
import { useParams, useSearchParams } from 'react-router-dom';
55
import { useGetMessagesByConvoId } from 'librechat-data-provider/react-query';
66
import type { TMessage } from 'librechat-data-provider';
77
import type { ChatFormValues } from '~/common';
@@ -19,10 +19,12 @@ import store from '~/store';
1919

2020
function ChatView({ index = 0 }: { index?: number }) {
2121
const { conversationId } = useParams();
22+
const [searchParams] = useSearchParams();
2223
const rootSubmission = useRecoilValue(store.submissionByIndex(index));
2324
const addedSubmission = useRecoilValue(store.submissionByIndex(index + 1));
2425

2526
const fileMap = useFileMapContext();
27+
const initialMessage = searchParams.get('initialMessage');
2628

2729
const { data: messagesTree = null, isLoading } = useGetMessagesByConvoId(conversationId ?? '', {
2830
select: useCallback(
@@ -65,7 +67,7 @@ function ChatView({ index = 0 }: { index?: number }) {
6567
<Presentation>
6668
{content}
6769
<div className="w-full border-t-0 pl-0 pt-2 dark:border-white/20 md:w-[calc(100%-.5rem)] md:border-t-0 md:border-transparent md:pl-0 md:pt-0 md:dark:border-transparent">
68-
<ChatForm index={index} />
70+
<ChatForm index={index} initialMessage={initialMessage} />
6971
<Footer />
7072
</div>
7173
</Presentation>

client/src/components/Chat/Input/ChatForm.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import SendButton from './SendButton';
3636
import Mention from './Mention';
3737
import store from '~/store';
3838

39-
const ChatForm = ({ index = 0 }) => {
39+
const ChatForm = ({ index = 0, initialMessage = '' }: { index?: number; initialMessage?: string | null }) => {
4040
const submitButtonRef = useRef<HTMLButtonElement>(null);
4141
const textAreaRef = useRef<HTMLTextAreaElement | null>(null);
4242
useQueryParams({ textAreaRef });
@@ -130,6 +130,12 @@ const ChatForm = ({ index = 0 }) => {
130130
},
131131
});
132132

133+
useEffect(() => {
134+
if (initialMessage) {
135+
methods.setValue('text', initialMessage);
136+
}
137+
}, [initialMessage, methods]);
138+
133139
useEffect(() => {
134140
if (!isSearching && textAreaRef.current && !disableInputs) {
135141
textAreaRef.current.focus();

0 commit comments

Comments
 (0)