summaryrefslogtreecommitdiff
path: root/bs5/server/pages/NoteList.re
blob: d284f0fc3a3e0084bb66d60ae7aed0277d0f74bb (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
open Lwt.Syntax;

let is_substring = (a, b) => {
  let len_a = String.length(a);
  let len_b = String.length(b);
  if (len_a > len_b) {
    false;
  } else {
    let rec check = start =>
      if (start > len_b - len_a) {
        false;
      } else if (String.sub(b, start, len_a) == a) {
        true;
      } else {
        check(start + 1);
      };
    check(0);
  };
};

[@react.async.component]
let make = (~searchText: string) => {
  let+ notes = DB.read_notes();

  switch (notes) {
  | Error(error) =>
    <div
      className="mt-8 h-full w-full flex flex-col items-center justify-center gap-4">
      <Text size=XXLarge> "❌" </Text>
      <Text> "Couldn't read notes file" </Text>
      <Text weight=Bold> error </Text>
    </div>
  | Ok(notes) when notes->List.length == 0 =>
    <div className="mt-8">
      <Text> "There's no notes created yet!" </Text>
    </div>
  | Ok(notes) =>
    <ul className="mt-8">
      {notes
       |> List.filter((note: Note.t) =>
            is_substring(
              String.lowercase_ascii(searchText),
              String.lowercase_ascii(note.title),
            )
          )
       |> List.map((note: Note.t) =>
            <li key={Int.to_string(note.id)}> <SidebarNote note /> </li>
          )
       |> React.list}
    </ul>
  };
};