From 2384b30a244c7d6477e54de5385fe7f1cc62d43a Mon Sep 17 00:00:00 2001
From: "Thomas F. K. Jorna" <hello@tefkah.com>
Date: Fri, 8 Oct 2021 23:39:37 +0200
Subject: feat(preview): proper file preview with api routing

---
 util/uniorg.tsx | 57 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 17 deletions(-)

(limited to 'util/uniorg.tsx')

diff --git a/util/uniorg.tsx b/util/uniorg.tsx
index e35a021..c36f662 100644
--- a/util/uniorg.tsx
+++ b/util/uniorg.tsx
@@ -1,34 +1,57 @@
-import React, { useEffect, useState } from 'react'
-import { NodeById } from '../pages/index'
+import React, { useEffect, useMemo, useState } from 'react'
+import { OrgRoamNode } from '../api'
+import { NodeByCite, NodeById } from '../pages/index'
 import { ProcessedOrg } from './processOrg'
 
 export interface UniOrgProps {
   nodeById: NodeById
   previewNode: any
   setPreviewNode: any
-  getText: any
+  nodeByCite: NodeByCite
+  setSidebarHighlightedNode: any
 }
 
 export const UniOrg = (props: UniOrgProps) => {
-  const { nodeById, previewNode, setPreviewNode, getText } = props
+  const { setSidebarHighlightedNode, nodeById, nodeByCite, previewNode, setPreviewNode } = props
 
   const [previewText, setPreviewText] = useState('')
 
+  const file = encodeURIComponent(previewNode.file)
   useEffect(() => {
-    if (previewNode?.id) {
-      getText(previewNode?.id, setPreviewText)
-    }
-  }, [previewNode?.id])
+    fetch(`api/notes/${file}`)
+      .then((res) => {
+        return res.text()
+      })
+      .then((res) => {
+        if (res !== 'error') {
+          setPreviewText(res)
+        }
+      })
+      .catch((e) => {
+        console.log(e)
+        return 'Could not fetch the text for some reason, sorry!\n\n This can happen because you have an id with forward slashes (/) in it.'
+      })
+  }, [previewNode.id])
+
+  useEffect(() => {
+    console.log('mount')
+    return () => console.log('unmount')
+  }, [])
 
   return (
-    <ProcessedOrg
-      {...{
-        getText,
-        nodeById,
-        previewNode,
-        setPreviewNode,
-        previewText,
-      }}
-    />
+    <>
+      {previewNode?.id && (
+        <ProcessedOrg
+          {...{
+            nodeById,
+            previewNode,
+            setPreviewNode,
+            previewText,
+            nodeByCite,
+            setSidebarHighlightedNode,
+          }}
+        />
+      )}
+    </>
   )
 }
-- 
cgit v1.2.3

