Skip to content

Commit

Permalink
feat: store conversation to independent page (#120, #138)
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Apr 2, 2023
1 parent fe2ce94 commit 39f83ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { isSafari } from '../utils/is-safari'
import { config as menuConfig } from '../content-script/menu-tools'
import { t, changeLanguage } from 'i18next'
import '../_locales/i18n'
import { openUrl } from '../utils/open-url'

const KEY_ACCESS_TOKEN = 'accessToken'
const cache = new ExpiryMap(10 * 1000)
Expand Down Expand Up @@ -146,6 +147,9 @@ Browser.runtime.onMessage.addListener(async (message) => {
const token = await getChatGptAccessToken()
const data = message.data
await deleteConversation(token, data.conversationId)
} else if (message.type === 'OPEN_URL') {
const data = message.data
openUrl(data.url)
}
})

Expand Down
29 changes: 28 additions & 1 deletion src/components/ConversationCard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Browser from 'webextension-polyfill'
import InputBox from '../InputBox'
import ConversationItem from '../ConversationItem'
import { createElementAtPosition, initSession, isSafari } from '../../utils'
import { DownloadIcon, LinkExternalIcon } from '@primer/octicons-react'
import { DownloadIcon, LinkExternalIcon, ArchiveIcon } from '@primer/octicons-react'
import { WindowDesktop, XLg, Pin } from 'react-bootstrap-icons'
import FileSaver from 'file-saver'
import { render } from 'preact'
Expand All @@ -14,6 +14,7 @@ import { Models } from '../../config/index.mjs'
import { useTranslation } from 'react-i18next'
import DeleteButton from '../DeleteButton'
import { useConfig } from '../../hooks/use-config.mjs'
import { createSession } from '../../config/localSession.mjs'

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

Expand Down Expand Up @@ -295,6 +296,31 @@ function ConversationCard(props) {
setSession(newSession)
}}
/>
{!props.pageMode && (
<span
title={t('Store to Independent Conversation Page')}
className="gpt-util-icon"
onClick={() => {
const newSession = {
...session,
sessionName: new Date().toLocaleString(),
autoClean: false,
sessionId: crypto.randomUUID(),
}
setSession(newSession)
createSession(newSession).then(() =>
Browser.runtime.sendMessage({
type: 'OPEN_URL',
data: {
url: Browser.runtime.getURL('IndependentPanel.html'),
},
}),
)
}}
>
<ArchiveIcon size={16} />
</span>
)}
<span
title={t('Save Conversation')}
className="gpt-util-icon"
Expand Down Expand Up @@ -369,6 +395,7 @@ ConversationCard.propTypes = {
dockable: PropTypes.bool,
onDock: PropTypes.func,
notClampSize: PropTypes.bool,
pageMode: PropTypes.bool,
}

export default memo(ConversationCard)
21 changes: 16 additions & 5 deletions src/config/localSession.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ export const initDefaultSession = async () => {
})
}

export const createSession = async (session) => {
const currentSessions = await getSessions()
if (!session) session = await initDefaultSession()
currentSessions.unshift(session)
export const createSession = async (newSession) => {
let currentSessions
if (newSession) {
const ret = await getSession(newSession.sessionId)
currentSessions = ret.currentSessions
if (ret.session)
currentSessions[
currentSessions.findIndex((session) => session.sessionId === newSession.sessionId)
] = newSession
else currentSessions.unshift(newSession)
} else {
newSession = await initDefaultSession()
currentSessions = await getSessions()
currentSessions.unshift(newSession)
}
await Browser.storage.local.set({ sessions: currentSessions })
return { session, currentSessions }
return { session: newSession, currentSessions }
}

export const deleteSession = async (sessionId) => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/IndependentPanel/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function App() {
<ConversationCard
session={currentSession}
notClampSize={true}
pageMode={true}
onUpdate={(port, session, cData) => {
currentPort.current = port
if (cData.length > 0 && cData[cData.length - 1].done) {
Expand Down

0 comments on commit 39f83ad

Please sign in to comment.