Skip to content

Commit 8559c21

Browse files
committed
Fixed writer-rewriter, working
1 parent f16ff1b commit 8559c21

File tree

6 files changed

+111
-59
lines changed

6 files changed

+111
-59
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
2424

25-
/old_working_build
25+
/old_working_build
26+
27+
build.zip

public/background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const defaultSettings = {
2323
tone: "as-is",
2424
length: "as-is",
2525
format: "as-is",
26-
context: "",
26+
context: "I am.",
2727
},
2828
write: {
2929
available: {
@@ -34,7 +34,7 @@ const defaultSettings = {
3434
tone: "formal",
3535
length: "medium",
3636
format: "plain-text",
37-
context: "",
37+
context: "I am.",
3838
},
3939
detect: {
4040
languageMapping: {

src/App.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,16 @@ const AppContent = ({ isGenerating, setIsGenerating, isBookmarking, setIsBookmar
252252

253253
const handleTriggerRewrite = async (message, messageID = null, messageType = null) => {
254254
console.log("handleTriggerRewrite message received");
255+
console.log("messageID: ", messageID);
256+
console.log("messageType: ", messageType);
257+
console.log("message: ", message);
255258
setIsGenerating(true);
256259
navigate('/output');
257260
try {
258-
const result = await handleRewrite(message.text);
261+
const result = await handleRewrite(message);
259262
const outputData = {
260263
id: messageID || uuidv4(),
261-
input: message.text,
264+
input: message,
262265
text: result,
263266
timestamp: new Date().toISOString(),
264267
type: messageType || 'Composition'

src/api/rewriteHandler.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const createReWriter = async () => {
2828

2929
// Function to prompt the model and get a response
3030
const promptRewriteModel = async (prompt) => {
31+
console.log("Prompt: ", prompt);
3132
if (!prompt) throw new Error("Prompt is required");
3233

3334
if (!window.ai || !window.ai.rewriter) {
@@ -57,6 +58,7 @@ const promptRewriteModel = async (prompt) => {
5758
// Handler function for external use
5859
export const handleRewrite = async (prompt) => {
5960
try {
61+
console.log("Prompt: ", prompt);
6062
const result = await promptRewriteModel(prompt);
6163
return result.sanitizedResponse;
6264
} catch (error) {

src/api/writeHandler.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ export const createWriterSession = async (tone, length, format, context) => {
1313
export const handleWrite = async (prompt, chosenTone = null, chosenLength = null) => {
1414
if (!prompt) throw new Error("Prompt is required");
1515

16-
const storedSettings = await loadSettings();
17-
const { tone, length, format, context } = storedSettings.write;
16+
try {
17+
const storedSettings = await loadSettings();
18+
const { tone, length, format, context } = storedSettings.write;
1819

19-
await createWriterSession(chosenTone === null ? tone : chosenTone, chosenLength === null ? length : chosenLength, format, context);
20+
await createWriterSession(chosenTone === null ? tone : chosenTone, chosenLength === null ? length : chosenLength, format, context);
2021

21-
const fullResponse = await writer.write(prompt);
22-
return DOMPurify.sanitize(marked.parse(fullResponse));
22+
const fullResponse = await writer.write(prompt);
23+
return DOMPurify.sanitize(marked.parse(fullResponse));
24+
} catch (error) {
25+
console.error("Error during writing process:", error);
26+
throw new Error("Failed to process the writing request.");
27+
}
2328
};

src/components/ui/OutputBox.js

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { motion, AnimatePresence } from 'framer-motion';
1313
import LoadingMessage from './LoadingMessage';
1414

1515
const OutputBox = ({ handleTriggerRewrite, isGenerating }) => {
16-
const [copied, setCopied] = useState(false);
16+
const [copied, setCopied] = useState({});
1717
const [selectedTabs, setSelectedTabs] = useState([]);
1818
const [outputs, setOutputs] = useState([]);
1919
const [error, setError] = useState(null);
@@ -48,13 +48,16 @@ const OutputBox = ({ handleTriggerRewrite, isGenerating }) => {
4848
}
4949
}, [isGenerating]);
5050

51-
const handleCopy = (text) => {
51+
const handleCopy = (text, id) => {
52+
// Clean text before copying
53+
text = text.replace(/<[^>]*>?/gm, '');
5254
navigator.clipboard.writeText(text);
53-
setCopied(true);
54-
setTimeout(() => setCopied(false), 2000);
55+
setCopied((prevCopied) => ({ ...prevCopied, [id]: true }));
56+
setTimeout(() => setCopied((prevCopied) => ({ ...prevCopied, [id]: false })), 2000);
5557
};
5658

5759
const handleRewrite = (text, id, type) => {
60+
console.log('Rewrite:', text, id, type);
5861
handleTriggerRewrite(text, id, type);
5962
};
6063

@@ -99,19 +102,46 @@ const OutputBox = ({ handleTriggerRewrite, isGenerating }) => {
99102
}}
100103
>
101104
<ListItemText primary={query} />
102-
<IconButton
103-
edge="end"
104-
aria-label="search"
105-
onClick={(e) => {
106-
e.stopPropagation();
107-
handleSearch(query);
108-
}}
109-
style={{
110-
color: '#1A73E8',
105+
<Box
106+
sx={{
107+
display: 'flex',
108+
flexDirection: 'column',
109+
alignItems: 'flex-end',
110+
gap: 1,
111111
}}
112112
>
113-
<SearchIcon />
114-
</IconButton>
113+
<Tooltip title="Search">
114+
<IconButton
115+
edge="end"
116+
aria-label="search"
117+
onClick={(e) => {
118+
e.stopPropagation();
119+
handleSearch(query);
120+
}}
121+
style={{
122+
color: '#1A73E8',
123+
}}
124+
>
125+
<SearchIcon />
126+
</IconButton>
127+
</Tooltip>
128+
{/* <Tooltip title="Copy to clipboard">
129+
<IconButton
130+
onClick={(e) => {
131+
e.stopPropagation();
132+
handleCopy(output.text, output.id);
133+
}}
134+
sx={{
135+
color: copied[output.id] ? 'green' : '#1A73E8',
136+
padding: 0.5,
137+
transition: 'color 0.3s',
138+
}}
139+
aria-label="copy"
140+
>
141+
{copied[output.id] ? <CheckIcon fontSize='small'/> : <ContentCopyIcon fontSize='small'/>}
142+
</IconButton>
143+
</Tooltip> */}
144+
</Box>
115145
</ListItem>
116146
))}
117147
</List>
@@ -141,7 +171,7 @@ const OutputBox = ({ handleTriggerRewrite, isGenerating }) => {
141171
width: '100%',
142172
boxShadow: 3,
143173
border: '2px solid #1A73E8',
144-
textAlign: 'center',
174+
// textAlign: 'center',
145175
}}
146176
>
147177
<Box
@@ -183,39 +213,49 @@ const OutputBox = ({ handleTriggerRewrite, isGenerating }) => {
183213
<InfoIcon fontSize='small'/>
184214
</IconButton>
185215
</Tooltip>
186-
<IconButton
187-
onClick={() => handleCopy(output.text)}
188-
sx={{
189-
color: copied ? 'green' : '#1A73E8',
190-
padding: 0.5,
191-
transition: 'color 0.3s',
192-
}}
193-
aria-label="copy"
194-
>
195-
{copied ? <CheckIcon fontSize='small'/> : <ContentCopyIcon fontSize='small'/>}
196-
</IconButton>
197-
<IconButton
198-
onClick={() => handleRewrite(output.text, output.id, output.type)}
199-
sx={{
200-
color: '#1A73E8',
201-
padding: 0.5,
202-
'&:hover': { color: '#1558B0' },
203-
}}
204-
aria-label="rewrite"
205-
>
206-
<EditIcon fontSize='small'/>
207-
</IconButton>
208-
<IconButton
209-
onClick={() => handleDelete(output.id)}
210-
sx={{
211-
color: '#1A73E8',
212-
padding: 0.5,
213-
'&:hover': { color: 'red' },
214-
}}
215-
aria-label="delete"
216-
>
217-
<DeleteIcon fontSize='small'/>
218-
</IconButton>
216+
{output.type !== 'Search' && (
217+
<>
218+
<Tooltip title="Copy to clipboard">
219+
<IconButton
220+
onClick={() => handleCopy(output.text, output.id)}
221+
sx={{
222+
color: copied[output.id] ? 'green' : '#1A73E8',
223+
padding: 0.5,
224+
transition: 'color 0.3s',
225+
}}
226+
aria-label="copy"
227+
>
228+
{copied[output.id] ? <CheckIcon fontSize='small'/> : <ContentCopyIcon fontSize='small'/>}
229+
</IconButton>
230+
</Tooltip>
231+
<Tooltip title="Rewrite">
232+
<IconButton
233+
onClick={() => handleRewrite(output.text, output.id, output.type)}
234+
sx={{
235+
color: '#1A73E8',
236+
padding: 0.5,
237+
'&:hover': { color: '#1558B0' },
238+
}}
239+
aria-label="rewrite"
240+
>
241+
<EditIcon fontSize='small'/>
242+
</IconButton>
243+
</Tooltip>
244+
</>
245+
)}
246+
<Tooltip title="Delete">
247+
<IconButton
248+
onClick={() => handleDelete(output.id)}
249+
sx={{
250+
color: '#1A73E8',
251+
padding: 0.5,
252+
'&:hover': { color: 'red' },
253+
}}
254+
aria-label="delete"
255+
>
256+
<DeleteIcon fontSize='small'/>
257+
</IconButton>
258+
</Tooltip>
219259
</Box>
220260
</Box>
221261
{content}

0 commit comments

Comments
 (0)