From b4b47912fc2915d5fbc998aea15afc98e8da5eb4 Mon Sep 17 00:00:00 2001 From: Kyle Watson Date: Thu, 25 Jan 2024 15:41:30 +0100 Subject: [PATCH 1/2] feat: Use new full message response to capture IDs --- src/context/ThreadContext.tsx | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/context/ThreadContext.tsx b/src/context/ThreadContext.tsx index b659bb3..d306203 100644 --- a/src/context/ThreadContext.tsx +++ b/src/context/ThreadContext.tsx @@ -322,15 +322,7 @@ const ThreadProvider = ({ type: "SET_THREAD", payload: { id: id, - thread: { - ...newThread, - messages: newThread.messages.map( - (message: Message["data"]) => ({ - id: Math.random(), - data: message, - }), - ), - }, + thread: newThread, }, }); setSelectedThread(newThread.id); @@ -359,7 +351,7 @@ const ThreadProvider = ({ const reply = await response.json(); dispatch({ type: "ADD_MESSAGE", - payload: { id, message: { id: Math.random(), data: reply } }, + payload: { id, message: reply }, }); } catch (error) { dispatch({ From 329537fd374e78dd506f7804c39740458a9dd41c Mon Sep 17 00:00:00 2001 From: Kyle Watson Date: Thu, 25 Jan 2024 16:04:11 +0100 Subject: [PATCH 2/2] feat: Send feedback to api when message is sent. --- src/components/ChatWindow/ChatWindow.tsx | 58 ++++++++++++++++++++---- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/src/components/ChatWindow/ChatWindow.tsx b/src/components/ChatWindow/ChatWindow.tsx index c1a395f..ebeed80 100644 --- a/src/components/ChatWindow/ChatWindow.tsx +++ b/src/components/ChatWindow/ChatWindow.tsx @@ -10,11 +10,12 @@ import { Paper, Skeleton, Stack, - styled, TextField, Typography, + styled, } from "@mui/material"; import React, { useEffect, useRef, useState } from "react"; +import { useAuthContext } from "../../context/AuthContext"; import { useSettings } from "../../context/SettingsContext"; import { Thread, useThreadContext } from "../../context/ThreadContext"; import PluginSelector from "../PluginSelector/PluginSelector"; @@ -79,10 +80,18 @@ const Message = ({ ); }; +export enum FeedbackRating { + BAD = 2, + NEUTRAL = 3, + GOOD = 4, +} + // MainContent component const MainContent: React.FC = () => { + const { credential, checkExpired } = useAuthContext(); + const [addingFeedback, setAddingFeedback] = useState< - "positive" | "negative" | "neutral" | undefined + FeedbackRating | undefined >(); const { postMessage, threads, selectedThreadId } = useThreadContext(); // State for the chat message and text field rows @@ -107,12 +116,37 @@ const MainContent: React.FC = () => { chatEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages, addingFeedback]); + const sendFeedback = () => { + if (checkExpired()) return; + const messageId = messages?.[messages.length - 1].id; + if (!messageId) return; + + const url = new URL(import.meta.env.VITE_API_BASE_URL || ""); + url.pathname = `api/message/${messageId}/feedback`; + + fetch(url, { + method: "POST", + headers: { + Authorization: `Bearer ${credential?.credential}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + text: message, + rating: addingFeedback, + }), + }).then((response) => { + if (!response.ok) { + throw new Error("Failed to fetch"); + } + }); + }; + // Function to handle sending a message const handleSendMessage = () => { if (!message || !selectedThread || selectedThread.loading) return; if (addingFeedback) { - // TODO: send feedback + sendFeedback(); setAddingFeedback(undefined); } else { postMessage( @@ -195,9 +229,9 @@ const MainContent: React.FC = () => { caption="" message={`Can you share a bit more info on what you ${ { - neutral: "think of", - negative: "dislike about", - positive: "like about", + [FeedbackRating.NEUTRAL]: "think of", + [FeedbackRating.BAD]: "dislike about", + [FeedbackRating.GOOD]: "like about", }[addingFeedback] } the previous response or your general experience with the app?`} > @@ -219,14 +253,20 @@ const MainContent: React.FC = () => { paddingX={2} justifyContent={"flex-end"} > - - setAddingFeedback("negative")}> + setAddingFeedback(FeedbackRating.BAD)} + > - setAddingFeedback("positive")}> + setAddingFeedback(FeedbackRating.GOOD)} + >