Skip to content

Commit

Permalink
Merge pull request #121 from MathisBurger/feature/file-tree-remove-op…
Browse files Browse the repository at this point in the history
…tion

File tree remove option
  • Loading branch information
MathisBurger authored Nov 11, 2024
2 parents 59c0053 + 7b49757 commit a0458b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions web/components/FileStructure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ const FileStructure = ({
isFolder={hasChildren}
expanded={expanded}
isTestFile={node?.nodeProps?.is_test_file ?? false}
fileStructure={structure}
setFileStructure={setStructure ? setStructure : () => {}}
setIsTestFile={(s) =>
setStructure
? setStructure(
Expand Down
38 changes: 28 additions & 10 deletions web/components/FileStructureComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { IconPlus } from "@tabler/icons-react";
import {IconPlus, IconTrash} from "@tabler/icons-react";
import {
Checkbox,
FocusTrap,
Group,
GroupProps,
TextInput,
Text,
Text, ActionIcon,
} from "@mantine/core";
import { useState } from "react";
import FileIcon from "@/components/FileIcon";
import { useForm } from "@mantine/form";
import {useTranslation} from "react-i18next";
import {FileStructureTree} from "@/components/FileStructure";
import {removeFile} from "@/utils/FileStructure";

interface FileStructureNewInputProps {
label: string;
Expand Down Expand Up @@ -65,6 +67,8 @@ interface FileStructureElementProps {
isFolder: boolean;
expanded: boolean;
setIsTestFile: (is: boolean) => void;
fileStructure: FileStructureTree;
setFileStructure: (structure: FileStructureTree) => void;
editable: boolean;
}

Expand All @@ -79,6 +83,8 @@ export const FileStructureElement = (
isTestFile: _3,
setIsTestFile: _4,
editable: _5,
setFileStructure: _6,
fileStructure: _7,
...elementProps
} = props;
/* eslint-enable @typescript-eslint/no-unused-vars */
Expand All @@ -92,14 +98,26 @@ export const FileStructureElement = (
expanded={props.expanded}
/>
<Text>{props.label}</Text>
{!props.isFolder && props.editable && (
<Checkbox
label={t('fields.test-file')}
checked={props.isTestFile}
onChange={(e) => props.setIsTestFile(e.target.checked)}
style={{ marginLeft: "auto" }}
/>
)}
<Group style={{marginLeft: "auto"}}>
{!props.isFolder && props.editable && (
<Checkbox
label={t('fields.test-file')}
checked={props.isTestFile}
onChange={(e) => props.setIsTestFile(e.target.checked)}

/>
)}
{props.editable && (
<ActionIcon
variant="light"
color="red"
style={{ marginLeft: "auto" }}
onClick={() => props.setFileStructure(removeFile(props.fileStructure, props.label, props.isFolder))}
>
<IconTrash />
</ActionIcon>
)}
</Group>
</Group>
);
};
26 changes: 26 additions & 0 deletions web/utils/FileStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,29 @@ export const removeObjectIds = (structure: FileStructureTree, fileNames: string[
});
return structure;
}

/**
* Removes a file from the file structure
*
* @param structure The file structure
* @param fileName The name of the file
* @param isFolder If the file to delete is a folder
*/
export const removeFile = (structure: FileStructureTree, fileName: string, isFolder: boolean): FileStructureTree => {
if (isFolder) {
const beforeSize = (structure.folders ?? []).length;
structure.folders = (structure.folders ?? []).filter((f) => f.current_folder_name !== fileName);

// Early return to prevent tree from being further searched
if (structure.folders.length != beforeSize) return structure;
} else {
const beforeSize = structure.files.length;
structure.files = structure.files.filter((f) => f.filename !== fileName);

// Early return to prevent tree from being further searched
if (structure.files.length != beforeSize) return structure;
}

structure.folders = (structure.folders ?? []).map((folder) => removeFile(folder, fileName, isFolder));
return structure;
}

0 comments on commit a0458b0

Please sign in to comment.