forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.tsx
More file actions
72 lines (67 loc) · 1.66 KB
/
options.tsx
File metadata and controls
72 lines (67 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
const Options = function () {
const [color, setColor] = useState<string>('');
const [status, setStatus] = useState<string>('');
const [like, setLike] = useState<boolean>(false);
useEffect(() => {
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
chrome.storage.sync.get(
{
favoriteColor: 'red',
likesColor: true,
},
(items) => {
setColor(items.favoriteColor);
setLike(items.likesColor);
},
);
}, []);
const saveOptions = () => {
// Saves options to chrome.storage.sync.
chrome.storage.sync.set(
{
favoriteColor: color,
likesColor: like,
},
() => {
// Update status to let user know options were saved.
setStatus('Options saved.');
const id = setTimeout(() => {
setStatus('');
}, 1000);
return () => clearTimeout(id);
},
);
};
return (
<>
<div>
Favorite color:{' '}
<select value={color} onChange={(event) => setColor(event.target.value)}>
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
<option value="yellow">yellow</option>
</select>
</div>
<div>
{/* eslint-disable-next-line jsx-a11y/label-has-for */}
<label>
<input type="checkbox" checked={like} onChange={(event) => setLike(event.target.checked)} />I like colors.
</label>
</div>
<div>{status}</div>
<button type="button" onClick={saveOptions}>
Save
</button>
</>
);
};
ReactDOM.render(
<React.StrictMode>
<Options />
</React.StrictMode>,
document.getElementById('root'),
);