Skip to content

Commit 39f83ad

Browse files
committed
feat: store conversation to independent page (#120, #138)
1 parent fe2ce94 commit 39f83ad

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/background/index.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { isSafari } from '../utils/is-safari'
2626
import { config as menuConfig } from '../content-script/menu-tools'
2727
import { t, changeLanguage } from 'i18next'
2828
import '../_locales/i18n'
29+
import { openUrl } from '../utils/open-url'
2930

3031
const KEY_ACCESS_TOKEN = 'accessToken'
3132
const cache = new ExpiryMap(10 * 1000)
@@ -146,6 +147,9 @@ Browser.runtime.onMessage.addListener(async (message) => {
146147
const token = await getChatGptAccessToken()
147148
const data = message.data
148149
await deleteConversation(token, data.conversationId)
150+
} else if (message.type === 'OPEN_URL') {
151+
const data = message.data
152+
openUrl(data.url)
149153
}
150154
})
151155

src/components/ConversationCard/index.jsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Browser from 'webextension-polyfill'
44
import InputBox from '../InputBox'
55
import ConversationItem from '../ConversationItem'
66
import { createElementAtPosition, initSession, isSafari } from '../../utils'
7-
import { DownloadIcon, LinkExternalIcon } from '@primer/octicons-react'
7+
import { DownloadIcon, LinkExternalIcon, ArchiveIcon } from '@primer/octicons-react'
88
import { WindowDesktop, XLg, Pin } from 'react-bootstrap-icons'
99
import FileSaver from 'file-saver'
1010
import { render } from 'preact'
@@ -14,6 +14,7 @@ import { Models } from '../../config/index.mjs'
1414
import { useTranslation } from 'react-i18next'
1515
import DeleteButton from '../DeleteButton'
1616
import { useConfig } from '../../hooks/use-config.mjs'
17+
import { createSession } from '../../config/localSession.mjs'
1718

1819
const logo = Browser.runtime.getURL('logo.png')
1920

@@ -295,6 +296,31 @@ function ConversationCard(props) {
295296
setSession(newSession)
296297
}}
297298
/>
299+
{!props.pageMode && (
300+
<span
301+
title={t('Store to Independent Conversation Page')}
302+
className="gpt-util-icon"
303+
onClick={() => {
304+
const newSession = {
305+
...session,
306+
sessionName: new Date().toLocaleString(),
307+
autoClean: false,
308+
sessionId: crypto.randomUUID(),
309+
}
310+
setSession(newSession)
311+
createSession(newSession).then(() =>
312+
Browser.runtime.sendMessage({
313+
type: 'OPEN_URL',
314+
data: {
315+
url: Browser.runtime.getURL('IndependentPanel.html'),
316+
},
317+
}),
318+
)
319+
}}
320+
>
321+
<ArchiveIcon size={16} />
322+
</span>
323+
)}
298324
<span
299325
title={t('Save Conversation')}
300326
className="gpt-util-icon"
@@ -369,6 +395,7 @@ ConversationCard.propTypes = {
369395
dockable: PropTypes.bool,
370396
onDock: PropTypes.func,
371397
notClampSize: PropTypes.bool,
398+
pageMode: PropTypes.bool,
372399
}
373400

374401
export default memo(ConversationCard)

src/config/localSession.mjs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,23 @@ export const initDefaultSession = async () => {
1212
})
1313
}
1414

15-
export const createSession = async (session) => {
16-
const currentSessions = await getSessions()
17-
if (!session) session = await initDefaultSession()
18-
currentSessions.unshift(session)
15+
export const createSession = async (newSession) => {
16+
let currentSessions
17+
if (newSession) {
18+
const ret = await getSession(newSession.sessionId)
19+
currentSessions = ret.currentSessions
20+
if (ret.session)
21+
currentSessions[
22+
currentSessions.findIndex((session) => session.sessionId === newSession.sessionId)
23+
] = newSession
24+
else currentSessions.unshift(newSession)
25+
} else {
26+
newSession = await initDefaultSession()
27+
currentSessions = await getSessions()
28+
currentSessions.unshift(newSession)
29+
}
1930
await Browser.storage.local.set({ sessions: currentSessions })
20-
return { session, currentSessions }
31+
return { session: newSession, currentSessions }
2132
}
2233

2334
export const deleteSession = async (sessionId) => {

src/pages/IndependentPanel/App.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function App() {
146146
<ConversationCard
147147
session={currentSession}
148148
notClampSize={true}
149+
pageMode={true}
149150
onUpdate={(port, session, cData) => {
150151
currentPort.current = port
151152
if (cData.length > 0 && cData[cData.length - 1].done) {

0 commit comments

Comments
 (0)