import { h, Component } from 'preact'
import { isMobile } from '../vendor/util'
// import db from '../db'
import { Link, withRouter } from 'react-router-dom'

import Header from './Header.jsx'
import Folders from './Folders/Folders.jsx'
import Folder from './Folder/Folder.jsx'

import client from '../client'

class App extends Component {
  constructor(props) {
    super()
    this.state = {
      folders: null,
      openFolders: [],
    }
    client.folder.index().then( folders => this.setState({ folders }) )
  }
  openFolder(folder) {
    if (this.state.openFolders.indexOf(folder) === -1) {
      this.setState({ openFolders: this.state.openFolders.concat([folder]) })
    }
  }
  render() {
    const openFolders = this.state.openFolders.map((folder) => {
      return (
        <Folder folder={folder} />
      )
    })
    return (
      <div>
        <Header />
        <Folders folders={this.state.folders} openFolder={(folder) => this.openFolder(folder)} />
        {openFolders}
      </div>
    )
  }
}

export default withRouter(App)
