Skip to content

Commit

Permalink
fix: useState for session
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 13, 2023
1 parent cf7b227 commit 9716c26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/components/ConversationCardForSearch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ function ConversationCardForSearch(props) {
])
const [isReady, setIsReady] = useState(false)
const [port, setPort] = useState(() => Browser.runtime.connect())
const [session, setSession] = useState(props.session)

useEffect(() => {
if (props.onUpdate) props.onUpdate()
})

useEffect(() => {
// when the page is responsive, session may accumulate redundant data and needs to be cleared after remounting and before making a new request
props.session = initSession({ question: props.question })
port.postMessage({ session: props.session })
const newSession = initSession({ question: props.question })
setSession(newSession)
port.postMessage({ session: newSession })
}, [props.question]) // usually only triggered once

/**
Expand All @@ -58,7 +60,7 @@ function ConversationCardForSearch(props) {
newType,
appended ? copy[index].content + value : value,
)
copy[index].session = { ...props.session }
copy[index].session = { ...session }
copy[index].done = done
return copy
})
Expand All @@ -79,7 +81,7 @@ function ConversationCardForSearch(props) {
UpdateAnswer(msg.answer, false, 'answer')
}
if (msg.session) {
props.session = msg.session
setSession(msg.session)
}
if (msg.done) {
UpdateAnswer('\n<hr/>', true, 'answer', true)
Expand Down Expand Up @@ -133,7 +135,7 @@ function ConversationCardForSearch(props) {
style="margin:15px 15px 10px;"
onClick={() => {
let output = ''
props.session.conversationRecords.forEach((data) => {
session.conversationRecords.forEach((data) => {
output += `Question:\n\n${data.question}\n\nAnswer:\n\n${data.answer}\n\n<hr/>\n\n`
})
const blob = new Blob([output], { type: 'text/plain;charset=utf-8' })
Expand Down Expand Up @@ -166,9 +168,10 @@ function ConversationCardForSearch(props) {
setConversationItemData([...conversationItemData, newQuestion, newAnswer])
setIsReady(false)

props.session.question = question
const newSession = { ...session, question }
setSession(newSession)
try {
port.postMessage({ session: props.session })
port.postMessage({ session: newSession })
} catch (e) {
UpdateAnswer(e, false, 'error')
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ConversationItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export function ConversationItem({ type, content, session, done }) {
<div className="gpt-header">
<p>{session ? 'ChatGPT:' : 'Loading...'}</p>
<div style="display: flex; gap: 15px;">
{done && !session.useApiKey && (
{done && session && session.conversationId && (
<FeedbackForChatGPTWeb
messageId={session.messageId}
conversationId={session.conversationId}
/>
)}
{session && session.conversationId && !session.useApiKey && (
{session && session.conversationId && (
<a
title="Continue on official website"
href={'https://chat.openai.com/chat/' + session.conversationId}
Expand Down
2 changes: 1 addition & 1 deletion src/components/DecisionCardForSearch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function DecisionCardForSearch(props) {
}

DecisionCardForSearch.propTypes = {
session: PropTypes.string.isRequired,
session: PropTypes.object.isRequired,
question: PropTypes.string.isRequired,
siteConfig: PropTypes.object.isRequired,
container: PropTypes.object.isRequired,
Expand Down

0 comments on commit 9716c26

Please sign in to comment.