Skip to content

Commit

Permalink
feat: clear conversation (josStorer#51, josStorer#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 30, 2023
1 parent d0728fc commit 0d16e31
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/_locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@
"Model Name": "Model Name",
"Custom Model API Url": "Custom Model API Url",
"Loading...": "Loading...",
"Feedback": "Feedback"
"Feedback": "Feedback",
"Confirm": "Confirm",
"Clear Conversation": "Clear Conversation"
}
4 changes: 3 additions & 1 deletion src/_locales/zh-hans/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@
"Model Name": "模型名",
"Custom Model API Url": "自定义模型的API地址",
"Loading...": "正在读取...",
"Feedback": "反馈"
"Feedback": "反馈",
"Confirm": "确认",
"Clear Conversation": "清理对话"
}
4 changes: 3 additions & 1 deletion src/_locales/zh-hant/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@
"Model Name": "模型名",
"Custom Model API Url": "自定義模型的API地址",
"Loading...": "正在讀取...",
"Feedback": "反饋"
"Feedback": "反饋",
"Confirm": "確認",
"Clear Conversation": "清理對話"
}
10 changes: 5 additions & 5 deletions src/background/apis/chatgpt-web.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export async function setConversationProperty(token, conversationId, propertyObj
await request(token, 'PATCH', `/conversation/${conversationId}`, propertyObject)
}

export async function deleteConversation(token, conversationId) {
if (conversationId) await setConversationProperty(token, conversationId, { is_visible: false })
}

