Skip to content

Commit

Permalink
WIP: Create single team per chat
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Dec 18, 2023
1 parent ce5d872 commit e416e3c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
14 changes: 8 additions & 6 deletions src/client/chatConversationHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ export async function addAgentMessageToConversation(
await updateExistingChat(payload);
}

const openAIResponse = {
chat_id: Number(chat_id),
message: response.content,
role: "assistant",
};
if (response.content) {
const openAIResponse = {
chat_id: Number(chat_id),
message: response.content,
role: "assistant",
};

await addNewConversationToChat(openAIResponse);
await addNewConversationToChat(openAIResponse);
}

return {
chat_id: Number(chat_id),
Expand Down
46 changes: 29 additions & 17 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export default function ConversationWrapper() {
const { socket, isConnected } = useSocket();
const [isLoading, setIsLoading] = useState(false);
const chatWindowRef = useRef(null);
const { data: currentChatDetails } = useQuery(getChat, {
const {
data: currentChatDetails,
refetch: refetchChat,
}: { data: any; refetch: any } = useQuery(getChat, {
chatId: Number(id),
});
const [isSubmitButtonDisabled, setIsSubmitButtonDisabled] = useState(false);
const { data: conversations, refetch } = useQuery(
getConversations,
{
Expand All @@ -35,34 +39,33 @@ export default function ConversationWrapper() {
);

const googleRedirectLoginMsg: any = getQueryParam("msg");
const googleRedirectLoginTeamName: any = getQueryParam("team_name");
const googleRedirectLoginTeadId: any = getQueryParam("team_id");
const formInputRef = useCallback(
async (node: any) => {
if (
node !== null &&
googleRedirectLoginMsg &&
googleRedirectLoginTeamName &&
googleRedirectLoginTeadId
) {
if (node !== null && googleRedirectLoginMsg) {
await addMessagesToConversation(googleRedirectLoginMsg);
}
},
[
googleRedirectLoginMsg,
googleRedirectLoginTeamName,
googleRedirectLoginTeadId,
]
[googleRedirectLoginMsg]
);

useEffect(() => {
if (currentChatDetails) {
setIsSubmitButtonDisabled(
currentChatDetails?.team_status === "inprogress"
);
}
}, [currentChatDetails]);

useSocketListener("newConversationAddedToDB", reFetchConversations);

function reFetchConversations() {
refetch();
refetchChat();
}

useEffect(() => {
scrollToBottom();
refetchChat();
}, [conversations]);

const scrollToBottom = () => {
Expand All @@ -83,13 +86,17 @@ export default function ConversationWrapper() {
userQuery
);
setIsLoading(true);
const teamId = googleRedirectLoginMsg
? Number(id)
: // @ts-ignore
currentChatDetails?.team_id;
const response: any = await addAgentMessageToConversation(
Number(id),
messages,
// @ts-ignore
currentChatDetails.team_id
teamId
);
if (response.team_status === "inprogress") {
// setIsSubmitButtonDisabled(true);
socket.emit("newConversationAdded", response.chat_id);
}

Expand Down Expand Up @@ -158,7 +165,12 @@ export default function ConversationWrapper() {
/>
<button
type="submit"
className="text-white absolute right-2.5 bottom-2.5 bg-captn-cta-green hover:bg-captn-cta-green-hover focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-captn-cta-green dark:hover:bg-captn-cta-green-hover dark:focus:ring-blue-800"
className={`text-white absolute right-2.5 bottom-2.5 ${
isSubmitButtonDisabled
? "bg-gray-400 cursor-not-allowed"
: "bg-captn-cta-green hover:bg-captn-cta-green-hover focus:ring-4 focus:outline-none focus:ring-blue-300"
} font-medium rounded-lg text-sm px-4 py-2 dark:bg-captn-cta-green dark:hover:bg-captn-cta-green-hover dark:focus:ring-blue-800`}
disabled={isSubmitButtonDisabled}
>
Send
</button>
Expand Down

0 comments on commit e416e3c

Please sign in to comment.