Skip to content

Commit

Permalink
feat: move session to component level
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 13, 2023
1 parent 2c65549 commit 055ae67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/components/ConversationCardForSearch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function ConversationCardForSearch(props) {

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

/**
Expand All @@ -54,7 +54,7 @@ function ConversationCardForSearch(props) {
newType,
appended ? copy[index].content + value : value,
)
copy[index].session = { ...window.session }
copy[index].session = { ...props.session }
copy[index].done = done
return copy
})
Expand All @@ -75,7 +75,7 @@ function ConversationCardForSearch(props) {
UpdateAnswer(msg.answer, false, 'answer')
}
if (msg.session) {
window.session = msg.session
props.session = msg.session
}
if (msg.done) {
UpdateAnswer('\n<hr/>', true, 'answer', true)
Expand Down Expand Up @@ -129,7 +129,7 @@ function ConversationCardForSearch(props) {
style="margin:15px 15px 10px;"
onClick={() => {
let output = ''
window.session.conversationRecords.forEach((data) => {
props.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 @@ -162,9 +162,9 @@ function ConversationCardForSearch(props) {
setConversationItemData([...conversationItemData, newQuestion, newAnswer])
setIsReady(false)

window.session.question = question
props.session.question = question
try {
port.postMessage({ session: window.session })
port.postMessage({ session: props.session })
} catch (e) {
UpdateAnswer(e, false, 'error')
}
Expand All @@ -175,6 +175,7 @@ function ConversationCardForSearch(props) {
}

ConversationCardForSearch.propTypes = {
session: PropTypes.object.isRequired,
question: PropTypes.string.isRequired,
}

Expand Down
9 changes: 5 additions & 4 deletions src/components/DecisionCardForSearch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ function DecisionCardForSearch(props) {
if (question)
switch (config.triggerMode) {
case 'always':
return <ConversationCardForSearch question={question} />
return <ConversationCardForSearch session={props.session} question={question} />
case 'manually':
if (triggered) {
return <ConversationCardForSearch question={question} />
return <ConversationCardForSearch session={props.session} question={question} />
}
return (
<p
Expand All @@ -107,10 +107,10 @@ function DecisionCardForSearch(props) {
)
case 'questionMark':
if (endsWithQuestionMark(question.trim())) {
return <ConversationCardForSearch question={question} />
return <ConversationCardForSearch session={props.session} question={question} />
}
if (triggered) {
return <ConversationCardForSearch question={question} />
return <ConversationCardForSearch session={props.session} question={question} />
}
return (
<p
Expand All @@ -134,6 +134,7 @@ function DecisionCardForSearch(props) {
}

DecisionCardForSearch.propTypes = {
session: PropTypes.string.isRequired,
question: PropTypes.string.isRequired,
siteConfig: PropTypes.object.isRequired,
container: PropTypes.object.isRequired,
Expand Down
7 changes: 6 additions & 1 deletion src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ async function mountComponent(siteConfig, userConfig) {
const container = document.createElement('div')
container.className = 'chat-gpt-container'
render(
<DecisionCardForSearch question={question} siteConfig={siteConfig} container={container} />,
<DecisionCardForSearch
session={initSession()}
question={question}
siteConfig={siteConfig}
container={container}
/>,
container,
)
}
Expand Down

0 comments on commit 055ae67

Please sign in to comment.