forked from wong2/chatgpt-google-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to add new prompt for different sites (#2)
* Install @geist-ui/icons package * Add PromptCard component * Update <main> tag width from 500 to 600 * Update PromptCard save button style * Display error toast when saving new prompt fails * Handle onClick event of remove icon in PromptCard * Display prompt overides using PromptCard * Add AddNewPromptModal component * Use matching prompt for current hostname * Fix: PromptCard disappears when prompt is updated * Validate input first when adding new prompt * Initialize states in AddNewPromptModal before close
- Loading branch information
1 parent
774608e
commit 98dbeb4
Showing
11 changed files
with
294 additions
and
45 deletions.
There are no files selected for viewing
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
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
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,93 @@ | ||
import { Input, Modal, Text, Textarea, useToasts } from '@geist-ui/core' | ||
import { useState } from 'preact/hooks' | ||
import { isValidHttpUrl } from '../content-script/utils' | ||
|
||
function AddNewPromptModal(props: { | ||
visible: boolean | ||
onClose: () => void | ||
onSave: (newOverride: { site: string; prompt: string }) => Promise<void> | ||
}) { | ||
const { visible, onClose, onSave } = props | ||
const [site, setSite] = useState<string>('') | ||
const [siteError, setSiteError] = useState<boolean>(false) | ||
const [prompt, setPrompt] = useState<string>('') | ||
const [promptError, setPromptError] = useState<boolean>(false) | ||
const { setToast } = useToasts() | ||
|
||
function validateInput() { | ||
const isSiteValid = isValidHttpUrl(site) | ||
setSiteError(!isSiteValid) | ||
if (!isSiteValid) { | ||
return false | ||
} | ||
const isPromptValid = prompt.trim().length > 0 | ||
setPromptError(!isPromptValid) | ||
return isPromptValid | ||
} | ||
|
||
function close() { | ||
setSite('') | ||
setSiteError(false) | ||
setPrompt('') | ||
setPromptError(false) | ||
onClose() | ||
} | ||
|
||
return ( | ||
<Modal visible={visible} onClose={onClose}> | ||
<Modal.Title>Add New Prompt</Modal.Title> | ||
<Modal.Content> | ||
<Input | ||
width={'100%'} | ||
clearable | ||
label="site" | ||
placeholder="https://arxiv.org/" | ||
onChange={(e) => setSite(e.target.value)} | ||
> | ||
{siteError && ( | ||
<Text small type="error"> | ||
Site is not valid | ||
</Text> | ||
)} | ||
</Input> | ||
{promptError && ( | ||
<div className="mt-3 mb-2 px-1"> | ||
<Text small type="error"> | ||
Prompt cannot be empty | ||
</Text> | ||
</div> | ||
)} | ||
<Textarea | ||
my={promptError ? 0 : 1} | ||
value={prompt} | ||
width="100%" | ||
height="10em" | ||
placeholder="Type prompt here" | ||
onChange={(event) => setPrompt(event.target.value)} | ||
/> | ||
</Modal.Content> | ||
<Modal.Action passive onClick={() => close()}> | ||
Cancel | ||
</Modal.Action> | ||
<Modal.Action | ||
onClick={() => { | ||
if (!validateInput()) { | ||
return | ||
} | ||
onSave({ site, prompt }) | ||
.then(() => { | ||
setToast({ text: 'New Prompt saved', type: 'success' }) | ||
close() | ||
}) | ||
.catch(() => { | ||
setToast({ text: 'Failed to save prompt', type: 'error' }) | ||
}) | ||
}} | ||
> | ||
Save | ||
</Modal.Action> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default AddNewPromptModal |
Oops, something went wrong.