aboutsummaryrefslogtreecommitdiffstats
path: root/ui/src/components/runbooks/List.tsx
blob: 42da38856ef5747e9813a79dc6f3ed9fc7fc3a70 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useEffect } from "react";
import {
  Button,
  ButtonGroup,
  Tooltip,
  Listbox,
  ListboxItem,
  Dropdown,
  DropdownTrigger,
  DropdownMenu,
  DropdownItem,
  Badge,
} from "@nextui-org/react";

import { EllipsisVerticalIcon } from "lucide-react";

import { DateTime } from "luxon";

import { NotebookPenIcon } from "lucide-react";
import Runbook from "@/state/runbooks/runbook";
import { AtuinState, useStore } from "@/state/store";

const NoteSidebar = () => {
  const runbooks = useStore((state: AtuinState) => state.runbooks);
  const refreshRunbooks = useStore(
    (state: AtuinState) => state.refreshRunbooks,
  );

  const currentRunbook = useStore((state: AtuinState) => state.currentRunbook);
  const setCurrentRunbook = useStore(
    (state: AtuinState) => state.setCurrentRunbook,
  );
  const runbookInfo = useStore((state: AtuinState) => state.runbookInfo);

  useEffect(() => {
    refreshRunbooks();
  }, []);

  return (
    <div className="w-48 flex flex-col border-r-1">
      <div className="overflow-y-auto flex-grow">
        <Listbox
          hideSelectedIcon
          items={runbooks.map((runbook: any): any => {
            return [runbook, runbookInfo[runbook.id]];
          })}
          variant="flat"
          aria-label="Runbook list"
          selectionMode="single"
          selectedKeys={currentRunbook ? [currentRunbook] : []}
          itemClasses={{ base: "data-[selected=true]:bg-gray-200" }}
          topContent={
            <ButtonGroup className="z-20">
              <Tooltip showArrow content="New Runbook" closeDelay={50}>
                <Button
                  isIconOnly
                  aria-label="New note"
                  variant="light"
                  size="sm"
                  onPress={async () => {
                    // otherwise the cursor is weirdly positioned in the new document
                    window.getSelection()?.removeAllRanges();

                    let runbook = await Runbook.create();
                    setCurrentRunbook(runbook.id);
                    refreshRunbooks();
                  }}
                >
                  <NotebookPenIcon className="p-[0.15rem]" />
                </Button>
              </Tooltip>
            </ButtonGroup>
          }
        >
          {([runbook, info]: [Runbook, { ptys: number }]) => (
            <ListboxItem
              key={runbook.id}
              onPress={() => {
                setCurrentRunbook(runbook.id);
              }}
              textValue={runbook.name || "Untitled"}
              endContent={
                <Dropdown>
                  <Badge
                    content={info?.ptys}
                    color="primary"
                    style={
                      info && info?.ptys > 0
                        ? {}
                        : {
                            display: "none",
                          }
                    }
                  >
                    <DropdownTrigger className="bg-transparent">
                      <Button isIconOnly>
                        <EllipsisVerticalIcon
                          size="16px"
                          className="bg-transparent"
                        />
                      </Button>
                    </DropdownTrigger>
                  </Badge>
                  <DropdownMenu aria-label="Dynamic Actions">
                    <DropdownItem
                      key={"delete"}
                      color="danger"
                      className="text-danger"
                      onPress={async () => {
                        await Runbook.delete(runbook.id);

                        if (runbook.id == currentRunbook) setCurrentRunbook("");

                        refreshRunbooks();
                      }}
                    >
                      Delete
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              }
            >
              <div className="flex flex-col">
                <div className="text-md">{runbook.name || "Untitled"}</div>
                <div className="text-xs text-gray-500">
                  <em>
                    {DateTime.fromJSDate(runbook.updated).toLocaleString(
                      DateTime.DATETIME_SHORT,
                    )}
                  </em>
                </div>
              </div>
            </ListboxItem>
          )}
        </Listbox>
      </div>
    </div>
  );
};

export default NoteSidebar;