From 33839479e269bed905f9eefc374060b9d3ee7e19 Mon Sep 17 00:00:00 2001
From: "Thomas F. K. Jorna" <hello@tefkah.com>
Date: Tue, 5 Oct 2021 12:35:40 +0200
Subject: feat(preview): filesystem basics in place

---
 pages/filesystemtest.tsx | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 pages/filesystemtest.tsx

(limited to 'pages/filesystemtest.tsx')

diff --git a/pages/filesystemtest.tsx b/pages/filesystemtest.tsx
new file mode 100644
index 0000000..4cadf0e
--- /dev/null
+++ b/pages/filesystemtest.tsx
@@ -0,0 +1,52 @@
+import React, { useEffect, useState } from 'react'
+import { Button } from '@chakra-ui/react'
+
+async function verifyPermission(fileHandle: any, readWrite: any) {
+  const options: any = {}
+  if (readWrite) {
+    options.mode = 'readwrite'
+  }
+  // Check if permission was already granted. If so, return true.
+  if ((await fileHandle.queryPermission(options)) === 'granted') {
+    return true
+  }
+  // Request permission. If the user grants permission, return true.
+  if ((await fileHandle.requestPermission(options)) === 'granted') {
+    return true
+  }
+  // The user didn't grant permission, so return false.
+  return false
+}
+
+export default function Testpage() {
+  const [text, setText] = useState(0)
+  const [dirHandle, setDirhandle] = useState<any>()
+  const [perm, setPerm] = useState(false)
+
+  const pick = async () => {
+    const dirHandle = await window.showDirectoryPicker()
+    console.log(dirHandle)
+    setDirhandle(dirHandle)
+  }
+
+  useEffect(() => {
+    ;(async () => {
+      console.log(dirHandle)
+      const newFileHandle = dirHandle ? await dirHandle.getFileHandle('inbox.org') : null
+      const file = await newFileHandle.getFile()
+      const ttext = await file.text()
+      setText(ttext)
+      const path = newFileHandle ? await dirHandle.resolve(newFileHandle) : null
+      console.log(path)
+    })()
+  }, [dirHandle])
+
+  return (
+    <div>
+      <Button onClick={() => pick()}> Press </Button>
+      <p>{text}</p>
+      <Button onClick={() => setPerm(verifyPermission(dirHandle, true))}>Check permission</Button>
+      <p>{perm ? '👍' : '👎'}</p>
+    </div>
+  )
+}
-- 
cgit v1.2.3

