diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-13 21:43:44 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-03-14 16:40:45 +0000 |
| commit | 04572a8e5081b1e4871e273cde9dbaaa44c52fe0 (patch) | |
| tree | 8e993acb732a50d1306d4d6953df96c165c57f57 /apps/browser-extension/src/SavePage.tsx | |
| parent | 2df08ed08c065e8b91bc8df0266bd4bcbb062be4 (diff) | |
| download | karakeep-04572a8e5081b1e4871e273cde9dbaaa44c52fe0.tar.zst | |
structure: Create apps dir and copy tooling dir from t3-turbo repo
Diffstat (limited to 'apps/browser-extension/src/SavePage.tsx')
| -rw-r--r-- | apps/browser-extension/src/SavePage.tsx | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/apps/browser-extension/src/SavePage.tsx b/apps/browser-extension/src/SavePage.tsx new file mode 100644 index 00000000..638af149 --- /dev/null +++ b/apps/browser-extension/src/SavePage.tsx @@ -0,0 +1,60 @@ +import { useEffect, useState } from "react"; +import Spinner from "./Spinner"; +import { api } from "./utils/trpc"; +import { Navigate } from "react-router-dom"; + +export default function SavePage() { + const [error, setError] = useState<string | undefined>(undefined); + + const { + data, + mutate: createBookmark, + status, + } = api.bookmarks.createBookmark.useMutation({ + onError: (e) => { + setError("Something went wrong: " + e.message); + }, + }); + + useEffect(() => { + async function runSave() { + 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"); + return; + } + + createBookmark({ + type: "link", + url: currentUrl, + }); + } + runSave(); + }, [createBookmark]); + + switch (status) { + case "error": { + return <div className="text-red-500">{error}</div>; + } + case "success": { + return <Navigate to={`/bookmark/${data.id}`} />; + } + case "pending": { + return ( + <div className="flex justify-between text-lg"> + <span>Saving Bookmark </span> + <Spinner /> + </div> + ); + } + case "idle": { + return <div />; + } + } +} |