export async function sendModerations(token, question, conversationId, messageId) {
await request(token, 'POST', `/moderations`, {
conversation_id: conversationId,
Expand All @@ -48,10 +52,6 @@ export async function getModels(token) {
* @param {string} accessToken
*/
export async function generateAnswersWithChatgptWebApi(port, question, session, accessToken) {
const deleteConversation = () => {
setConversationProperty(accessToken, session.conversationId, { is_visible: false })
}

const controller = new AbortController()
const stopListener = (msg) => {
if (msg.stop) {
Expand All @@ -65,7 +65,7 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
port.onDisconnect.addListener(() => {
console.debug('port disconnected')
controller.abort()
deleteConversation()
deleteConversation(accessToken, session.conversationId)
})

const models = await getModels(accessToken).catch(() => {
Expand Down
10 changes: 9 additions & 1 deletion src/background/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Browser from 'webextension-polyfill'
import ExpiryMap from 'expiry-map'
import { generateAnswersWithChatgptWebApi, sendMessageFeedback } from './apis/chatgpt-web'
import {
deleteConversation,
generateAnswersWithChatgptWebApi,
sendMessageFeedback,
} from './apis/chatgpt-web'
import { generateAnswersWithBingWebApi } from './apis/bing-web.mjs'
import {
generateAnswersWithChatgptApi,
Expand Down Expand Up @@ -126,6 +130,10 @@ Browser.runtime.onMessage.addListener(async (message) => {
if (message.type === 'FEEDBACK') {
const token = await getChatGptAccessToken()
await sendMessageFeedback(token, message.data)
} else if (message.type === 'DELETE_CONVERSATION') {
const token = await getChatGptAccessToken()
const data = message.data
await deleteConversation(token, data.conversationId)
}
})

Expand Down
48 changes: 32 additions & 16 deletions src/components/ConversationCard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FloatingToolbar from '../FloatingToolbar'
import { useClampWindowSize } from '../../hooks/use-clamp-window-size'
import { defaultConfig, getUserConfig } from '../../config/index.mjs'
import { useTranslation } from 'react-i18next'
import DeleteButton from '../DeleteButton'

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

Expand Down Expand Up @@ -221,22 +222,37 @@ function ConversationCard(props) {
}}
/>
)}
<span
title={t('Save Conversation')}
className="gpt-util-icon"
style="margin:15px;"
onClick={() => {
let output = ''
session.conversationRecords.forEach((data) => {
output += `${t('Question')}:\n\n${data.question}\n\n${t('Answer')}:\n\n${
data.answer
}\n\n<hr/>\n\n`
})
const blob = new Blob([output], { type: 'text/plain;charset=utf-8' })
FileSaver.saveAs(blob, 'conversation.md')
}}
>
<DownloadIcon size={16} />
<span className="gpt-util-group">
<DeleteButton
size={16}
onConfirm={() => {
port.postMessage({ stop: true })
Browser.runtime.sendMessage({
type: 'DELETE_CONVERSATION',
data: {
conversationId: session.conversationId,
},
})
setConversationItemData([])
setSession(initSession())
}}
/>
<span
title={t('Save Conversation')}
className="gpt-util-icon"
onClick={() => {
let output = ''
session.conversationRecords.forEach((data) => {
output += `${t('Question')}:\n\n${data.question}\n\n${t('Answer')}:\n\n${
data.answer
}\n\n<hr/>\n\n`
})
const blob = new Blob([output], { type: 'text/plain;charset=utf-8' })
FileSaver.saveAs(blob, 'conversation.md')
}}
>
<DownloadIcon size={16} />
</span>
</span>
</div>
<hr />
Expand Down
6 changes: 3 additions & 3 deletions src/components/ConversationItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function ConversationItem({ type, content, session, done, port }) {
<div className={type} dir="auto">
<div className="gpt-header">
<p>{t('You')}:</p>
<div style="display: flex; gap: 15px;">
<div className="gpt-util-group">
<CopyButton contentFn={() => content} size={14} />
{!collapsed ? (
<span
Expand Down Expand Up @@ -47,7 +47,7 @@ export function ConversationItem({ type, content, session, done, port }) {
<p style="white-space: nowrap;">
{session && session.aiName ? `${t(session.aiName)}:` : t('Loading...')}
</p>
<div style="display: flex; gap: 15px; align-items: center; white-space: nowrap;">
<div className="gpt-util-group">
{!done && (
<button
type="button"
Expand Down Expand Up @@ -105,7 +105,7 @@ export function ConversationItem({ type, content, session, done, port }) {
<div className={type} dir="auto">
<div className="gpt-header">
<p>{t('Error')}:</p>
<div style="display: flex; gap: 15px;">
<div className="gpt-util-group">
<CopyButton contentFn={() => content} size={14} />
{!collapsed ? (
<span
Expand Down
54 changes: 54 additions & 0 deletions src/components/DeleteButton/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import { TrashIcon } from '@primer/octicons-react'

DeleteButton.propTypes = {
onConfirm: PropTypes.func.isRequired,
size: PropTypes.number.isRequired,
}

function DeleteButton({ onConfirm, size }) {
const { t } = useTranslation()
const [waitConfirm, setWaitConfirm] = useState(false)
const confirmRef = useRef(null)

useEffect(() => {
if (waitConfirm) confirmRef.current.focus()
}, [waitConfirm])

return (
<span>
<button
ref={confirmRef}
type="button"
className="normal-button"
style={{
fontSize: '10px',
...(waitConfirm ? {} : { display: 'none' }),
}}
onBlur={() => {
setWaitConfirm(false)
}}
onClick={() => {
setWaitConfirm(false)
onConfirm()
}}
>
{t('Confirm')}
</button>
<span
title={t('Clear Conversation')}
className="gpt-util-icon"
style={waitConfirm ? { display: 'none' } : {}}
onClick={() => {
setWaitConfirm(true)
}}
>
<TrashIcon size={size} />
</span>
</span>
)
}

export default DeleteButton
6 changes: 6 additions & 0 deletions src/content-script/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
color: #f08080;
}

.gpt-util-group {
display: flex;
gap: 15px;
align-items: center;
}

.gpt-util-icon {
display: flex;
cursor: pointer;
Expand Down

0 comments on commit 0d16e31

Please sign in to comment.