Skip to content

Commit 4b25d0a

Browse files
committed
added write settings in compose tab
1 parent b7ee87f commit 4b25d0a

File tree

3 files changed

+106
-76
lines changed

3 files changed

+106
-76
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ const AppContent = ({ isGenerating, setIsGenerating, isBookmarking, setIsBookmar
206206
};
207207
}, [navigate, setIsGenerating, setIsBookmarking, setError]);
208208

209-
const handleQuestionSubmit = async (question) => {
209+
const handleQuestionSubmit = async (question, tone = null, length = null) => {
210210
setIsGenerating(true);
211211
navigate('/output');
212212
try {
213-
const result = await handleWrite(question);
213+
const result = await handleWrite(question, tone, length);
214214
const outputData = {
215215
id: uuidv4(),
216216
input: question,

src/api/writeHandler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export const createWriterSession = async (tone, length, format, context) => {
1010
writer = await window.ai.writer.create({ tone, length, format, sharedContext: context });
1111
};
1212

13-
export const handleWrite = async (prompt) => {
13+
export const handleWrite = async (prompt, chosenTone = null, chosenLength = null) => {
1414
if (!prompt) throw new Error("Prompt is required");
1515

1616
const storedSettings = await loadSettings();
1717
const { tone, length, format, context } = storedSettings.write;
1818

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

2121
const fullResponse = await writer.write(prompt);
2222
return DOMPurify.sanitize(marked.parse(fullResponse));

src/components/ui/QuestionBox.js

Lines changed: 102 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
import React, { useState } from 'react';
2-
import { Box, TextField, IconButton } from '@mui/material';
1+
import React, { useState, useEffect } from 'react';
2+
import { Box, TextField, IconButton, Select, MenuItem, Tooltip, Typography, CircularProgress } from '@mui/material';
33
import SendIcon from '@mui/icons-material/Send';
44
import CloseIcon from '@mui/icons-material/Close';
55
import SearchIcon from '@mui/icons-material/Search';
6+
import { loadSettings, saveSettings } from "../../api/settingsStorage";
67

78
const QuestionBox = ({ compose, closeBox, enhanceQuery }) => {
89
const [question, setQuestion] = useState("");
10+
const [settings, setSettings] = useState({});
11+
const [loading, setLoading] = useState(true); // Track loading state
912

10-
const handleSubmit = () => {
13+
// Load settings on component mount
14+
useEffect(() => {
15+
const fetchSettings = async () => {
16+
const storedSettings = await loadSettings();
17+
const filteredSettings = Object.keys(storedSettings)
18+
.filter(key => key !== "detect")
19+
.reduce((obj, key) => {
20+
obj[key] = storedSettings[key];
21+
return obj;
22+
}, {});
23+
setSettings(filteredSettings);
24+
console.log('Settings:', filteredSettings); // Log the settings
25+
setLoading(false); // Mark loading as complete
26+
};
27+
fetchSettings();
28+
}, []);
29+
30+
const handleSubmit = async () => {
1131
if (question.trim()) {
32+
await saveSettings(settings);
1233
compose(question);
1334
setQuestion("");
1435
}
@@ -20,90 +41,99 @@ const QuestionBox = ({ compose, closeBox, enhanceQuery }) => {
2041
}
2142
};
2243

44+
if (loading) {
45+
// Show a loading spinner while settings are being fetched
46+
return (
47+
<Box
48+
sx={{
49+
display: 'flex',
50+
justifyContent: 'center',
51+
alignItems: 'center',
52+
height: '100vh', // Adjust as needed
53+
backgroundColor: 'background.default',
54+
}}
55+
>
56+
<CircularProgress />
57+
</Box>
58+
);
59+
}
60+
2361
return (
2462
<Box
2563
sx={{
2664
position: 'relative',
2765
backgroundColor: 'background.default',
28-
color: 'text.primary',
29-
borderRadius: 2,
30-
p: 2,
31-
mt: 2,
32-
width: '100%',
66+
padding: 2,
67+
borderRadius: 1,
3368
boxShadow: 3,
34-
border: '2px solid #1A73E8',
35-
textAlign: 'center',
3669
}}
3770
>
38-
<Box
39-
sx={{
40-
display: 'flex',
41-
alignItems: 'flex-start',
42-
}}
43-
>
44-
<TextField
45-
fullWidth
46-
multiline
47-
rows={4}
48-
variant="outlined"
49-
value={question}
50-
onChange={(e) => setQuestion(e.target.value)}
51-
placeholder="Type your question here..."
52-
sx={{
53-
flexGrow: 1,
54-
bgcolor: 'background.paper',
55-
color: 'text.primary',
56-
}}
57-
/>
58-
<Box
71+
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
72+
<Typography
73+
variant="h6"
5974
sx={{
75+
background: 'linear-gradient(90deg, #4285F4, #EA4335)',
76+
WebkitBackgroundClip: 'text',
77+
WebkitTextFillColor: 'transparent',
6078
display: 'flex',
61-
flexDirection: 'column',
62-
marginLeft: 1,
79+
alignItems: 'flex-end',
80+
lineHeight: 2,
6381
}}
6482
>
65-
<IconButton
66-
onClick={closeBox}
67-
sx={{
68-
color: '#1A73E8',
69-
'&:hover': {
70-
color: '#1558B0',
71-
},
72-
marginBottom: 1,
73-
}}
74-
aria-label="close"
75-
>
76-
<CloseIcon />
77-
</IconButton>
78-
<IconButton
79-
onClick={handleEnhanceQuery}
80-
sx={{
81-
color: '#1A73E8',
82-
'&:hover': {
83-
color: '#1558B0',
84-
},
85-
marginBottom: 1,
86-
}}
87-
aria-label="search"
88-
>
89-
<SearchIcon />
90-
</IconButton>
91-
<IconButton
92-
onClick={handleSubmit}
93-
sx={{
94-
color: '#1A73E8',
95-
'&:hover': {
96-
color: '#1558B0',
97-
},
98-
}}
99-
aria-label="submit"
100-
>
101-
<SendIcon />
102-
</IconButton>
83+
Compose
84+
</Typography>
85+
<IconButton onClick={closeBox}>
86+
<CloseIcon />
87+
</IconButton>
88+
</Box>
89+
<TextField
90+
fullWidth
91+
multiline
92+
rows={4}
93+
value={question}
94+
onChange={(e) => setQuestion(e.target.value)}
95+
placeholder="Type your question here..."
96+
variant="outlined"
97+
sx={{ mb: 2 }}
98+
/>
99+
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
100+
<Select
101+
value={settings["write"]?.tone || ""}
102+
onChange={(e) => setSettings({ ...settings, "write": { ...settings["write"], tone: e.target.value } })}
103+
sx={{ mr: 2 }}
104+
>
105+
{["formal", "neutral", "casual"].map((option) => (
106+
<MenuItem key={option} value={option}>
107+
{option.charAt(0).toUpperCase() + option.slice(1).toLowerCase()}
108+
</MenuItem>
109+
))}
110+
</Select>
111+
<Select
112+
value={settings["write"]?.length || ""}
113+
onChange={(e) => setSettings({ ...settings, "write": { ...settings["write"], length: e.target.value } })}
114+
sx={{ mr: 2 }}
115+
>
116+
{["short", "medium", "long"].map((option) => (
117+
<MenuItem key={option} value={option}>
118+
{option.charAt(0).toUpperCase() + option.slice(1).toLowerCase()}
119+
</MenuItem>
120+
))}
121+
</Select>
122+
<Box sx={{ ml: 'auto', display: 'flex', alignItems: 'center' }}>
123+
<Tooltip title="Enhance Query">
124+
<IconButton onClick={handleEnhanceQuery}>
125+
<SearchIcon />
126+
</IconButton>
127+
</Tooltip>
128+
<Tooltip title="Compose">
129+
<IconButton onClick={handleSubmit}>
130+
<SendIcon />
131+
</IconButton>
132+
</Tooltip>
103133
</Box>
104134
</Box>
105135
</Box>
106136
);
107137
};
108138

109-
export default QuestionBox;
139+
export default QuestionBox;

0 commit comments

Comments
 (0)