-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.tsx
194 lines (171 loc) · 6.25 KB
/
popup.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
import React, { useEffect, useState } from "react";
import { FormControlLabel, FormGroup, IconButton, Switch, TextareaAutosize, TextField } from "@mui/material";
import { get_default_configuration, merge_config } from "./Configuration";
import ConfigurationPanel from "~options";
/*
helper functions related to our use of content.js to do various operations
and the required messaging back and forth to the React app contained within
content.js
*/
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
// send a message to the background.js worker
async function askServiceWorker(message) {
console.log("Popup sending message ", message);
const response = await chrome.runtime.sendMessage(message);
console.log("Popup message got response: ", response);
return response;
}
const endpointUrl = "https://europe-west1-tagr-prod.cloudfunctions.net/addToNodeV2";
async function pushDataToEndpoint(payload: any, token:string) {
console.log("Pushing data to endpoint", payload);
try {
console.log("Pushing data to endpoint", JSON.stringify(payload));
const response = await fetch(endpointUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: 'Bearer ' + token,
},
body: JSON.stringify(payload),
});
if (response.ok) {
console.log("Data pushed successfully!");
} else {
const errorBody = await response.text();
console.error("Failed to push data from popup:", errorBody);
}
} catch (error) {
console.error("An error occurred while pushing data:", error);
}
}
function ClipPopup() {
const [shouldLoadConfig, setShouldLoadConfig] = useState(true);
const [configuration, setConfiguration] = useState(get_default_configuration());
const [data, setData] = useState("");
const [nodes, setNodes] = useState({});
const [fetchClipping, setFetchClipping] = useState(false);
const [showSettings, setShowSettings] = useState(false);
const [disableSendtoTana, setDisableSendtoTana] = useState(false);
// install event handler on load
useEffect(() => {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Popup got broadcast: ", message);
if (message === "selection-changed") {
setShouldLoadConfig(true);
}
});
}, []);
// TODO: replace this with plasmo storage API wrapper
useEffect(() => {
if (shouldLoadConfig) {
chrome.storage.sync.get("configuration").then((data) => {
if (data?.configuration) {
let new_config = merge_config(configuration, data.configuration);
console.log("popup loaded configuration", JSON.stringify(new_config));
setConfiguration(new_config);
setDisableSendtoTana(!new_config.config.inbox.pushinbox)
}
setShouldLoadConfig(false); // only try once
setFetchClipping(true); // now fetch the clipping
});
}
}, [shouldLoadConfig]);
useEffect(() => {
setDisableSendtoTana(!configuration.config.inbox.pushinbox);
}, [configuration]);
// TODO: replace this with plasmo message API wrapper
useEffect(() => {
if (fetchClipping) {
console.log("Popup sending get-tanapaste");
// invoke clip2tana to grab current tab selection, etc
askServiceWorker({ command: "get-tanapaste", configuration: configuration })
.then((response) => {
setData(response.selection);
setFetchClipping(false)
console.log("Popup get-tanapaste complete");
});
console.log("Popup sending get-tananodes with params", JSON.stringify(configuration));
// also fetch the nodes version of this for inbox posting purposes
askServiceWorker({ command: "get-tananodes", configuration: configuration })
.then((response) => {
setNodes(response.nodes);
console.log("Popup get-tananodes complete");
});
}
}, [fetchClipping]);
function openOptionsPage() {
setShowSettings(true);
}
function closeOptionsPage() {
setShowSettings(false);
// refetch our config, which will fetch clipping again
setShouldLoadConfig(true);
}
function openEmbeddedOptions() {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
}
function copyToClipboard() {
//navigator.clipboard.writeText(data).then(
// Make this use the background service worker
// since it's not succeeding from the foreground popup context
askServiceWorker({ command: "set-clipboard", clipboard: data }).then(
function () {
console.error("Successfully copied data to clipboard");
},
function (err) {
console.error("Error copying data to clipboard: ", err);
}
);
close();
}
function sendToTana() {
// send to Tana Input API
// or Tana Helper queue?
pushDataToEndpoint(nodes, configuration.config.inbox.tanaapikey)
.then(() => {
close();
});
}
function handleTextEdit(event) {
setData(event.target.value);
// if we edit the text, we can't push to inbox anymore
// TODO: reparse edited text into Tana Input API structure
setDisableSendtoTana(true);
}
if (showSettings) {
return (
<div style={{ width: '100%', height: '100%' }}>
<ConfigurationPanel closeHandler={() => closeOptionsPage()} />
</div>
);
}
else {
return (
<div style={{ width: 600, height: '100%' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<h2>Clip2Tana</h2>
<div style={{ height: 20, width: 400, display: 'flex', justifyContent: 'space-between' }}>
<button onClick={openOptionsPage}>Settings</button>
<button onClick={sendToTana} disabled={disableSendtoTana}>Send to Tana</button>
<button onClick={copyToClipboard}>Copy</button>
</div>
</div>
<TextareaAutosize style={{ width: '100%' }}
autoFocus={true}
value={data}
onChange={handleTextEdit}
/>
</div>
);
}
}
export default ClipPopup