aboutsummaryrefslogtreecommitdiff
path: root/src/main.ts
blob: 21bf2157936a973ee15093712f8aad8e8b2ea1f5 (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
import { h, withVar, mount } from 'lib/rx'
import * as Search from 'lib/search'
import * as Functions from 'lib/functions'
import * as I18n from 'lib/i18n'
import * as Filters from 'view/filters'
import * as Book from 'book'

// @ts-ignore
const sortedBookLibrary: Array<Book> = (bookLibrary as Array<Book.Book>).sort((a, b) =>
  a.authorsSort == b.authorsSort
    ? a.date > b.date
    : a.authorsSort > b.authorsSort)

mount(withVar<Filters.Model>({}, (filters, updateFilters) =>
  withVar('', (search, updateSearch) => {
    const filteredBooks = filters.flatMap(f => search.map(s =>
      sortedBookLibrary.filter(book =>
        (f.read === undefined || book.read === f.read)
        && (s === '' || matchSearch(book, s))
      )
    ))

    return [
      h('aside', Filters.view({ filteredBooks, filters, updateFilters })),
      h('main',
        h('header',
          h('input',
            { className: 'g-Search',
              oninput: Functions.debounce(
                (event: Event) => updateSearch(_ => (event.target as HTMLInputElement).value),
                500
              )
            }
          ),
          filteredBooks.map(fb => I18n.unit(fb.length, 'livre', 'livres'))
        ),
        h('div',
          { className: 'g-Books' },
          filteredBooks.map(fb =>
            fb.map(book => h('img', { className: 'g-Book', src: book.cover }))
          )
        )
      )
    ]
  })
))

function matchSearch(book: Book.Book, search: string): boolean {
  return Search.match(search, book.title, book.subtitle, ...book.authors)
}