-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.tsx
547 lines (503 loc) · 18.2 KB
/
options.tsx
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
This is a React based app which is the Options page
for our Chrome Extension. It allows the user to set
various default configuration options including
the Tana supertag to use, the Tana API key, etc.
TODO: replace all of this custom form handling with
the formik library.
*/
import React, { useEffect, useRef, useState, useCallback } from "react";
import { FormControlLabel, FormGroup, IconButton, Switch, TextField, Card, Typography, Box, Button, Tabs, Tab, Select, MenuItem } from "@mui/material";
import { get_default_configuration, merge_config } from "./Configuration";
import DeleteIcon from '@mui/icons-material/Delete';
import AddIcon from '@mui/icons-material/Add';
import { endpointUrl } from "~background";
const ConfigurationPanel = ({ closeHandler }) => {
const [savedState, setSavedState] = useState("Initial");
const [shouldLoadConfig, setShouldLoadConfig] = useState(true);
const [configuration, setConfiguration] = useState(get_default_configuration());
const divRef = useRef();
const saveConfiguration = (section: string, property: string, newValue: any) => {
let newconfig = configuration;
newconfig.config[section][property] = newValue;
setSavedState("saving");
chrome.storage.sync.set({ configuration }).then(() => {
// update local react state
setConfiguration(newconfig);
setSavedState("saved");
});
}
const handleToggle = (section: string, property: string,) => {
let currentValue: boolean = configuration.config[section][property];
saveConfiguration(section, property, !currentValue);
}
// TODO: replace this with plasmo storage API wrapper
useEffect(() => {
chrome.storage.sync.get("configuration").then((data) => {
let new_config = configuration;
console.log("Initial config", new_config);
if (data?.configuration) {
console.log("Options retrieved configuration", data.configuration);
new_config = merge_config(get_default_configuration(), data.configuration);
}
console.log("Using configuration", new_config);
setConfiguration(new_config);
setShouldLoadConfig(false);
});
}, [shouldLoadConfig]);
function resetToDefaults() {
setSavedState("saving");
let configuration = get_default_configuration();
chrome.storage.sync.set({ configuration }).then(() => {
console.log("Reset default configuration", configuration);
setConfiguration(get_default_configuration());
setSavedState("saved");
});
}
// scroll to the top, since we seem to start at the bottom...?
useEffect(() => {
const {current} = divRef;
if (current) {
current.scrollIntoView({behavior: "smooth"})
}
}, [shouldLoadConfig]);
// super simple React UI at this point
let count = 0;
const handleUpdateSuperTag = (index: number, updatedTag: any) => {
console.log("Handling update for tag at index:", index);
console.log("Updated tag data:", updatedTag);
let newconfig = {...configuration};
newconfig.config.inbox.superTags[index] = updatedTag;
console.log("New configuration:", newconfig);
setSavedState("saving");
chrome.storage.sync.set({ configuration: newconfig }).then(() => {
setConfiguration(newconfig);
setSavedState("saved");
});
};
const handleAddSuperTag = async (setSelectedTab: (index: number) => void) => {
const newTag = {
id: '',
title: '',
fields: []
};
let newconfig = {...configuration};
newconfig.config.inbox.superTags.push(newTag);
setSavedState("saving");
chrome.storage.sync.set({ configuration: newconfig }).then(() => {
setConfiguration(newconfig);
setSavedState("saved");
setSelectedTab(newconfig.config.inbox.superTags.length - 1);
});
};
const handleDeleteSuperTag = (index: number) => {
let newconfig = {...configuration};
newconfig.config.inbox.superTags.splice(index, 1);
setSavedState("saving");
chrome.storage.sync.set({ configuration: newconfig }).then(() => {
setConfiguration(newconfig);
setSavedState("saved");
});
};
if (shouldLoadConfig) {
return <div>Loading...</div>
}
else {
return (
<div style={{
width: 600,
height:'100%',
}} ref={divRef}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<h2>Configuration for clip2tana</h2>
<div style={{ height: 20, width: 200, display: 'flex', justifyContent: 'space-between' }}>
<button onClick={resetToDefaults}>Defaults</button>
<button onClick={closeHandler}>Done</button>
</div>
</div>
<FormGroup>
{configuration.schema.map((schema_elem, i) => {
count++;
let config = configuration.config[schema_elem.key];
return (
<div key={i}>
<h2>{schema_elem.label}</h2>
{schema_elem.key === 'inbox' ? (
<>
{schema_elem.properties
.filter(prop => ['pushinbox', 'tanaapikey'].includes(prop.key))
.map((property_elem, j) => {
if (property_elem.type === "boolean") {
return (
<div key={j}>
<FormControlLabel
style={{ width: '100%' }}
control={
<Switch
checked={config[property_elem.key] === true}
onChange={e => handleToggle(schema_elem.key, property_elem.key)}
/>
}
label={property_elem.label}
disabled={property_elem.disabled}
/>
<div style={{ height: '12px' }} />
</div>
);
} else if (property_elem.type === "string") {
return (
<div key={j}>
<TextField
style={{ width: '100%' }}
value={config[property_elem.key]}
onChange={e => saveConfiguration(schema_elem.key, property_elem.key, e.target.value)}
variant="outlined"
label={property_elem.label}
disabled={property_elem.disabled}
/>
<div style={{ height: '12px' }} />
</div>
);
}
})}
<Typography variant="h6" sx={{ mt: 2, mb: 2 }}>Super Tags</Typography>
{config.superTags?.length > 0 ? (
<SuperTagCard
superTags={config.superTags}
onUpdate={handleUpdateSuperTag}
onDelete={handleDeleteSuperTag}
onAdd={handleAddSuperTag}
configuration={configuration}
/>
) : (
<Button
variant="contained"
startIcon={<AddIcon />}
onClick={handleAddSuperTag}
size="small"
>
Add First Super Tag
</Button>
)}
</>
) : (
schema_elem.properties.map((property_elem, j) => {
if (property_elem.type == "string") {
return (
<div key={j}>
<TextField style={{ width: '100%' }}
autoFocus={count != 1}
value={config[property_elem.key]}
onChange={e => saveConfiguration(schema_elem.key, property_elem.key, e.target.value)}
variant="outlined"
label={property_elem.label}
disabled={property_elem.disabled}
/>
<div style={{ height: '12px' }} />
</div>
)
}
else if (property_elem.type == "boolean") {
return (
<div key={j}>
<FormControlLabel style={{ width: '100%' }}
control={
<Switch
checked={config[property_elem.key] == true}
onChange={e => handleToggle(schema_elem.key, property_elem.key)}
/>}
label={property_elem.label}
disabled={property_elem.disabled}
/>
<div style={{ height: '12px' }} />
</div>
)
}
})
)}
</div>
)
})}
</FormGroup>
</div>
);
}
}
const SuperTagCard = ({ superTags, onUpdate, onDelete, onAdd, configuration }) => {
const [selectedTab, setSelectedTab] = useState(0);
const [isCreating, setIsCreating] = useState(false);
const [createStatus, setCreateStatus] = useState<'idle' | 'creating' | 'success' | 'error'>('idle');
const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
setSelectedTab(newValue);
};
// 当前选中的 superTag
const currentTag = superTags[selectedTab];
const handleCreateSuperTag = async (index: number, title: string, currentTag: any) => {
if (!title.trim() || !configuration.config.inbox.tanaapikey || isCreating) {
return;
}
setIsCreating(true);
setCreateStatus('creating');
try {
const payload = {
targetNodeId: 'SCHEMA',
nodes: [{
name: title.trim(),
supertags: [{ id: 'SYS_T01' }]
}]
};
const response = await fetch(endpointUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: 'Bearer ' + configuration.config.inbox.tanaapikey,
},
body: JSON.stringify(payload),
});
if (!response.ok) {
const errorBody = await response.text();
if (!errorBody.includes("already exists")) {
console.error("Failed to create supertag:", errorBody);
setCreateStatus('error');
return;
}
}
// 获取响应数据
const responseData = await response.json();
console.log("Response data:", responseData);
// 从响应中获取 children 数组的第一个元素
const createdNode = responseData.children[0];
console.log("Created node:", createdNode);
if (createdNode && createdNode.nodeId) {
console.log("Creating super tag with nodeId:", createdNode.nodeId);
console.log("Current tag before update:", currentTag);
// 更新 currentTag,包含新的 nodeId 作为 id
const updatedTag = {
...currentTag,
title: title.trim(),
id: createdNode.nodeId
};
console.log("Updated tag:", updatedTag);
onUpdate(index, updatedTag);
setCreateStatus('success');
} else {
console.error("No nodeId found in response");
setCreateStatus('error');
}
setTimeout(() => {
setCreateStatus('idle');
}, 3000);
} catch (error) {
console.error("Error creating supertag:", error);
setCreateStatus('error');
} finally {
setIsCreating(false);
}
};
const getHelperText = () => {
switch (createStatus) {
case 'creating':
return "Creating super tag...";
case 'success':
return "Super tag created successfully!";
case 'error':
return "Failed to create super tag";
default:
return "";
}
};
const getHelperTextColor = () => {
switch (createStatus) {
case 'creating':
return 'text.secondary';
case 'success':
return 'success.main';
case 'error':
return 'error.main';
default:
return 'text.secondary';
}
};
return (
<Card sx={{ mb: 2 }}>
<Box sx={{ borderColor: 'divider' }}>
<Box sx={{
display: 'flex',
alignItems: 'center',
gap: 2,
width: '100%',
padding: '16px',
position: 'relative'
}}>
<Select
value={selectedTab}
onChange={(e) => setSelectedTab(Number(e.target.value))}
size="small"
sx={{
minWidth: 200,
width: 300,
'& .MuiSelect-select': {
width: '100%',
paddingRight: '32px'
}
}}
MenuProps={{
PaperProps: {
sx: {
width: 300,
maxHeight: 300
}
},
anchorOrigin: {
vertical: 'bottom',
horizontal: 'left'
},
transformOrigin: {
vertical: 'top',
horizontal: 'left'
},
disablePortal: true,
slotProps: {
paper: {
sx: {
position: 'absolute',
zIndex: 1300
}
}
}
}}
>
{superTags.map((tag, index) => (
<MenuItem
key={index}
value={index}
sx={{
width: '100%', // 确保菜单项宽度一致
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis'
}}
>
{tag.title || `Tag ${index + 1}`}
</MenuItem>
))}
</Select>
<IconButton
onClick={() => onAdd(setSelectedTab)}
size="small"
color="primary"
sx={{ flexShrink: 0 }} // 防止按钮被压缩
>
<AddIcon />
</IconButton>
</Box>
</Box>
{currentTag && (
<Box sx={{ p: 2 }}>
<Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 2 }}>
<Typography variant="h6">
{currentTag.title || `Super Tag ${selectedTab + 1}`}
</Typography>
<IconButton
color="error"
onClick={() => onDelete(selectedTab)}
size="small"
>
<DeleteIcon />
</IconButton>
</Box>
<TextField
label="Super Tag Title"
value={currentTag.title}
onChange={(e) => {
onUpdate(selectedTab, { ...currentTag, title: e.target.value });
}}
onKeyDown={(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
console.log("Enter key pressed");
e.preventDefault();
handleCreateSuperTag(selectedTab, currentTag.title, currentTag);
}
}}
fullWidth
size="small"
sx={{ mb: 2 }}
disabled={isCreating}
helperText={getHelperText() || "Press Enter to create"}
FormHelperTextProps={{
sx: { color: getHelperTextColor() }
}}
/>
<TextField
label="Super Tag ID"
value={currentTag.id}
onChange={(e) => onUpdate(selectedTab, { ...currentTag, id: e.target.value })}
fullWidth
size="small"
sx={{ mb: 2 }}
/>
<Typography variant="subtitle2" sx={{ mb: 1 }}>Fields</Typography>
{currentTag.fields.map((field, index) => (
<Box key={index} sx={{ display: 'flex', gap: 1, mb: 1 }}>
<TextField
label="Field ID"
value={field.id}
onChange={(e) => {
const newFields = [...currentTag.fields];
newFields[index] = { id: e.target.value };
onUpdate(selectedTab, { ...currentTag, fields: newFields });
}}
fullWidth
size="small"
/>
<IconButton
onClick={() => {
const newFields = currentTag.fields.filter((_, i) => i !== index);
onUpdate(selectedTab, { ...currentTag, fields: newFields });
}}
size="small"
>
<DeleteIcon />
</IconButton>
</Box>
))}
<Button
startIcon={<AddIcon />}
onClick={() => onUpdate(selectedTab, {
...currentTag,
fields: [...currentTag.fields, { id: '' }]
})}
size="small"
sx={{ mt: 1 }}
>
Add Field
</Button>
<Typography
variant="caption"
color="textSecondary"
sx={{
display: 'block',
mt: 2,
mb: 1,
'& ol': {
margin: '8px 0 0 0',
paddingLeft: '20px'
},
'& li': {
marginBottom: '4px'
}
}}
>
Note: For new Super Tags, you need to get the ID from Tana client:
<ol>
<li>Open the supertag configuration panel in Tana</li>
<li>Run "Show API Schema" command on the supertag title</li>
<li>Copy the ID and paste it into the "Super Tag ID" field above</li>
</ol>
</Typography>
</Box>
)}
</Card>
);
};
export default ConfigurationPanel;