Skip to content

Commit

Permalink
Add Folder selection to FileBrowser component
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-neurosc committed Nov 25, 2024
1 parent 550bd49 commit d8581c1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
36 changes: 18 additions & 18 deletions gui_dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"@emotion/react": "^11.13.5",
"@emotion/styled": "^11.13.5",
"@mui/icons-material": "^6.1.8",
"@mui/material": "^6.1.8",
"immer": "^10.1.1",
"plotly.js-basic-dist-min": "^2.35.2",
"react": "next",
"react-dom": "next",
"react-router-dom": "^6.26.2",
"zustand": "latest"
"react-router-dom": "^7.0.1",
"zustand": "^5.0.1"
},
"devDependencies": {
"@babel/core": "^7.25.7",
"@babel/eslint-parser": "^7.25.7",
"@babel/preset-env": "^7.25.7",
"@babel/preset-react": "^7.25.7",
"@eslint/compat": "^1.2.0",
"@vitejs/plugin-react": "^4.3.2",
"@babel/core": "^7.26.0",
"@babel/eslint-parser": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"@eslint/compat": "^1.2.3",
"@vitejs/plugin-react": "^4.3.3",
"@welldone-software/why-did-you-render": "^8.0.3",
"babel-plugin-react-compiler": "latest",
"eslint": "^9.12.0",
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jsdoc": "^50.3.1",
"eslint-plugin-react": "^7.37.1",
"eslint-plugin-jsdoc": "^50.5.0",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-compiler": "latest",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.12",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"prettier": "^3.3.3",
"vite": "^5.4.8"
"vite": "^5.4.11"
}
}
45 changes: 32 additions & 13 deletions gui_dev/src/components/FileBrowser/FileBrowser.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useReducer, useEffect } from "react";
import {
Box,
Button,
Paper,
Typography,
TextField,
Expand All @@ -14,6 +15,7 @@ import {
TableSortLabel,
Modal,
Select,
Stack,
FormControl,
InputLabel,
Snackbar,
Expand Down Expand Up @@ -89,8 +91,10 @@ const columns = [
export const FileBrowser = ({
isModal = false,
directory = null,
onlyDirectories = false,
allowedExtensions = ALLOWED_EXTENSIONS,
onClose,
onFileSelect,
onSelect,
}) => {
const [state, dispatch] = useReducer(reducer, initialState);

Expand Down Expand Up @@ -141,11 +145,16 @@ export const FileBrowser = ({

const fetchFiles = async (path) => {
try {
const files = await fileManager.getFiles({
let files = await fileManager.getFiles({
path,
allowedExtensions: ALLOWED_EXTENSIONS.join(","),
allowedExtensions: allowedExtensions.join(","),
showHidden: state.showHiddenFiles,
});

if (onlyDirectories) {
files = files.filter((file) => file.is_directory);
}

dispatch({
type: "SET_FILES",
payload: files.map((file) => ({
Expand All @@ -164,13 +173,13 @@ export const FileBrowser = ({
}
};

const handleFileClick = (file) => {
const handleRowClick = (file) => {
if (file.is_directory) {
dispatch({ type: "SET_CURRENT_PATH", payload: file.path });
} else if (
ALLOWED_EXTENSIONS.some((ext) => file.name.toLowerCase().endsWith(ext))
) {
onFileSelect(file);
onSelect(file);
}
};

Expand Down Expand Up @@ -272,9 +281,9 @@ export const FileBrowser = ({
};

const FileBrowserContent = (
<Box sx={{ width: "100%", maxWidth: 800, margin: "auto" }}>
<Paper elevation={3} sx={{ p: 2, mb: 2 }}>
<Box display="flex" alignItems="center">
<Stack gap={2} width="100%">
<Paper elevation={3} sx={{ p: 2, width: "100%" }}>
<Stack direction="row" alignItems="center">
{state.drives.length > 1 && (
<FormControl sx={{ minWidth: 120, mr: 2 }}>
<InputLabel id="drive-select-label">Drive</InputLabel>
Expand Down Expand Up @@ -333,15 +342,14 @@ export const FileBrowser = ({
<ListItemText primary="Show Hidden Files" />
</MenuItem>{" "}
</Menu>
</Box>
</Stack>
</Paper>
<Box
sx={{
width: "100%",
height: "100%",
display: "flex",
flexDirection: "row",
maxWidth: 800,
margin: "auto",
gap: 2,
}}
Expand All @@ -353,7 +361,6 @@ export const FileBrowser = ({
sx={{
flexBasis: "75%",
flexShrink: 0,
maxHeight: 400,
scrollbarWidth: "thin",
scrollbarColor: "rgba(0,0,0,.1) transparent",
}}
Expand Down Expand Up @@ -383,7 +390,7 @@ export const FileBrowser = ({
<TableRow
key={file.path}
hover
onClick={() => handleFileClick(file)}
onClick={() => handleRowClick(file)}
sx={{ cursor: "pointer" }}
>
{columns.map((column) => (
Expand All @@ -399,13 +406,25 @@ export const FileBrowser = ({
</Table>
</TableContainer>
</Box>

<Stack direction="row" width="100%" gap={2} justifyContent="center">
{onlyDirectories && (
<Button
variant="contained"
onClick={() => onSelect(state.currentPath)}
>
Select Folder
</Button>
)}
</Stack>

<Snackbar
open={!!state.error}
autoHideDuration={6000}
onClose={() => dispatch({ type: "SET_ERROR", payload: "" })}
message={state.error}
/>
</Box>
</Stack>
);

return isModal ? (
Expand Down
1 change: 0 additions & 1 deletion gui_dev/src/components/FileBrowser/QuickAccess.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const QuickAccessSidebar = ({ onItemClick }) => {
flexGrow: 1,
display: "flex",
flexDirection: "column",
maxHeight: 400,
overflowX: "hidden",
overflowY: "auto",
scrollbarWidth: "thin",
Expand Down
35 changes: 28 additions & 7 deletions gui_dev/src/pages/SourceSelection/FileSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ export const FileSelector = () => {

const [isSelecting, setIsSelecting] = useState(false);
const [showFileBrowser, setShowFileBrowser] = useState(false);
const [showFolderBrowser, setShowFolderBrowser] = useState(false);

useEffect(() => {
setSourceType("lsl");
}, []);

const handleSelectFile = () => {
setShowFileBrowser(true);
};

const handleFileSelect = (file) => {
setIsSelecting(true);

Expand All @@ -50,11 +47,17 @@ export const FileSelector = () => {
}
};

const handleFolderSelect = (folder) => {
setShowFolderBrowser(false);
};

return (
<TitledBox title="Read data from file">
<Button
variant="contained"
onClick={handleSelectFile}
onClick={() => {
setShowFileBrowser(true);
}}
disabled={isSelecting}
sx={{ width: "100%" }}
>
Expand All @@ -66,7 +69,7 @@ export const FileSelector = () => {
Selected File: <i>{fileSource.name}</i>
</Typography>
)}
{fileSource.size && (
{fileSource.size != "0" && (
<Typography variant="body2">File Size: {fileSource.size}</Typography>
)}
{fileSource.path && (
Expand All @@ -80,6 +83,15 @@ export const FileSelector = () => {
>
Open File
</Button>
<Button
variant="contained"
onClick={() => {
setShowFolderBrowser(true);
}}
sx={{ width: "fit-content" }}
>
Select Folder
</Button>
{streamSetupMessage && (
<Typography
variant="body2"
Expand All @@ -94,7 +106,16 @@ export const FileSelector = () => {
isModal={true}
directory={fileBrowserDirRef.current}
onClose={() => setShowFileBrowser(false)}
onFileSelect={handleFileSelect}
onSelect={handleFileSelect}
/>
)}
{showFolderBrowser && (
<FileBrowser
isModal={true}
directory={fileBrowserDirRef.current}
onClose={() => setShowFolderBrowser(false)}
onSelect={handleFolderSelect}
onlyDirectories={true}
/>
)}
</TitledBox>
Expand Down
1 change: 0 additions & 1 deletion py_neuromodulation/gui/backend/app_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Query,
WebSocket,
)
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import ValidationError
Expand Down

0 comments on commit d8581c1

Please sign in to comment.