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
|
import { useEffect, useState } from "react";
import { Settings } from "./settings";
export default function SavePage({ settings }: { settings: Settings }) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | undefined>(undefined);
useEffect(() => {
async function runFetch() {
let currentUrl;
const [currentTab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});
if (currentTab?.url) {
currentUrl = currentTab.url;
} else {
setError("Couldn't find the URL of the current tab");
setLoading(false);
return;
}
try {
const resp = await fetch(
`${settings.address}/api/trpc/bookmarks.createBookmark`,
{
method: "POST",
headers: {
Authorization: `Bearer ${settings.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
json: {
type: "link",
url: currentUrl,
},
}),
},
);
if (!resp.ok) {
setError(
"Something went wrong: " + JSON.stringify(await resp.json()),
);
}
} catch (e) {
setError("Message: " + (e as Error).message);
}
setLoading(false);
}
runFetch();
}, [settings]);
if (loading) {
return <div>Loading ...</div>;
}
if (error) {
return <div className="text-red-500">{error} ...</div>;
}
return <div>SAVED!</div>;
}
|