From f2357a84469751c8f90713627e81c0a05ee72ab4 Mon Sep 17 00:00:00 2001
From: sowgro <tpoke.ferrari@gmail.com>
Date: Sun, 3 Nov 2024 21:00:43 -0500
Subject: Root folder dropdown applies to bookmarks (somewhat broken)

---
 extension/src/Body.tsx | 46 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 7 deletions(-)

(limited to 'extension/src/Body.tsx')

diff --git a/extension/src/Body.tsx b/extension/src/Body.tsx
index df1c325..b2f8495 100644
--- a/extension/src/Body.tsx
+++ b/extension/src/Body.tsx
@@ -4,19 +4,51 @@ import Folder from "./Folder.tsx";
 import imageUrl from "./assets/settings.svg"
 import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode;
 
-function Body(props: {tree: BookmarkTreeNode[]}) {
+// @ts-ignore
+if (typeof browser === "undefined") {
+    var browser = chrome;
+}
+
+export const RootFolder = React.createContext({
+    rootFolder: '0',
+    setRootFolder: (arg0: string) => {}
+});
+
+export const BookmarkTree = React.createContext({
+    bookmarkTree: [] as BookmarkTreeNode[],
+    setBookmarkTree: (arg0: BookmarkTreeNode[]) => {}
+})
+
+function Body() {
     const [settingsOpen, setSettingsOpen] = useState(false);
+    const [rootFolder, setRootFolder] = useState('1')
+    const [bookmarkTree, setBookmarkTree] = useState<BookmarkTreeNode[]>([])
+    const [ogBookmarkTree, setOgBookmarkTree] = useState<BookmarkTreeNode[] | null>([])
+
+    browser.bookmarks.getSubTree(rootFolder).then(t => {
+        console.log(t);
+        if (t != bookmarkTree) {
+            setBookmarkTree(t);
+        }
+        console.log(bookmarkTree == t)
+    });
+
+    if (ogBookmarkTree?.length == 0) {
+        browser.bookmarks.getTree().then(t => {
+            setOgBookmarkTree(t);
+        })
+    }
 
     return (
-        <>
+        <BookmarkTree.Provider value={{bookmarkTree, setBookmarkTree}}>
+        <RootFolder.Provider value={{rootFolder, setRootFolder}}>
             <button id="settings-button" onClick={_ => setSettingsOpen(!settingsOpen)}>
                 <img alt="open settings" src={imageUrl}/>
             </button>
-            {settingsOpen && (<SettingsEditor tree={props.tree} closer={setSettingsOpen}/>)}
-            <Folder data={
-                (props.tree && props.tree[0] && props.tree[0].children && props.tree[0].children[0])!
-            }/>
-        </>
+            {settingsOpen && (<SettingsEditor tree={ogBookmarkTree!} closer={setSettingsOpen}/>)}
+            {bookmarkTree[0] && (<Folder data={bookmarkTree[0]}/>)}
+        </RootFolder.Provider>
+        </BookmarkTree.Provider>
     )
 }
 
-- 
cgit v1.2.3

