import brain from "data-base64:~assets/brain.png"; import icon from "data-base64:~assets/icon.png"; import { sendToBackground } from "@plasmohq/messaging"; import { Storage } from "@plasmohq/storage"; import { CrossCircledIcon, DiscIcon, ExitIcon, FileIcon, ReloadIcon, UploadIcon, } from "@radix-ui/react-icons"; import { convertHtmlToMarkdown } from "dom-to-semantic-markdown"; import { Check, ChevronsUpDown } from "lucide-react"; import React, { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { cn } from "~/lib/utils"; import { Button } from "~/routes/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "~/routes/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "~/routes/ui/popover"; import { Label } from "~routes/ui/label"; import { useToast } from "~routes/ui/use-toast"; import { getRenderedHtml } from "~utils/commons"; import type { WebHistory } from "~utils/interfaces"; import Loading from "./Loading"; const HomePage = () => { const { toast } = useToast(); const navigation = useNavigate(); const [noOfWebPages, setNoOfWebPages] = useState(0); const [loading, setLoading] = useState(true); const [open, setOpen] = React.useState(false); const [value, setValue] = React.useState(""); const [searchspaces, setSearchSpaces] = useState([]); const [isSaving, setIsSaving] = useState(false); useEffect(() => { const checkSearchSpaces = async () => { const storage = new Storage({ area: "local" }); const token = await storage.get("token"); try { const response = await fetch( `${process.env.PLASMO_PUBLIC_BACKEND_URL}/api/v1/searchspaces/`, { headers: { Authorization: `Bearer ${token}`, }, } ); if (!response.ok) { throw new Error("Token verification failed"); } else { const res = await response.json(); console.log(res); setSearchSpaces(res); } } catch (error) { await storage.remove("token"); await storage.remove("showShadowDom"); navigation("/login"); } }; checkSearchSpaces(); setLoading(false); }, []); useEffect(() => { async function onLoad() { try { chrome.storage.onChanged.addListener((changes: any, areaName: string) => { if (changes.webhistory) { const webhistory = JSON.parse(changes.webhistory.newValue); console.log("webhistory", webhistory); let sum = 0; webhistory.webhistory.forEach((element: any) => { sum = sum + element.tabHistory.length; }); setNoOfWebPages(sum); } }); const storage = new Storage({ area: "local" }); const searchspace = await storage.get("search_space"); if (searchspace) { setValue(searchspace); } await storage.set("showShadowDom", true); const webhistoryObj: any = await storage.get("webhistory"); if (webhistoryObj.webhistory.length) { const webhistory = webhistoryObj.webhistory; if (webhistoryObj) { let sum = 0; webhistory.forEach((element: any) => { sum = sum + element.tabHistory.length; }); setNoOfWebPages(sum); } } else { setNoOfWebPages(0); } } catch (error) { console.log(error); } } onLoad(); }, []); async function clearMem(): Promise { try { const storage = new Storage({ area: "local" }); const webHistory: any = await storage.get("webhistory"); const urlQueue: any = await storage.get("urlQueueList"); const timeQueue: any = await storage.get("timeQueueList"); if (!webHistory.webhistory) { return; } //Main Cleanup COde chrome.tabs.query({}, async (tabs) => { //Get Active Tabs Ids let actives = tabs.map((tab) => { if (tab.id) { return tab.id; } }); actives = actives.filter((item: any) => item); //Only retain which is still active const newHistory = webHistory.webhistory.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element; } }); const newUrlQueue = urlQueue.urlQueueList.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element; } }); const newTimeQueue = timeQueue.timeQueueList.map((element: any) => { //@ts-ignore if (actives.includes(element.tabsessionId)) { return element; } }); await storage.set("webhistory", { webhistory: newHistory.filter((item: any) => item) }); await storage.set("urlQueueList", { urlQueueList: newUrlQueue.filter((item: any) => item), }); await storage.set("timeQueueList", { timeQueueList: newTimeQueue.filter((item: any) => item), }); toast({ title: "History store cleared", description: "Inactive history sessions have been removed", variant: "destructive", }); }); } catch (error) { console.log(error); } } async function saveCurrSnapShot(): Promise { chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { const storage = new Storage({ area: "local" }); const tab = tabs[0]; if (tab.id) { const tabId: number = tab.id; const result = await chrome.scripting.executeScript({ // @ts-ignore target: { tabId: tab.id }, // @ts-ignore func: getRenderedHtml, }); const toPushInTabHistory: any = result[0].result; //Updates 'tabhistory' const webhistoryObj: any = await storage.get("webhistory"); const webHistoryOfTabId = webhistoryObj.webhistory.filter((data: WebHistory) => { return data.tabsessionId === tab.id; }); toPushInTabHistory.pageContentMarkdown = convertHtmlToMarkdown( toPushInTabHistory.renderedHtml, { extractMainContent: true, includeMetaData: false, enableTableColumnTracking: true, } ); delete toPushInTabHistory.renderedHtml; const tabhistory = webHistoryOfTabId[0].tabHistory; const urlQueueListObj: any = await storage.get("urlQueueList"); const timeQueueListObj: any = await storage.get("timeQueueList"); const isUrlQueueThere = urlQueueListObj.urlQueueList.find( (data: WebHistory) => data.tabsessionId === tabId ); const isTimeQueueThere = timeQueueListObj.timeQueueList.find( (data: WebHistory) => data.tabsessionId === tabId ); toPushInTabHistory.duration = toPushInTabHistory.entryTime - isTimeQueueThere.timeQueue[isTimeQueueThere.timeQueue.length - 1]; if (isUrlQueueThere.urlQueue.length === 1) { toPushInTabHistory.reffererUrl = "START"; } if (isUrlQueueThere.urlQueue.length > 1) { toPushInTabHistory.reffererUrl = isUrlQueueThere.urlQueue[isUrlQueueThere.urlQueue.length - 2]; } webHistoryOfTabId[0].tabHistory.push(toPushInTabHistory); await storage.set("webhistory", webhistoryObj); toast({ title: "Snapshot saved", description: `Captured: ${toPushInTabHistory.title}`, }); } }); } const saveDatamessage = async () => { if (value === "") { toast({ title: "Select a SearchSpace !", }); return; } const storage = new Storage({ area: "local" }); const search_space_id = await storage.get("search_space_id"); if (!search_space_id) { toast({ title: "Invalid SearchSpace selected!", variant: "destructive", }); return; } setIsSaving(true); toast({ title: "Save job running", description: "Saving captured content to SurfSense", }); try { const resp = await sendToBackground({ // @ts-ignore name: "savedata", }); toast({ title: resp.message, }); } catch (error) { toast({ title: "Error saving data", description: "Please try again", variant: "destructive", }); } finally { setIsSaving(false); } }; async function logOut(): Promise { const storage = new Storage({ area: "local" }); await storage.remove("token"); await storage.remove("showShadowDom"); navigation("/login"); } if (loading) { return ; } else { return searchspaces.length === 0 ? (
SurfSense

SurfSense

Please create a Search Space to continue

) : (
SurfSense

SurfSense

brain {noOfWebPages}

Captured web pages

No search spaces found. {searchspaces.map((space) => ( { const storage = new Storage({ area: "local" }); if (currentValue === value) { await storage.set("search_space", ""); await storage.set("search_space_id", 0); } else { const selectedSpace = searchspaces.find( (space) => space.name === currentValue ); await storage.set("search_space", currentValue); await storage.set("search_space_id", selectedSpace.id); } setValue(currentValue === value ? "" : currentValue); setOpen(false); }} className="aria-selected:bg-gray-700" >
{space.name}
))}
); } }; export default HomePage;