-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfile.tsx
More file actions
135 lines (126 loc) · 4.96 KB
/
file.tsx
File metadata and controls
135 lines (126 loc) · 4.96 KB
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
import { useState } from "react";
import FileNew from '@mui/icons-material/AddBox';
import FileOpen from '@mui/icons-material/OpenInNew';
import DriveFileRenameOutlineIcon from '@mui/icons-material/DriveFileRenameOutline';
import SaveIcon from '@mui/icons-material/Save';
import SaveAltIcon from '@mui/icons-material/SaveAlt';
import ImportExportIcon from '@mui/icons-material/ImportExport';
import MenuList from "@mui/material/MenuList";
import MenuItem from "@mui/material/MenuItem";
import ListItemText from "@mui/material/ListItemText";
import ListItemIcon from "@mui/material/ListItemIcon";
import Typography from "@mui/material/Typography";
import Paper from '@mui/material/Paper';
import {
openFile,
openNewFile,
saveNotebookToFileSystem,
downloadAsNewNotebook,
} from "../../lib/FileSystem/fileSystem";
import {
addNotebook,
setActiveNotebookTabNumber,
updateActiveNotebookName,
setNotebookSavingStatus,
} from "../../lib/state/reducer"
import { useDispatch, useSelector } from 'react-redux';
import { AppState } from '../../lib/typings/types';
import Export from "../Modals/Export";
export default function FileMenu() {
const [showExport, setShowExport] = useState<boolean>(false);
const dispatch = useDispatch()
const { activeNotebookTabNumber, notebooks, activeNotebookName } = useSelector((state: { app: AppState }) => state.app)
const handleOpenNewFile = async () => {
const newNotebook = await openNewFile()
dispatch(addNotebook(newNotebook))
dispatch(setActiveNotebookTabNumber(Object.keys(notebooks).length))
dispatch(updateActiveNotebookName(newNotebook?.name));
}
const handleOpenExistingFile = async () => {
const notebook = await openFile();
const tabNames = Object.keys(notebooks);
if (tabNames.includes(notebook.name)){
dispatch(setActiveNotebookTabNumber(tabNames.indexOf(notebook.name)))
dispatch(updateActiveNotebookName(notebook.name))
return;
}
dispatch(addNotebook(notebook))
dispatch(setActiveNotebookTabNumber(activeNotebookTabNumber + 1))
dispatch(updateActiveNotebookName(notebook.name))
}
const handleSaveFile = async () => {
dispatch(setNotebookSavingStatus("saving"))
const currentNotebook = notebooks[activeNotebookName]
const fileHandle = currentNotebook.metadata?.fileHandle
const contents = JSON.stringify(currentNotebook)
await saveNotebookToFileSystem(fileHandle, contents)
dispatch(setNotebookSavingStatus("saved"))
}
const downloadActiveNotebook = async () => {
dispatch(setNotebookSavingStatus("downloading"))
const currentNotebook = {...notebooks[activeNotebookName]}
await downloadAsNewNotebook(currentNotebook)
dispatch(setNotebookSavingStatus("downloaded"))
}
return (
<Paper sx={{ width: 320, maxWidth: '100%' }}>
<MenuList>
<MenuItem onClick={() => handleOpenNewFile()}>
<ListItemIcon>
<FileNew fontSize="small" />
</ListItemIcon>
<ListItemText>New</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘N
</Typography>
</MenuItem>
<MenuItem onClick={() => handleOpenExistingFile()}>
<ListItemIcon>
<FileOpen fontSize="small" />
</ListItemIcon>
<ListItemText>Open</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘O
</Typography>
</MenuItem>
{/* <MenuItem disabled={activeNotebookName === "Dashboard"}>
<ListItemIcon>
<DriveFileRenameOutlineIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Rename Notebook</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘R
</Typography>
</MenuItem> */}
<MenuItem onClick={() => handleSaveFile()} disabled={activeNotebookName === "Dashboard"}>
<ListItemIcon>
<SaveIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Save Notebook</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘S
</Typography>
</MenuItem>
<MenuItem onClick={() => downloadActiveNotebook()} disabled={activeNotebookName === "Dashboard"}>
<ListItemIcon>
<SaveAltIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Download</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘D
</Typography>
</MenuItem>
<MenuItem disabled={activeNotebookName === "Dashboard"} onClick={() => setShowExport(true)}>
<ListItemIcon>
<ImportExportIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Export Notebook as ...</ListItemText>
<Typography variant="body2" color="text.secondary">
⌘DE
</Typography>
</MenuItem>
</MenuList>
<Export open={showExport} handleClose={setShowExport} currentNote={""} />
</Paper>
);
};