-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: floating selection toolbar for
translate
, summary
, polish
…
…, `sentiment analysis`, `divide paragraphs`, `ask`
- Loading branch information
Showing
8 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Browser from 'webextension-polyfill' | ||
import { cloneElement, useEffect, useRef, useState } from 'react' | ||
import ConversationCardForSearch from '../ConversationCardForSearch' | ||
import PropTypes from 'prop-types' | ||
import { defaultConfig, getUserConfig } from '../../config.mjs' | ||
import { config as toolsConfig } from '../../content-script/selection-tools' | ||
import { updateRefHeight } from '../../utils/update-ref-height.mjs' | ||
import { initSession } from '../../utils/index.mjs' | ||
|
||
const logo = Browser.runtime.getURL('logo.png') | ||
|
||
function FloatingToolbar(props) { | ||
const [prompt, setPrompt] = useState('') | ||
const [triggered, setTriggered] = useState(false) | ||
const [config, setConfig] = useState(defaultConfig) | ||
const [render, setRender] = useState(false) | ||
const [session] = useState(initSession()) | ||
const toolWindow = useRef(null) | ||
|
||
useEffect(() => { | ||
getUserConfig() | ||
.then(setConfig) | ||
.then(() => setRender(true)) | ||
}, []) | ||
|
||
useEffect(() => { | ||
const listener = (changes) => { | ||
const changedItems = Object.keys(changes) | ||
let newConfig = {} | ||
for (const key of changedItems) { | ||
newConfig[key] = changes[key].newValue | ||
} | ||
setConfig({ ...config, ...newConfig }) | ||
} | ||
Browser.storage.local.onChanged.addListener(listener) | ||
return () => { | ||
Browser.storage.local.onChanged.removeListener(listener) | ||
} | ||
}, [config]) | ||
|
||
if (!render) return <div /> | ||
|
||
const tools = [] | ||
|
||
for (const key in toolsConfig) { | ||
const toolConfig = toolsConfig[key] | ||
tools.push( | ||
cloneElement(toolConfig.icon, { | ||
size: 20, | ||
className: 'gpt-selection-toolbar-button', | ||
title: toolConfig.label, | ||
onClick: () => { | ||
setPrompt(toolConfig.genPrompt(props.selection)) | ||
setTriggered(true) | ||
}, | ||
}), | ||
) | ||
} | ||
|
||
return ( | ||
<div data-theme={config.themeMode}> | ||
{triggered ? ( | ||
<div className="gpt-selection-window" ref={toolWindow}> | ||
<div className="chat-gpt-container"> | ||
<ConversationCardForSearch | ||
session={session} | ||
question={prompt} | ||
onUpdate={() => updateRefHeight(toolWindow)} | ||
/> | ||
</div> | ||
</div> | ||
) : ( | ||
<div className="gpt-selection-toolbar"> | ||
<img src={logo} width="24" height="24" /> | ||
{tools} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
FloatingToolbar.propTypes = { | ||
selection: PropTypes.string.isRequired, | ||
} | ||
|
||
export default FloatingToolbar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
CardHeading, | ||
CardList, | ||
EmojiSmile, | ||
Palette, | ||
QuestionCircle, | ||
Translate, | ||
} from 'react-bootstrap-icons' | ||
|
||
export const config = { | ||
translate: { | ||
icon: <Translate />, | ||
label: 'Translate', | ||
genPrompt: (selection) => `Translate the following into Chinese:"${selection}"`, | ||
}, | ||
summary: { | ||
icon: <CardHeading />, | ||
label: 'Summary', | ||
genPrompt: (selection) => `Summarize the following:"${selection}"`, | ||
}, | ||
polish: { | ||
icon: <Palette />, | ||
label: 'Polish', | ||
genPrompt: (selection) => | ||
`Check the following content for diction and grammar,and polish:"${selection}"`, | ||
}, | ||
sentiment: { | ||
icon: <EmojiSmile />, | ||
label: 'Sentiment Analysis', | ||
genPrompt: (selection) => | ||
`Analyze the sentiments expressed in the following content and make a brief summary:"${selection}"`, | ||
}, | ||
divide: { | ||
icon: <CardList />, | ||
label: 'Divide Paragraphs', | ||
genPrompt: (selection) => | ||
`Divide the following into paragraphs that are easy to read and understand:"${selection}"`, | ||
}, | ||
ask: { | ||
icon: <QuestionCircle />, | ||
label: 'Ask', | ||
genPrompt: (selection) => | ||
`Analyze the following content and express your opinion,or give your answer:"${selection}"`, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters