mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 08:33:20 +00:00
wires up the feedback event handler with redux storie
This commit is contained in:
@@ -8,10 +8,12 @@ import {
|
|||||||
fetchAnswer,
|
fetchAnswer,
|
||||||
selectQueries,
|
selectQueries,
|
||||||
selectStatus,
|
selectStatus,
|
||||||
|
updateQuery,
|
||||||
} from './conversationSlice';
|
} from './conversationSlice';
|
||||||
import Send from './../assets/send.svg';
|
import Send from './../assets/send.svg';
|
||||||
import Spinner from './../assets/spinner.svg';
|
import Spinner from './../assets/spinner.svg';
|
||||||
import { Query } from './conversationModels';
|
import { FEEDBACK, Query } from './conversationModels';
|
||||||
|
import { sendFeedback } from './conversationApi';
|
||||||
|
|
||||||
export default function Conversation() {
|
export default function Conversation() {
|
||||||
const queries = useSelector(selectQueries);
|
const queries = useSelector(selectQueries);
|
||||||
@@ -22,7 +24,7 @@ export default function Conversation() {
|
|||||||
|
|
||||||
useEffect(
|
useEffect(
|
||||||
() => endMessageRef?.current?.scrollIntoView({ behavior: 'smooth' }),
|
() => endMessageRef?.current?.scrollIntoView({ behavior: 'smooth' }),
|
||||||
[queries],
|
[queries.length, queries[queries.length - 1]],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleQuestion = (question: string) => {
|
const handleQuestion = (question: string) => {
|
||||||
@@ -30,6 +32,14 @@ export default function Conversation() {
|
|||||||
dispatch(fetchAnswer({ question }));
|
dispatch(fetchAnswer({ question }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleFeedback = (query: Query, feedback: FEEDBACK, index: number) => {
|
||||||
|
const prevFeedback = query.feedback;
|
||||||
|
dispatch(updateQuery({ index, query: { feedback } }));
|
||||||
|
sendFeedback(query.prompt, query.response!, feedback).catch(() =>
|
||||||
|
dispatch(updateQuery({ index, query: { feedback: prevFeedback } })),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const prepResponseView = (query: Query, index: number) => {
|
const prepResponseView = (query: Query, index: number) => {
|
||||||
let responseView;
|
let responseView;
|
||||||
if (query.error) {
|
if (query.error) {
|
||||||
@@ -51,6 +61,9 @@ export default function Conversation() {
|
|||||||
message={query.response}
|
message={query.response}
|
||||||
type={'ANSWER'}
|
type={'ANSWER'}
|
||||||
feedback={query.feedback}
|
feedback={query.feedback}
|
||||||
|
handleFeedback={(feedback: FEEDBACK) =>
|
||||||
|
handleFeedback(query, feedback, index)
|
||||||
|
}
|
||||||
></ConversationBubble>
|
></ConversationBubble>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,17 +12,17 @@ const ConversationBubble = forwardRef<
|
|||||||
type: MESSAGE_TYPE;
|
type: MESSAGE_TYPE;
|
||||||
className?: string;
|
className?: string;
|
||||||
feedback?: FEEDBACK;
|
feedback?: FEEDBACK;
|
||||||
handleFeedback?: () => Promise<void>;
|
handleFeedback?: (feedback: FEEDBACK) => void;
|
||||||
}
|
}
|
||||||
>(function ConversationBubble(
|
>(function ConversationBubble(
|
||||||
{ message, type, className, feedback, handleFeedback },
|
{ message, type, className, feedback, handleFeedback },
|
||||||
ref,
|
ref,
|
||||||
) {
|
) {
|
||||||
const [showFeedback, setShowFeedback] = useState(false);
|
const [showFeedback, setShowFeedback] = useState(false);
|
||||||
const [overriddenFeedback, setOverriddenFeedback] = useState<
|
// const [overriddenFeedback, setOverriddenFeedback] = useState<
|
||||||
FEEDBACK | undefined
|
// FEEDBACK | undefined
|
||||||
>(undefined);
|
// >(undefined);
|
||||||
const effectiveFeedback = overriddenFeedback ?? feedback;
|
// const effectiveFeedback = overriddenFeedback ?? feedback;
|
||||||
let bubble;
|
let bubble;
|
||||||
|
|
||||||
if (type === 'QUESTION') {
|
if (type === 'QUESTION') {
|
||||||
@@ -57,7 +57,7 @@ const ConversationBubble = forwardRef<
|
|||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className={`mr-2 flex items-center justify-center ${
|
className={`mr-2 flex items-center justify-center ${
|
||||||
effectiveFeedback === 'LIKE' || (type !== 'ERROR' && showFeedback)
|
feedback === 'LIKE' || (type !== 'ERROR' && showFeedback)
|
||||||
? ''
|
? ''
|
||||||
: 'invisible'
|
: 'invisible'
|
||||||
}`}
|
}`}
|
||||||
@@ -65,17 +65,14 @@ const ConversationBubble = forwardRef<
|
|||||||
<Like
|
<Like
|
||||||
fill="none"
|
fill="none"
|
||||||
className={`cursor-pointer ${
|
className={`cursor-pointer ${
|
||||||
effectiveFeedback === 'LIKE'
|
feedback === 'LIKE' ? 'fill-blue-1000' : 'hover:fill-gray-4000'
|
||||||
? 'fill-blue-1000'
|
|
||||||
: 'hover:fill-gray-4000'
|
|
||||||
}`}
|
}`}
|
||||||
onClick={() => setOverriddenFeedback('LIKE')}
|
onClick={() => handleFeedback?.('LIKE')}
|
||||||
></Like>
|
></Like>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className={`mr-10 flex items-center justify-center ${
|
className={`mr-10 flex items-center justify-center ${
|
||||||
effectiveFeedback === 'DISLIKE' ||
|
feedback === 'DISLIKE' || (type !== 'ERROR' && showFeedback)
|
||||||
(type !== 'ERROR' && showFeedback)
|
|
||||||
? ''
|
? ''
|
||||||
: 'invisible'
|
: 'invisible'
|
||||||
}`}
|
}`}
|
||||||
@@ -83,11 +80,9 @@ const ConversationBubble = forwardRef<
|
|||||||
<Dislike
|
<Dislike
|
||||||
fill="none"
|
fill="none"
|
||||||
className={`cursor-pointer ${
|
className={`cursor-pointer ${
|
||||||
effectiveFeedback === 'DISLIKE'
|
feedback === 'DISLIKE' ? 'fill-red-2000' : 'hover:fill-gray-4000'
|
||||||
? 'fill-red-2000'
|
|
||||||
: 'hover:fill-gray-4000'
|
|
||||||
}`}
|
}`}
|
||||||
onClick={() => setOverriddenFeedback('DISLIKE')}
|
onClick={() => handleFeedback?.('DISLIKE')}
|
||||||
></Dislike>
|
></Dislike>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -30,6 +30,16 @@ export const conversationSlice = createSlice({
|
|||||||
addQuery(state, action: PayloadAction<Query>) {
|
addQuery(state, action: PayloadAction<Query>) {
|
||||||
state.queries.push(action.payload);
|
state.queries.push(action.payload);
|
||||||
},
|
},
|
||||||
|
updateQuery(
|
||||||
|
state,
|
||||||
|
action: PayloadAction<{ index: number; query: Partial<Query> }>,
|
||||||
|
) {
|
||||||
|
const index = action.payload.index;
|
||||||
|
state.queries[index] = {
|
||||||
|
...state.queries[index],
|
||||||
|
...action.payload.query,
|
||||||
|
};
|
||||||
|
},
|
||||||
},
|
},
|
||||||
extraReducers(builder) {
|
extraReducers(builder) {
|
||||||
builder
|
builder
|
||||||
@@ -55,5 +65,5 @@ export const selectQueries = (state: RootState) => state.conversation.queries;
|
|||||||
|
|
||||||
export const selectStatus = (state: RootState) => state.conversation.status;
|
export const selectStatus = (state: RootState) => state.conversation.status;
|
||||||
|
|
||||||
export const { addQuery } = conversationSlice.actions;
|
export const { addQuery, updateQuery } = conversationSlice.actions;
|
||||||
export default conversationSlice.reducer;
|
export default conversationSlice.reducer;
|
||||||
|
|||||||
Reference in New Issue
Block a user