Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions src/pages/Chat/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,9 @@ import SpeechToText, {
} from '../../../hooks/useSpeechToText';
import { useChatContext } from '../../../store/chat';
import { MessageExtra } from '../../../types';
import { cleanCurrentUrl } from '../../../utils';
import { usePrefilledMessage } from '../hooks/usePrefilledMessage';
import { DropzoneArea } from './DropzoneArea';

/**
* If the current URL contains "?m=...", prefill the message input with the value.
* If the current URL contains "?q=...", prefill and SEND the message.
*/
function getPrefilledContent() {
const searchParams = new URL(window.location.href).searchParams;
return searchParams.get('m') || searchParams.get('q') || '';
}
function isPrefilledSend() {
const searchParams = new URL(window.location.href).searchParams;
return searchParams.has('q');
}
function resetPrefilled() {
cleanCurrentUrl(['m', 'q']);
}

type CallbackSendMessage = (
content: string,
extra: MessageExtra[] | undefined
Expand All @@ -43,7 +27,8 @@ export const ChatInput = memo(
({ convId, onSend }: { convId?: string; onSend: CallbackSendMessage }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const textarea: ChatTextareaApi = useChatTextarea(getPrefilledContent());
const { prefilledContent, isPrefilledSend } = usePrefilledMessage();
const textarea: ChatTextareaApi = useChatTextarea(prefilledContent);
const extraContext = useFileUpload();
const { isGenerating, stopGenerating } = useChatContext();

Expand Down Expand Up @@ -83,20 +68,17 @@ export const ChatInput = memo(

useEffect(() => {
// set textarea with prefilled value
const prefilled = getPrefilledContent();
if (prefilled) {
textarea.setValue(prefilled);
if (prefilledContent) {
textarea.setValue(prefilledContent);
}

// send the prefilled message if needed
// otherwise, focus on the input
if (isPrefilledSend()) sendNewMessage();
if (isPrefilledSend) sendNewMessage();
else textarea.focus();

// no need to keep track of sendNewMessage
resetPrefilled();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [window.location.href]);
}, [isPrefilledSend, prefilledContent]);

return (
<div
Expand Down
36 changes: 36 additions & 0 deletions src/pages/Chat/hooks/usePrefilledMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router';
import { isDev } from '../../../config';

/**
* Custom hook to manage prefilled messages from URL query parameters.
* Looks for 'm' (message) or 'q' (query) parameters.
* If 'q' is present, the message should be sent immediately.
*/
export function usePrefilledMessage() {
const [searchParams, setSearchParams] = useSearchParams();

const [prefilledContent, setPrefilledContent] = useState('');
const [isPrefilledSend, setIsPrefilledSend] = useState(false);

useEffect(() => {
const message = searchParams.get('m') || searchParams.get('q') || '';
const send = searchParams.has('q');
if (isDev)
console.debug('usePrefilledMessage', {
message,
send,
});

setPrefilledContent(message);
setIsPrefilledSend(send);

// Clean up URL parameters after reading them
const cleanedSearchParams = new URLSearchParams(searchParams);
cleanedSearchParams.delete('m');
cleanedSearchParams.delete('q');
setSearchParams(cleanedSearchParams);
}, [searchParams, setSearchParams]);

return { prefilledContent, isPrefilledSend };
}