-
Notifications
You must be signed in to change notification settings - Fork 482
/
index.tsx
169 lines (149 loc) · 5.1 KB
/
index.tsx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React, { useState, useEffect } from "react";
import clsx from "clsx";
import _isNil from "lodash/isNil";
import _isEmpty from "lodash/isEmpty";
import queryString from "query-string";
import { Drawer, Fab } from "@material-ui/core";
import ChevronIcon from "@material-ui/icons/KeyboardArrowLeft";
import ChevronUpIcon from "@material-ui/icons/KeyboardArrowUp";
import ChevronDownIcon from "@material-ui/icons/KeyboardArrowDown";
import Form from "./Form";
import ErrorBoundary from "components/ErrorBoundary";
import { useStyles } from "./useStyles";
import { useFiretableContext } from "contexts/FiretableContext";
import useDoc from "hooks/useDoc";
export const DRAWER_WIDTH = 600;
export const DRAWER_COLLAPSED_WIDTH = 36;
type SelectedCell = { row: number; column: string } | null;
export type SideDrawerRef = {
cell: SelectedCell;
setCell: React.Dispatch<React.SetStateAction<SelectedCell>>;
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
};
export default function SideDrawer() {
const classes = useStyles();
const { tableState, dataGridRef, sideDrawerRef } = useFiretableContext();
const [cell, setCell] = useState<SelectedCell>(null);
const [open, setOpen] = useState(false);
if (sideDrawerRef) sideDrawerRef.current = { cell, setCell, open, setOpen };
const disabled = !open && (!cell || _isNil(cell.row));
useEffect(() => {
if (disabled && setOpen) setOpen(false);
}, [disabled]);
const handleNavigate = (direction: "up" | "down") => () => {
if (!tableState?.rows) return;
let row = cell!.row;
if (direction === "up" && row > 0) row -= 1;
if (direction === "down" && row < tableState.rows.length - 1) row += 1;
setCell!((cell) => ({ column: cell!.column, row }));
const idx = tableState?.columns[cell!.column]?.index;
dataGridRef?.current?.selectCell({ rowIdx: row, idx });
};
const [urlDocState, dispatchUrlDoc] = useDoc({});
useEffect(() => {
if (urlDocState.doc) setOpen(true);
}, [urlDocState]);
useEffect(() => {
setOpen(false);
dispatchUrlDoc({ path: "", doc: null });
}, [window.location.pathname]);
useEffect(() => {
const rowRef = queryString.parse(window.location.search).rowRef as string;
if (rowRef) dispatchUrlDoc({ path: decodeURIComponent(rowRef) });
}, []);
useEffect(() => {
if (cell && tableState?.rows[cell.row]) {
window.history.pushState(
"",
`${tableState?.tablePath}`,
`${window.location.pathname}?rowRef=${encodeURIComponent(
tableState?.rows[cell.row].ref.path
)}`
);
// console.log(tableState?.tablePath, tableState?.rows[cell.row].id);
if (urlDocState.doc) {
urlDocState.unsubscribe();
dispatchUrlDoc({ path: "", doc: null });
}
}
}, [cell]);
return (
<div className={clsx(open && classes.open, disabled && classes.disabled)}>
<Drawer
variant="permanent"
anchor="right"
className={classes.drawer}
classes={{
paperAnchorDockedRight: clsx(
classes.paper,
!disabled && classes.bumpPaper
),
paper: clsx({ [classes.paperClose]: !open }),
}}
>
<ErrorBoundary>
<div className={classes.drawerContents}>
{open &&
(urlDocState.doc || cell) &&
!_isEmpty(tableState?.columns) && (
<Form
key={urlDocState.path}
values={
urlDocState.doc ?? tableState?.rows[cell?.row ?? -1] ?? {}
}
/>
)}
</div>
</ErrorBoundary>
{open && (
<div className={classes.navFabContainer}>
<Fab
classes={{
root: clsx(classes.fab, classes.navFab),
disabled: classes.disabled,
}}
style={{ animationDelay: "0.2s" }}
color="secondary"
size="small"
disabled={disabled || !cell || cell.row <= 0}
onClick={handleNavigate("up")}
>
<ChevronUpIcon />
</Fab>
<Fab
classes={{
root: clsx(classes.fab, classes.navFab),
disabled: classes.disabled,
}}
style={{ animationDelay: "0.1s" }}
color="secondary"
size="small"
disabled={
disabled ||
!tableState ||
!cell ||
cell.row >= tableState.rows.length - 1
}
onClick={handleNavigate("down")}
>
<ChevronDownIcon />
</Fab>
</div>
)}
<div className={classes.drawerFabContainer}>
<Fab
classes={{ root: classes.fab, disabled: classes.disabled }}
color="secondary"
disabled={disabled}
onClick={() => {
if (setOpen) setOpen((o) => !o);
}}
>
<ChevronIcon className={classes.drawerFabIcon} />
</Fab>
</div>
</Drawer>
</div>
);
}