summaryrefslogtreecommitdiff
path: root/pages/filesystemtest.tsx
blob: 4cadf0e0601660d778cbe18f06d54b384ff99826 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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>
  )
}