Navigation is allowed to all folders
regardless of if images are contained in a folder Some cleanup is not done yet. See TODO
This commit is contained in:
parent
25d17c5cde
commit
80f11b2539
|
|
@ -4,7 +4,7 @@ import FolderOpenIcon from "@mui/icons-material/FolderOpen";
|
||||||
import PhotoOutlined from "@mui/icons-material/PhotoOutlined";
|
import PhotoOutlined from "@mui/icons-material/PhotoOutlined";
|
||||||
import { SimpleTreeView, TreeItem } from "@mui/x-tree-view";
|
import { SimpleTreeView, TreeItem } from "@mui/x-tree-view";
|
||||||
import { useLocation, useNavigate } from "react-router-dom";
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import React, { useMemo, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { Folders } from "./models";
|
import { Folders } from "./models";
|
||||||
import Toolbar from "@mui/material/Toolbar";
|
import Toolbar from "@mui/material/Toolbar";
|
||||||
import { Chip, useTheme } from "@mui/material";
|
import { Chip, useTheme } from "@mui/material";
|
||||||
|
|
@ -15,7 +15,7 @@ import Typography from "@mui/material/Typography";
|
||||||
|
|
||||||
function generateTreeViewChildren(
|
function generateTreeViewChildren(
|
||||||
folders: Folders[],
|
folders: Folders[],
|
||||||
navigateAndToggleExpand: (_path: string, _navigationAllowed: boolean) => void,
|
navigateAndToggleExpand: (_path: string) => void,
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -28,16 +28,13 @@ function generateTreeViewChildren(
|
||||||
{f.name} <Chip label={f.numberOfFiles} size="small" />
|
{f.name} <Chip label={f.numberOfFiles} size="small" />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
const containsImages = f.numberOfFiles > 0;
|
|
||||||
if (f.children.length === 0) {
|
if (f.children.length === 0) {
|
||||||
return (
|
return (
|
||||||
<TreeItem
|
<TreeItem
|
||||||
key={f.fullPath}
|
key={f.fullPath}
|
||||||
itemId={f.fullPath}
|
itemId={f.fullPath}
|
||||||
label={label}
|
label={label}
|
||||||
onClick={() =>
|
onClick={() => navigateAndToggleExpand(f.fullPath)}
|
||||||
navigateAndToggleExpand(f.fullPath, containsImages)
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +43,7 @@ function generateTreeViewChildren(
|
||||||
key={f.fullPath}
|
key={f.fullPath}
|
||||||
itemId={f.fullPath}
|
itemId={f.fullPath}
|
||||||
label={label}
|
label={label}
|
||||||
onClick={() => navigateAndToggleExpand(f.fullPath, containsImages)}
|
onClick={() => navigateAndToggleExpand(f.fullPath)}
|
||||||
>
|
>
|
||||||
{generateTreeViewChildren(f.children, navigateAndToggleExpand)}
|
{generateTreeViewChildren(f.children, navigateAndToggleExpand)}
|
||||||
</TreeItem>
|
</TreeItem>
|
||||||
|
|
@ -56,29 +53,32 @@ function generateTreeViewChildren(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const calcFolderWithItem = (
|
|
||||||
cur: Folders,
|
|
||||||
calculated: Set<string>,
|
|
||||||
): Set<string> => {
|
|
||||||
if (cur.numberOfFiles > 0 || cur.children.length == 0) {
|
|
||||||
calculated.add(cur.fullPath);
|
|
||||||
}
|
|
||||||
cur.children.forEach((a) => calcFolderWithItem(a, calculated));
|
|
||||||
return calculated;
|
|
||||||
};
|
|
||||||
|
|
||||||
const GenerateTreeView = ({ root }: { root: Folders }) => {
|
const GenerateTreeView = ({ root }: { root: Folders }) => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const folderFullPathContainingPhotos = useMemo(
|
|
||||||
() => calcFolderWithItem(root, new Set()),
|
|
||||||
[root],
|
|
||||||
);
|
|
||||||
const [expandedItems, setExpandedItems] = useState<string[]>(
|
const [expandedItems, setExpandedItems] = useState<string[]>(
|
||||||
getDefaultExpanded(location.pathname),
|
getDefaultExpanded(location.pathname),
|
||||||
);
|
);
|
||||||
const [selectedItem, setSelectedItem] = useState<string>(location.pathname);
|
const [selectedItem, setSelectedItem] = useState<string>(location.pathname);
|
||||||
|
|
||||||
|
// TODO: clean this effect up. See also `getDefaultExpanded`
|
||||||
|
useEffect(() => {
|
||||||
|
let curPathname = location.pathname.startsWith("/")
|
||||||
|
? location.pathname.slice(1)
|
||||||
|
: location.pathname;
|
||||||
|
while (curPathname.endsWith("/")) {
|
||||||
|
curPathname = curPathname.slice(0, -1);
|
||||||
|
}
|
||||||
|
const parentPathname = curPathname.substring(
|
||||||
|
0,
|
||||||
|
curPathname.lastIndexOf("/"),
|
||||||
|
);
|
||||||
|
if (!expandedItems.includes(parentPathname)) {
|
||||||
|
setExpandedItems([parentPathname, ...expandedItems]);
|
||||||
|
}
|
||||||
|
setSelectedItem(curPathname);
|
||||||
|
}, [location]);
|
||||||
|
|
||||||
const toggleExpanded = (path: string) => {
|
const toggleExpanded = (path: string) => {
|
||||||
if (expandedItems.includes(path)) {
|
if (expandedItems.includes(path)) {
|
||||||
setExpandedItems(expandedItems.filter((p) => p !== path));
|
setExpandedItems(expandedItems.filter((p) => p !== path));
|
||||||
|
|
@ -87,16 +87,11 @@ const GenerateTreeView = ({ root }: { root: Folders }) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const navigateAndToggleExpand = (
|
const navigateAndToggleExpand = (path: string) => {
|
||||||
path: string,
|
|
||||||
navigationAllowed: boolean,
|
|
||||||
) => {
|
|
||||||
if (!navigationAllowed || location.pathname === path) {
|
|
||||||
toggleExpanded(path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
toggleExpanded(path);
|
toggleExpanded(path);
|
||||||
navigate(path);
|
if (location.pathname !== path) {
|
||||||
|
navigate(path);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -109,7 +104,7 @@ const GenerateTreeView = ({ root }: { root: Folders }) => {
|
||||||
expandedItems={expandedItems}
|
expandedItems={expandedItems}
|
||||||
selectedItems={selectedItem}
|
selectedItems={selectedItem}
|
||||||
onSelectedItemsChange={(event, itemId) => {
|
onSelectedItemsChange={(event, itemId) => {
|
||||||
if (itemId != null && folderFullPathContainingPhotos.has(itemId)) {
|
if (itemId != null) {
|
||||||
setSelectedItem(itemId);
|
setSelectedItem(itemId);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ function ImageGalleryLayout() {
|
||||||
{images.length > 0 && <ImageGallery images={images} />}
|
{images.length > 0 && <ImageGallery images={images} />}
|
||||||
{images.length == 0 && foldersPreview.length == 0 && (
|
{images.length == 0 && foldersPreview.length == 0 && (
|
||||||
<p>
|
<p>
|
||||||
No images available. You may want to add images in your root
|
No images available. You may want to add images to this
|
||||||
directory.
|
directory.
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue