aboutsummaryrefslogtreecommitdiffstats
path: root/apps/browser-extension/src/SavePage.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'apps/browser-extension/src/SavePage.tsx')
-rw-r--r--apps/browser-extension/src/SavePage.tsx44
1 files changed, 34 insertions, 10 deletions
diff --git a/apps/browser-extension/src/SavePage.tsx b/apps/browser-extension/src/SavePage.tsx
index d3d9458a..b4b9ce95 100644
--- a/apps/browser-extension/src/SavePage.tsx
+++ b/apps/browser-extension/src/SavePage.tsx
@@ -10,6 +10,8 @@ import {
import { NEW_BOOKMARK_REQUEST_KEY_NAME } from "./background/protocol";
import Spinner from "./Spinner";
import { api } from "./utils/trpc";
+import { MessageType } from "./utils/type";
+import { isHttpUrl } from "./utils/url";
export default function SavePage() {
const [error, setError] = useState<string | undefined>(undefined);
@@ -22,8 +24,18 @@ export default function SavePage() {
onError: (e) => {
setError("Something went wrong: " + e.message);
},
+ onSuccess: async () => {
+ // After successful creation, update badge cache and notify background
+ const [currentTab] = await chrome.tabs.query({
+ active: true,
+ lastFocusedWindow: true,
+ });
+ await chrome.runtime.sendMessage({
+ type: MessageType.BOOKMARK_REFRESH_BADGE,
+ currentTab: currentTab,
+ });
+ },
});
-
useEffect(() => {
async function getNewBookmarkRequestFromBackgroundScriptIfAny(): Promise<ZNewBookmarkRequest | null> {
const { [NEW_BOOKMARK_REQUEST_KEY_NAME]: req } =
@@ -44,17 +56,24 @@ export default function SavePage() {
active: true,
lastFocusedWindow: true,
});
- if (currentTab?.url) {
- newBookmarkRequest = {
- type: BookmarkTypes.LINK,
- url: currentTab.url,
- title: currentTab.title,
- source: "extension",
- };
- } else {
- setError("Couldn't find the URL of the current tab");
+ if (!currentTab.url) {
+ setError("Current tab has no URL to bookmark.");
return;
}
+
+ if (!isHttpUrl(currentTab.url)) {
+ setError(
+ "Cannot bookmark this type of URL. Only HTTP/HTTPS URLs are supported.",
+ );
+ return;
+ }
+
+ newBookmarkRequest = {
+ type: BookmarkTypes.LINK,
+ title: currentTab.title,
+ url: currentTab.url,
+ source: "extension",
+ };
}
createBookmark({
@@ -62,9 +81,14 @@ export default function SavePage() {
source: newBookmarkRequest.source || "extension",
});
}
+
runSave();
}, [createBookmark]);
+ if (error) {
+ return <div className="text-red-500">{error}</div>;
+ }
+
switch (status) {
case "error": {
return <div className="text-red-500">{error}</div>;