forked from Saifullah-dev/react-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.jsx
More file actions
210 lines (200 loc) · 7.07 KB
/
Toolbar.jsx
File metadata and controls
210 lines (200 loc) · 7.07 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { useState } from "react";
import { BsCopy, BsFolderPlus, BsGridFill, BsScissors } from "react-icons/bs";
import { FiRefreshCw } from "react-icons/fi";
import {
MdClear,
MdOutlineDelete,
MdOutlineFileDownload,
MdOutlineFileUpload,
} from "react-icons/md";
import { BiRename, BiQuestionMark } from "react-icons/bi";
import { FaListUl, FaRegPaste } from "react-icons/fa6";
import LayoutToggler from "./LayoutToggler";
import { useFileNavigation } from "../../contexts/FileNavigationContext";
import { useSelection } from "../../contexts/SelectionContext";
import { useClipBoard } from "../../contexts/ClipboardContext";
import { useLayout } from "../../contexts/LayoutContext";
import { validateApiCallback } from "../../utils/validateApiCallback";
import { useTranslation } from "../../contexts/TranslationProvider";
import "./Toolbar.scss";
const Toolbar = ({ onLayoutChange, onRefresh, triggerAction, permissions }) => {
const [showToggleViewMenu, setShowToggleViewMenu] = useState(false);
const { currentFolder } = useFileNavigation();
const { selectedFiles, setSelectedFiles, handleDownload } = useSelection();
const { clipBoard, setClipBoard, handleCutCopy, handlePasting } = useClipBoard();
const { activeLayout } = useLayout();
const t = useTranslation();
// Toolbar Items
const toolbarLeftItems = [
{
icon: <BsFolderPlus size={17} strokeWidth={0.3} />,
text: t("newFolder"),
permission: permissions.create,
onClick: () => triggerAction.show("createFolder"),
},
{
icon: <MdOutlineFileUpload size={18} />,
text: t("upload"),
permission: permissions.upload,
onClick: () => triggerAction.show("uploadFile"),
},
{
icon: <FaRegPaste size={18} />,
text: t("paste"),
permission: !!clipBoard,
onClick: handleFilePasting,
},
];
const toolbarRightItems = [
{
icon: activeLayout === "grid" ? <BsGridFill size={16} /> : <FaListUl size={16} />,
title: t("changeView"),
onClick: () => setShowToggleViewMenu((prev) => !prev),
},
{
icon: <FiRefreshCw size={16} />,
title: t("refresh"),
onClick: () => {
validateApiCallback(onRefresh, "onRefresh");
setClipBoard(null);
},
},
];
function handleFilePasting() {
handlePasting(currentFolder);
}
const handleDownloadItems = () => {
handleDownload();
setSelectedFiles([]);
};
// Selected File/Folder Actions
if (selectedFiles.length > 0) {
let customButtonId = 0;
return (
<div className="toolbar file-selected">
<div className="file-action-container">
<div>
{permissions.move && (
<button className="item-action file-action" onClick={() => handleCutCopy(true)}>
<BsScissors size={18} />
<span>{t("cut")}</span>
</button>
)}
{permissions.copy && (
<button className="item-action file-action" onClick={() => handleCutCopy(false)}>
<BsCopy strokeWidth={0.1} size={17} />
<span>{t("copy")}</span>
</button>
)}
{clipBoard?.files?.length > 0 && (
<button
className="item-action file-action"
onClick={handleFilePasting}
// disabled={!clipBoard}
>
<FaRegPaste size={18} />
<span>{t("paste")}</span>
</button>
)}
{selectedFiles.length === 1 && permissions.rename && (
<button
className="item-action file-action"
onClick={() => triggerAction.show("rename")}
>
<BiRename size={19} />
<span>{t("rename")}</span>
</button>
)}
{permissions.download && (
<button className="item-action file-action" onClick={handleDownloadItems}>
<MdOutlineFileDownload size={19} />
<span>{t("download")}</span>
</button>
)}
{permissions.delete && (
<button
className="item-action file-action"
onClick={() => triggerAction.show("delete")}
>
<MdOutlineDelete size={19} />
<span>{t("delete")}</span>
</button>
)}
{selectedFiles.length > 0 && selectedFiles[0].customActions &&
selectedFiles[0].customActions.filter(x => x.inToolbarMenu)
.filter(customButton => {
let hidden = (selectedFiles.length == 1) ? false : true;
if(selectedFiles.length > 1 && customButton.enableMultiItems) {
// Display button only if all the selected items have the same button
hidden = !selectedFiles.every(x=>x.customActions && x.customActions.includes(customButton))
}
return !hidden;
})
.map(customButton => (
<button key={"customButtonId_" + (customButtonId++)}
className="item-action file-action"
onClick={() => {
if(customButton.onClick) {
customButton.onClick(selectedFiles);
} else {
console.warn("no onClick specified for this custom button");
}
}}
>
{customButton.icon ? customButton.icon : <BiQuestionMark />}
<span>{customButton.title ?? "??"}</span>
</button>
)
)}
</div>
<button
className="item-action file-action"
title={t("clearSelection")}
onClick={() => setSelectedFiles([])}
>
<span>
{selectedFiles.length}{" "}
{t(selectedFiles.length > 1 ? "itemsSelected" : "itemSelected")}
</span>
<MdClear size={18} />
</button>
</div>
</div>
);
}
//
return (
<div className="toolbar">
<div className="fm-toolbar">
<div>
{toolbarLeftItems
.filter((item) => item.permission)
.map((item, index) => (
<button className="item-action" key={index} onClick={item.onClick}>
{item.icon}
<span>{item.text}</span>
</button>
))}
</div>
<div>
{toolbarRightItems.map((item, index) => (
<div key={index} className="toolbar-left-items">
<button className="item-action icon-only" title={item.title} onClick={item.onClick}>
{item.icon}
</button>
{index !== toolbarRightItems.length - 1 && <div className="item-separator"></div>}
</div>
))}
{showToggleViewMenu && (
<LayoutToggler
setShowToggleViewMenu={setShowToggleViewMenu}
onLayoutChange={onLayoutChange}
/>
)}
</div>
</div>
</div>
);
};
Toolbar.displayName = "Toolbar";
export default Toolbar;