Skip to content

Commit 87b1b4d

Browse files
authored
Merge pull request #11 from devklick/dev
Add/remove file browser favorites
2 parents 61a4e37 + 67d5bce commit 87b1b4d

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/programs/FileBrowser/DirectoryOrFile/DirectoryOrFile.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function DirectoryOrFile({
4040
null
4141
);
4242
const settings = useSystemSettings();
43+
const fs = useLocalFS();
4344

4445
function handleRightClick(event: React.MouseEvent) {
4546
clickPosition.current = { x: event.clientX, y: event.clientY };
@@ -61,7 +62,8 @@ function DirectoryOrFile({
6162
<ContextMenu
6263
position={clickPosition.current}
6364
items={getFSObjectContextMenu(
64-
fsObject.type,
65+
fsObject,
66+
fs,
6567
setContextAction,
6668
setContextMenuOpen
6769
)}

src/programs/FileBrowser/contextMenus.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MenuItemProps } from "../../components/MenuItems";
2-
import { FSObjectType } from "../../stores/localFS";
2+
import { FSObject, FSObjectType, LocalFSState } from "../../stores/localFS";
33

44
export type ContextMenuAction = "delete" | "rename";
55

@@ -29,15 +29,21 @@ export function getMainContentContextItems(
2929
}
3030

3131
export function getFSObjectContextMenu(
32-
fsObjectType: FSObjectType,
32+
fsObject: FSObject,
33+
fs: LocalFSState,
3334
setContextAction: (action: ContextMenuAction) => void,
3435
setContextMenuOpen: (open: boolean) => void
3536
): Array<MenuItemProps> {
36-
switch (fsObjectType) {
37+
switch (fsObject.type) {
3738
case "file":
3839
return getFSFileContextMenu(setContextAction, setContextMenuOpen);
3940
case "directory":
40-
return getFSDirectoryContextMenu(setContextAction, setContextMenuOpen);
41+
return getFSDirectoryContextMenu(
42+
fsObject,
43+
fs,
44+
setContextAction,
45+
setContextMenuOpen
46+
);
4147
}
4248
}
4349

@@ -64,7 +70,10 @@ function getFSFileContextMenu(
6470
}
6571

6672
function getFSDirectoryContextMenu(
73+
fsObject: FSObject,
74+
fs: LocalFSState,
6775
setContextAction: (action: ContextMenuAction) => void,
76+
6877
setContextMenuOpen: (open: boolean) => void
6978
): Array<MenuItemProps> {
7079
return [
@@ -82,5 +91,16 @@ function getFSDirectoryContextMenu(
8291
setContextMenuOpen(false);
8392
},
8493
},
94+
{
95+
title: fs.favorites.includes(fsObject.path)
96+
? "Remove from Favorites"
97+
: "Add to Favorites",
98+
action: () => {
99+
fs.favorites.includes(fsObject.path)
100+
? fs.removeFromFavorites(fsObject.path)
101+
: fs.addToFavorites(fsObject.path);
102+
setContextMenuOpen(false);
103+
},
104+
},
85105
];
86106
}

src/stores/localFS.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export interface LocalFSState {
123123
delete: (parentDirectory: FSDirectory, fsObject: FSObject) => void;
124124
getParentDirectory: (path: string) => FSDirectory | null;
125125
getLastFromPath: (path: string) => string | null;
126+
addToFavorites: (path: string) => void;
127+
removeFromFavorites: (path: string) => void;
126128
}
127129

128130
export const useLocalFS = create<LocalFSState>()(
@@ -290,12 +292,32 @@ export const useLocalFS = create<LocalFSState>()(
290292
// If the FSObject is in the favorites, update that too.
291293
const favorites = get().favorites;
292294
const favIndex = favorites.findIndex((fav) => fav === oldPath);
293-
if (favIndex) {
295+
if (favIndex >= 0) {
294296
favorites[favIndex] = fsObject.path;
295297
}
296298

297299
set({ root: get().root, favorites });
298300
};
301+
302+
const addToFavorites: LocalFSState["addToFavorites"] = (path) => {
303+
const oldFavorites = get().favorites;
304+
const favorites = [...oldFavorites];
305+
if (!favorites.includes(path)) {
306+
favorites.push(path);
307+
}
308+
set({ favorites });
309+
};
310+
const removeFromFavorites: LocalFSState["removeFromFavorites"] = (
311+
path
312+
) => {
313+
const oldFavorites = get().favorites;
314+
const favorites = [...oldFavorites];
315+
const index = favorites.indexOf(path);
316+
if (index >= 0) {
317+
favorites.splice(index, 1);
318+
}
319+
set({ favorites });
320+
};
299321
return {
300322
root: rootDir,
301323
favorites: [
@@ -316,6 +338,8 @@ export const useLocalFS = create<LocalFSState>()(
316338
getParentDirectory,
317339
rename,
318340
validateFSObjectName,
341+
addToFavorites,
342+
removeFromFavorites,
319343
};
320344
},
321345
{

0 commit comments

Comments
 (0)