forked from lxieyang/chrome-extension-boilerplate-react
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Popup.jsx
255 lines (241 loc) · 7.67 KB
/
Popup.jsx
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React, { Component } from 'react';
import logo from '../../assets/img/logo.svg';
import Greetings from '../../containers/Greetings/Greetings';
import './Popup.css';
// react material ui
import { green, pink, red } from '@mui/material/colors';
import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemAvatar from '@mui/material/ListItemAvatar';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import ListSubheader from '@mui/material/ListSubheader';
import Avatar from '@mui/material/Avatar';
import IconButton from '@mui/material/IconButton';
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import Grid from '@mui/material/Grid';
import Typography from '@mui/material/Typography';
import AssignmentIcon from '@mui/icons-material/Assignment';
import DeleteIcon from '@mui/icons-material/Delete';
import GitHubIcon from '@mui/icons-material/GitHub';
import AccountTreeIcon from '@mui/icons-material/AccountTree';
import DeleteSweepIcon from '@mui/icons-material/DeleteSweep';
import { ListItemSecondaryAction } from '@mui/material';
// helper functions
import { getTitleFromUrl } from '../../helper';
class Popup extends Component {
constructor(props) {
super(props);
this.state = { data: [], daysInput: 3 };
this.removebyid = this.removebyid.bind(this);
chrome.storage.sync.get(
null,
function (items) {
if (items.hasOwnProperty('data')) {
this.setState({ data: items.data });
}
if (items.hasOwnProperty('timerDays')) {
this.setState({ daysInput: items.timerDays });
}
}.bind(this)
);
this.handleSubmit = this.handleSubmit.bind(this);
}
removebyid(id) {
chrome.storage.sync.get(
'data',
function (items) {
let redos;
if (Object.keys(items).length === 0) {
redos = [];
} else {
redos = items.data;
}
let index = 0;
for (; index < redos.length; index++) {
if (redos[index].id === id) {
redos.splice(index, 1);
break;
}
}
chrome.storage.sync.set(
{ data: redos },
function () {
chrome.storage.sync.get(
'data',
function (items) {
if (Object.keys(items).length > 0) {
this.setState({ data: items.data });
}
}.bind(this)
);
}.bind(this)
);
}.bind(this)
);
}
removeall() {
chrome.storage.sync.set({ data: [] });
this.setState({ data: [] });
}
handleSubmit(event) {
alert('Saved the number of days for timer: ' + this.state.daysInput);
chrome.storage.sync.set({ timerDays: this.state.daysInput });
event.preventDefault();
}
render() {
const itemsList = this.state.data.map((redo) => {
const redoURI = redo.uri;
const today = new Date();
var delta = (new Date(redo.reminderDate) - today) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// what's left is seconds
var seconds = delta % 60;
const avatarBgColor =
new Date(redo.reminderDate) - today <= 0 ? red[700] : green[700];
return (
<ListItem
key={redo.id}
button
component="a"
href={redo.uri}
onClick={(e) => {
chrome.tabs.update({ url: redo.uri });
e.preventDefault();
}}
>
<ListItemAvatar>
<Avatar sx={{ bgcolor: avatarBgColor }}>
<AssignmentIcon />
</Avatar>
</ListItemAvatar>
<ListItemText
primary={redo.id}
secondary={
new Date(redo.reminderDate) - today <= 0
? 'Time to re-do!'
: `${days} days ${hours} hours`
}
/>
<ListItemSecondaryAction>
<IconButton
edge="end"
aria-label="delete"
onClick={(e) => {
e.preventDefault();
this.removebyid(redo.id);
}}
>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
);
});
return (
<div className="App">
<header className="App-header">
<div className="Title-bar">
<form onSubmit={this.handleSubmit}>
<label style={{ fontSize: '10px' }}>
<input
type="text"
pattern="[0-9]*"
style={{ width: '30px' }}
onChange={(event) =>
this.setState({
daysInput: event.target.value.replace(/\D/, ''),
})
}
value={this.state.daysInput}
/>
</label>
<input
type="submit"
value="💾"
style={{ fontSize: 'smaller' }}
/>
</form>
<Typography variant="subtitle2" component="div">
LeetCode List
</Typography>
<div>
<IconButton
edge="end"
size="large"
aria-label="delete"
onClick={(e) => {
e.preventDefault();
this.removeall();
}}
>
<DeleteSweepIcon style={{ fontSize: 'large' }} />
</IconButton>
</div>
</div>
<List dense={false} style={{ maxHeight: '100%', overflow: 'auto' }}>
{itemsList}
</List>
</header>
<div class="footer">
<div>
<IconButton
edge="end"
size="small"
aria-label="delete"
onClick={(e) => {
chrome.tabs.create({
url: 'https://github.com/architec/mistake',
});
e.preventDefault();
}}
>
<GitHubIcon style={{ fontSize: 'xx-small' }} />
</IconButton>
</div>
<div>
<IconButton
edge="end"
size="small"
aria-label="delete"
onClick={(e) => {
chrome.tabs.create({
url: 'https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions',
});
e.preventDefault();
}}
>
<AccountTreeIcon style={{ fontSize: 'xx-small' }} />
</IconButton>
<IconButton
edge="end"
size="small"
aria-label="delete"
onClick={(e) => {
chrome.tabs.create({
url: 'https://leetcode.com/tag/dynamic-programming/discuss/1050391/Must-do-Dynamic-programming-Problems-Category-wise',
});
e.preventDefault();
}}
>
<AccountTreeIcon style={{ fontSize: 'xx-small' }} />
</IconButton>
</div>
</div>
</div>
);
}
}
export default Popup;