Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove setTimeout and code cleanup #39

Merged
Merged
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
57 changes: 32 additions & 25 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { useState, useRef, useEffect } from "react";
import { useState, useRef, useEffect, useCallback } from "react";
import { useParams } from "react-router";
import { Redirect, useLocation } from "react-router-dom";

Expand All @@ -8,8 +8,6 @@ import updateConversation from "@wasp/actions/updateConversation";
import getAgentResponse from "@wasp/actions/getAgentResponse";
import getConversations from "@wasp/queries/getConversations";

// import type { Conversation } from "@wasp/entities";

import ConversationsList from "./ConversationList";
import Loader from "./Loader";

Expand All @@ -22,20 +20,19 @@ function getQueryParam(paramName: string) {
);
}

export async function triggerSubmit(
export function setRedirectMsg(formInputRef: any, loginMsgQuery: string) {
if (loginMsgQuery) {
formInputRef.value = decodeURIComponent(loginMsgQuery);
}
}

export function triggerSubmit(
node: any,
loginMsgQuery: string,
// Todo: remove the below ignore comment
// @ts-ignore
submitBtnRef,
// Todo: remove the below ignore comment
// @ts-ignore
formInputRef
formInputRef: any
) {
if (loginMsgQuery) {
setTimeout(() => {
formInputRef.current.value = decodeURIComponent(loginMsgQuery);
submitBtnRef.current.click();
}, 1500); // todo: remove timeout and implement a proper fix
if (loginMsgQuery && formInputRef && formInputRef.value !== "") {
node.click();
}
}

Expand All @@ -44,10 +41,27 @@ export default function ConversationWrapper() {
// @ts-ignore
const { id } = useParams();
const [isLoading, setIsLoading] = useState(false);
const loginMsgQuery = getQueryParam("msg");
const chatContainerRef = useRef(null);
const submitBtnRef = useRef(null);
const formInputRef = useRef(null);

const loginMsgQuery: any = getQueryParam("msg");
const formInputRef = useCallback(
(node: any) => {
if (node !== null) {
setRedirectMsg(node, loginMsgQuery);
}
},
[loginMsgQuery]
);

const submitBtnRef = useCallback(
(node: any) => {
if (node !== null) {
triggerSubmit(node, loginMsgQuery, formInputRef);
}
},
[loginMsgQuery, formInputRef]
);

const {
data: conversations,
isLoading: isConversationLoading,
Expand All @@ -60,13 +74,6 @@ export default function ConversationWrapper() {
{ enabled: !!id }
);

// componentDidMount
useEffect(() => {
// Todo: remove the below ignore comment
// @ts-ignore
triggerSubmit(loginMsgQuery, submitBtnRef, formInputRef);
}, [loginMsgQuery]);

useEffect(() => {
if (chatContainerRef.current) {
// Todo: remove the below ignore comment
Expand Down
Loading