Skip to content

Commit 3173cac

Browse files
committed
Updated FileSelector to have a two step process to confirm file selection
1 parent 0335158 commit 3173cac

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

frontend/src/components/navigation/FileSelector.tsx

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,27 @@ import TextIcon from "@mui/icons-material/Description";
2424
import { fetchDatasets } from "../../actions/dataset";
2525
import { V2 } from "../../openapi";
2626

27-
// Define the file details
2827
interface FileDetails {
2928
fileId: string;
3029
fileName: string;
3130
}
3231

33-
// Define the RecursiveComponent component with props type
3432
interface RecursiveComponentProps {
3533
item: FSItem;
3634
depth?: number;
37-
onSelectFile: (fileId: string, fileName: string) => void;
35+
onHighlightFile: (fileId: string, fileName: string) => void;
36+
highlightedFileId: string;
3837
}
3938

40-
// Define a type for items in the directory structure
4139
interface FSItem {
4240
datasetId: string;
4341
id?: string;
4442
label: string;
4543
children?: FSItem[] | undefined;
46-
type: string; // A FSItem can be a folder or a file,
44+
type: string;
4745
content_type?: string;
4846
}
4947

50-
// Function to fetch children of a folder
5148
async function fetchFolderFiles(
5249
datasetid: string,
5350
folderId: string | undefined
@@ -57,7 +54,6 @@ async function fetchFolderFiles(
5754
await V2.DatasetsService.getDatasetFoldersAndFilesApiV2DatasetsDatasetIdFoldersAndFilesGet(
5855
datasetid,
5956
folderId,
60-
// TODO: Remove hardcoded values
6157
0,
6258
3000
6359
);
@@ -85,13 +81,14 @@ async function fetchFolderFiles(
8581
const RecursiveComponent: React.FC<RecursiveComponentProps> = ({
8682
item,
8783
depth = 0,
88-
onSelectFile,
84+
onHighlightFile,
85+
highlightedFileId,
8986
}) => {
9087
const [expanded, setExpanded] = useState(false);
9188
const [children, setChildren] = useState<FSItem[] | undefined>(item.children);
9289
const isFolderOrDataset = item.type === "folder" || item.type === "dataset";
90+
const isHighlighted = item.id === highlightedFileId;
9391

94-
// Function to generate Icon based on item type
9592
const getIcon = () => {
9693
if (item.type === "folder") {
9794
return <FolderIcon />;
@@ -112,7 +109,6 @@ const RecursiveComponent: React.FC<RecursiveComponentProps> = ({
112109
}
113110
};
114111

115-
// Function to handle selection of folder or file
116112
const onSelect = () => {
117113
if (isFolderOrDataset) {
118114
if (!expanded) {
@@ -123,21 +119,25 @@ const RecursiveComponent: React.FC<RecursiveComponentProps> = ({
123119
setExpanded(!expanded);
124120
} else {
125121
if (item.id !== undefined) {
126-
onSelectFile(item.id, item.label);
122+
onHighlightFile(item.id, item.label);
127123
}
128124
}
129125
};
130126

131127
return (
132128
<List disablePadding>
133-
{/* Indentation of item proportional to depth */}
134129
<ListItem
135130
sx={{
136131
pl: depth * 2,
137132
borderBottom: "none",
138133
py: 0.5,
134+
backgroundColor: isHighlighted
135+
? "rgba(25, 118, 210, 0.12)"
136+
: "transparent",
139137
"&:hover": {
140-
backgroundColor: "rgba(0, 0, 0, 0.1)", // or any other color
138+
backgroundColor: isHighlighted
139+
? "rgba(25, 118, 210, 0.2)"
140+
: "rgba(0, 0, 0, 0.1)",
141141
cursor: "pointer",
142142
},
143143
}}
@@ -166,7 +166,8 @@ const RecursiveComponent: React.FC<RecursiveComponentProps> = ({
166166
key={child.id}
167167
item={child}
168168
depth={depth + 1}
169-
onSelectFile={onSelectFile}
169+
onHighlightFile={onHighlightFile}
170+
highlightedFileId={highlightedFileId}
170171
/>
171172
))}
172173
</Box>
@@ -177,22 +178,20 @@ const RecursiveComponent: React.FC<RecursiveComponentProps> = ({
177178
};
178179

179180
const FileSystemViewer: React.FC<{
180-
onSelectFile: (fileId: string, fileName: string) => void;
181-
}> = ({ onSelectFile }) => {
181+
onHighlightFile: (fileId: string, fileName: string) => void;
182+
highlightedFileId: string;
183+
}> = ({ onHighlightFile, highlightedFileId }) => {
182184
const dispatch = useDispatch();
183185
const datasets = useSelector((state: any) => state.dataset.datasets);
184186
const [FSItems, setFSItems] = useState<FSItem[]>([]);
185187

186-
// API function call to get Datasets
187188
const listDatasets = (skip?: number, limit?: number, mine?: boolean) => {
188189
dispatch(fetchDatasets(skip, limit, mine));
189190
};
190191

191-
// Fetch datasets on component mount
192192
useEffect(() => {
193-
// TODO: Remove hardcoded values for skip and limit
194193
listDatasets(0, 3000, true);
195-
}, []); //
194+
}, []);
196195

197196
useEffect(() => {
198197
if (datasets.data) {
@@ -225,20 +224,21 @@ const FileSystemViewer: React.FC<{
225224
<RecursiveComponent
226225
key={FSItem.id}
227226
item={FSItem}
228-
onSelectFile={onSelectFile}
227+
onHighlightFile={onHighlightFile}
228+
highlightedFileId={highlightedFileId}
229229
/>
230230
))}
231231
</Box>
232232
) : null;
233233
};
234234

235235
const DatasetFileViewer: React.FC<{
236-
onSelectFile: (fileId: string, fileName: string) => void;
236+
onHighlightFile: (fileId: string, fileName: string) => void;
237+
highlightedFileId: string;
237238
datasetId: string;
238-
}> = ({ onSelectFile, datasetId }) => {
239+
}> = ({ onHighlightFile, highlightedFileId, datasetId }) => {
239240
const [FSItems, setFSItems] = useState<FSItem[]>([]);
240241

241-
// Only display contents of the passed dataset
242242
useEffect(() => {
243243
fetchFolderFiles(datasetId, undefined).then((data) => {
244244
setFSItems(data);
@@ -263,7 +263,8 @@ const DatasetFileViewer: React.FC<{
263263
<RecursiveComponent
264264
key={FSItem.id}
265265
item={FSItem}
266-
onSelectFile={onSelectFile}
266+
onHighlightFile={onHighlightFile}
267+
highlightedFileId={highlightedFileId}
267268
/>
268269
))}
269270
</Box>
@@ -280,14 +281,27 @@ const FileSelector: React.FC<{
280281
fileId: "",
281282
fileName: "",
282283
});
284+
const [highlightedFile, setHighlightedFile] = useState<FileDetails>({
285+
fileId: "",
286+
fileName: "",
287+
});
283288

284289
const handleOpen = () => setOpen(true);
285-
const handleClose = () => setOpen(false);
290+
const handleClose = () => {
291+
setHighlightedFile({ fileId: "", fileName: "" });
292+
setOpen(false);
293+
};
286294

287-
const handleFileSelect = (fileId: string, fileName: string) => {
288-
setSelectedFile({ fileId: fileId, fileName: fileName });
289-
onChange(fileId);
290-
handleClose();
295+
const handleHighlight = (fileId: string, fileName: string) => {
296+
setHighlightedFile({ fileId, fileName });
297+
};
298+
299+
const handleConfirmSelection = () => {
300+
if (highlightedFile.fileId) {
301+
setSelectedFile(highlightedFile);
302+
onChange(highlightedFile.fileId);
303+
handleClose();
304+
}
291305
};
292306

293307
return (
@@ -324,12 +338,25 @@ const FileSelector: React.FC<{
324338
>
325339
{showOnlyDatasetFiles ? (
326340
<DatasetFileViewer
327-
onSelectFile={handleFileSelect}
341+
onHighlightFile={handleHighlight}
342+
highlightedFileId={highlightedFile.fileId}
328343
datasetId={datasetId as string}
329344
/>
330345
) : (
331-
<FileSystemViewer onSelectFile={handleFileSelect} />
346+
<FileSystemViewer
347+
onHighlightFile={handleHighlight}
348+
highlightedFileId={highlightedFile.fileId}
349+
/>
332350
)}
351+
<Box sx={{ display: "flex", justifyContent: "flex-end", mt: 2 }}>
352+
<Button
353+
variant="contained"
354+
onClick={handleConfirmSelection}
355+
disabled={!highlightedFile.fileId}
356+
>
357+
Select
358+
</Button>
359+
</Box>
333360
</Box>
334361
</Modal>
335362
</Box>

0 commit comments

Comments
 (0)