Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit e162178

Browse files
committed
fix: minor bugs, TODO: session management
1 parent fa7a3b1 commit e162178

5 files changed

Lines changed: 42 additions & 12 deletions

File tree

src/apis/chatgpt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
/* eslint-disable @typescript-eslint/no-empty-function */
23
/* eslint-disable @typescript-eslint/no-explicit-any */
34
import { isEmpty } from 'lodash-es'
@@ -82,7 +83,7 @@ export async function generateAnswersWithChatgptWebApi(
8283
port.onDisconnect.addListener(() => {
8384
console.debug('port disconnected')
8485
controller.abort()
85-
deleteConversation()
86+
// deleteConversation()
8687
})
8788

8889
const models = await getModels(accessToken).catch(() => {

src/components/ChatCard.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,27 @@ function usePrevious(value: any) {
4444
}
4545

4646
function ChatCard(props: ChatCardProps) {
47+
// Component states
4748
const [isReady, setIsReady] = useState(!props.question)
4849
const [port, setPort] = useState(() => Browser.runtime.connect())
4950
const [session, setSession] = useState(props.session)
51+
const [config, setConfig] = useState(defaultConfig)
52+
53+
const prevQuestion = usePrevious(props.question)
5054
const windowSize = useClampWindowSize([0, Infinity], [250, 1100])
5155
const bodyRef = useRef<HTMLDivElement>(null)
56+
57+
// Load and set user configuration
58+
useEffect(() => {
59+
getUserConfig().then(setConfig)
60+
}, [])
61+
62+
// Handle onUpdate callback if provided
63+
useEffect(() => {
64+
if (props.onUpdate) props.onUpdate()
65+
})
66+
67+
// Initialize chatItemData
5268
const [chatItemData, setChatItemData] = useState<ChatItemData[]>(
5369
(() => {
5470
if (props.session.conversationRecords?.length === 0) {
@@ -65,16 +81,6 @@ function ChatCard(props: ChatCardProps) {
6581
}
6682
})(),
6783
)
68-
const [config, setConfig] = useState(defaultConfig)
69-
const prevQuestion = usePrevious(props.question)
70-
71-
useEffect(() => {
72-
getUserConfig().then(setConfig)
73-
}, [])
74-
75-
useEffect(() => {
76-
if (props.onUpdate) props.onUpdate()
77-
})
7884

7985
useEffect(() => {
8086
if (props.question && (!prevQuestion || prevQuestion !== props.question) && isReady) {
@@ -124,6 +130,7 @@ function ChatCard(props: ChatCardProps) {
124130
useEffect(() => {
125131
const listener = () => {
126132
setPort(Browser.runtime.connect())
133+
setIsReady(true)
127134
}
128135
port.onDisconnect.addListener(listener)
129136
return () => {

src/configs/userConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export async function updateUserConfig(updates: Partial<UserConfigType>) {
175175

176176
export async function getPreferredLanguage() {
177177
return getUserConfig().then((config) => {
178-
if (config.uiLanguage === 'Auto') {
178+
if (config.chatLanguage === 'Auto') {
179179
return getUILanguage()
180180
}
181181
return config.chatLanguage

src/content/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable @typescript-eslint/no-unused-vars */
33
/* eslint-disable @typescript-eslint/no-non-null-assertion */
44
import { render } from 'preact'
5+
import Browser from 'webextension-polyfill'
56
import '../../styles/base.css'
67
import '../../styles/styles.scss'
78
import ChatContainer from '../components/ChatContainer'
@@ -144,6 +145,17 @@ async function run() {
144145
window.postMessage({ type: 'NEW_PROMPT', prompt }, '*')
145146
}
146147

148+
const onUrlChange = () => {
149+
// Send a message to the background script to delete the conversation when the context changes.
150+
const message = {
151+
type: 'DELETE_CONVERSATION',
152+
conversationId: session.conversationId,
153+
}
154+
Browser.runtime.sendMessage(message)
155+
}
156+
157+
window.addEventListener('popstate', onUrlChange)
158+
147159
// Call mountChatContainer with the initialQuestion as the initial prompt
148160
mountChatContainer(session, initialQuestion, siteConfig)
149161
attachToolbar(session, handlePromptGenerated)

src/service/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
generateAnswersWithChatgptWebApi,
88
generateAnswersWithGptCompletionApi,
99
sendMessageFeedback,
10+
setConversationProperty,
1011
} from '../apis'
1112
import {
1213
chatgptApiModelKeys,
@@ -17,6 +18,13 @@ import {
1718
} from '../configs'
1819
import { cache, getAccessToken, KEY_ACCESS_TOKEN } from '../utils'
1920

21+
async function deleteConversation(conversationId: string) {
22+
const accessToken = await getAccessToken()
23+
if (conversationId) {
24+
await setConversationProperty(accessToken, conversationId, { is_visible: false })
25+
}
26+
}
27+
2028
Browser.runtime.onConnect.addListener((port) => {
2129
console.debug('connected')
2230
port.onMessage.addListener(async (msg) => {
@@ -75,6 +83,8 @@ Browser.runtime.onMessage.addListener(async (message) => {
7583
return getAccessToken()
7684
} else if (message.type === 'OPEN_LECTURE') {
7785
Browser.tabs.create({ url: 'https://lecture.entelecheia.ai' })
86+
} else if (message.type === 'DELETE_CONVERSATION') {
87+
deleteConversation(message.conversationId)
7888
}
7989
})
8090

0 commit comments

Comments
 (0)