mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: local volume explore file system
This commit is contained in:
@@ -8,6 +8,7 @@ import { useForm } from "react-hook-form";
|
||||
import { testConnectionMutation } from "~/api-client/@tanstack/react-query.gen";
|
||||
import { cn, slugify } from "~/lib/utils";
|
||||
import { deepClean } from "~/utils/object";
|
||||
import { DirectoryBrowser } from "./directory-browser";
|
||||
import { Button } from "./ui/button";
|
||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "./ui/form";
|
||||
import { Input } from "./ui/input";
|
||||
@@ -30,7 +31,7 @@ type Props = {
|
||||
};
|
||||
|
||||
const defaultValuesForType = {
|
||||
directory: { backend: "directory" as const },
|
||||
directory: { backend: "directory" as const, path: "/" },
|
||||
nfs: { backend: "nfs" as const, port: 2049, version: "4.1" as const },
|
||||
smb: { backend: "smb" as const, port: 445, vers: "3.0" as const },
|
||||
webdav: { backend: "webdav" as const, port: 80, ssl: false },
|
||||
@@ -52,8 +53,10 @@ export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, for
|
||||
const watchedName = watch("name");
|
||||
|
||||
useEffect(() => {
|
||||
form.reset({ name: watchedName, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType] });
|
||||
}, [watchedBackend, watchedName, form.reset]);
|
||||
if (mode === "create") {
|
||||
form.reset({ name: watchedName, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType] });
|
||||
}
|
||||
}, [watchedBackend, watchedName, form.reset, mode]);
|
||||
|
||||
const [testMessage, setTestMessage] = useState<{ success: boolean; message: string } | null>(null);
|
||||
|
||||
@@ -133,6 +136,39 @@ export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, for
|
||||
)}
|
||||
/>
|
||||
|
||||
{watchedBackend === "directory" && (
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="path"
|
||||
render={({ field }) => {
|
||||
const [showBrowser, setShowBrowser] = useState(!field.value || field.value === "/");
|
||||
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>Directory Path</FormLabel>
|
||||
<FormControl>
|
||||
{!showBrowser && field.value ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex-1 border rounded-md p-3 bg-muted/50">
|
||||
<div className="text-xs font-medium text-muted-foreground mb-1">Selected path:</div>
|
||||
<div className="text-sm font-mono break-all">{field.value}</div>
|
||||
</div>
|
||||
<Button type="button" variant="outline" size="sm" onClick={() => setShowBrowser(true)}>
|
||||
Change
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<DirectoryBrowser onSelectPath={(path) => field.onChange(path)} selectedPath={field.value} />
|
||||
)}
|
||||
</FormControl>
|
||||
<FormDescription>Browse and select a directory on the host filesystem to track.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{watchedBackend === "nfs" && (
|
||||
<>
|
||||
<FormField
|
||||
|
||||
133
apps/client/app/components/directory-browser.tsx
Normal file
133
apps/client/app/components/directory-browser.tsx
Normal file
@@ -0,0 +1,133 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import { browseFilesystemOptions } from "~/api-client/@tanstack/react-query.gen";
|
||||
import { FileTree, type FileEntry } from "./file-tree";
|
||||
import { ScrollArea } from "./ui/scroll-area";
|
||||
|
||||
type Props = {
|
||||
onSelectPath: (path: string) => void;
|
||||
selectedPath?: string;
|
||||
};
|
||||
|
||||
export const DirectoryBrowser = ({ onSelectPath, selectedPath }: Props) => {
|
||||
const queryClient = useQueryClient();
|
||||
const [expandedFolders, setExpandedFolders] = useState<Set<string>>(new Set());
|
||||
const [fetchedFolders, setFetchedFolders] = useState<Set<string>>(new Set(["/"]));
|
||||
const [loadingFolders, setLoadingFolders] = useState<Set<string>>(new Set());
|
||||
const [allFiles, setAllFiles] = useState<Map<string, FileEntry>>(new Map());
|
||||
|
||||
const { data, isLoading } = useQuery({
|
||||
...browseFilesystemOptions({ query: { path: "/" } }),
|
||||
});
|
||||
|
||||
useMemo(() => {
|
||||
if (data?.directories) {
|
||||
setAllFiles((prev) => {
|
||||
const next = new Map(prev);
|
||||
for (const dir of data.directories) {
|
||||
next.set(dir.path, { name: dir.name, path: dir.path, type: "folder" });
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
const fileArray = useMemo(() => Array.from(allFiles.values()), [allFiles]);
|
||||
|
||||
const handleFolderExpand = useCallback(
|
||||
async (folderPath: string) => {
|
||||
setExpandedFolders((prev) => {
|
||||
const next = new Set(prev);
|
||||
next.add(folderPath);
|
||||
return next;
|
||||
});
|
||||
|
||||
if (!fetchedFolders.has(folderPath)) {
|
||||
setLoadingFolders((prev) => new Set(prev).add(folderPath));
|
||||
|
||||
try {
|
||||
const result = await queryClient.fetchQuery(
|
||||
browseFilesystemOptions({
|
||||
query: { path: folderPath },
|
||||
}),
|
||||
);
|
||||
|
||||
if (result.directories) {
|
||||
setAllFiles((prev) => {
|
||||
const next = new Map(prev);
|
||||
for (const dir of result.directories) {
|
||||
next.set(dir.path, { name: dir.name, path: dir.path, type: "folder" });
|
||||
}
|
||||
return next;
|
||||
});
|
||||
|
||||
setFetchedFolders((prev) => new Set(prev).add(folderPath));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch folder contents:", error);
|
||||
} finally {
|
||||
setLoadingFolders((prev) => {
|
||||
const next = new Set(prev);
|
||||
next.delete(folderPath);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
[fetchedFolders, queryClient],
|
||||
);
|
||||
|
||||
const handleFolderHover = useCallback(
|
||||
(folderPath: string) => {
|
||||
if (!fetchedFolders.has(folderPath) && !loadingFolders.has(folderPath)) {
|
||||
queryClient.prefetchQuery(browseFilesystemOptions({ query: { path: folderPath } }));
|
||||
}
|
||||
},
|
||||
[fetchedFolders, loadingFolders, queryClient],
|
||||
);
|
||||
|
||||
if (isLoading && fileArray.length === 0) {
|
||||
return (
|
||||
<div className="border rounded-lg overflow-hidden">
|
||||
<ScrollArea className="h-64">
|
||||
<div className="text-sm text-gray-500 p-4">Loading directories...</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (fileArray.length === 0) {
|
||||
return (
|
||||
<div className="border rounded-lg overflow-hidden">
|
||||
<ScrollArea className="h-64">
|
||||
<div className="text-sm text-gray-500 p-4">No subdirectories found</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="border rounded-lg overflow-hidden">
|
||||
<ScrollArea className="h-64">
|
||||
<FileTree
|
||||
files={fileArray}
|
||||
onFolderExpand={handleFolderExpand}
|
||||
onFolderHover={handleFolderHover}
|
||||
expandedFolders={expandedFolders}
|
||||
loadingFolders={loadingFolders}
|
||||
foldersOnly
|
||||
selectableFolders
|
||||
selectedFolder={selectedPath}
|
||||
onFolderSelect={onSelectPath}
|
||||
/>
|
||||
</ScrollArea>
|
||||
|
||||
{selectedPath && (
|
||||
<div className="bg-muted/50 border-t p-2 text-sm">
|
||||
<div className="font-medium text-muted-foreground">Selected path:</div>
|
||||
<div className="font-mono text-xs break-all">{selectedPath}</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -36,6 +36,9 @@ interface Props {
|
||||
selectedPaths?: Set<string>;
|
||||
onSelectionChange?: (selectedPaths: Set<string>) => void;
|
||||
foldersOnly?: boolean;
|
||||
selectableFolders?: boolean;
|
||||
onFolderSelect?: (folderPath: string) => void;
|
||||
selectedFolder?: string;
|
||||
}
|
||||
|
||||
export const FileTree = memo((props: Props) => {
|
||||
@@ -52,6 +55,9 @@ export const FileTree = memo((props: Props) => {
|
||||
selectedPaths = new Set(),
|
||||
onSelectionChange,
|
||||
foldersOnly = false,
|
||||
selectableFolders = false,
|
||||
onFolderSelect,
|
||||
selectedFolder,
|
||||
} = props;
|
||||
|
||||
const fileList = useMemo(() => {
|
||||
@@ -126,6 +132,13 @@ export const FileTree = memo((props: Props) => {
|
||||
[onFileSelect],
|
||||
);
|
||||
|
||||
const handleFolderSelect = useCallback(
|
||||
(folderPath: string) => {
|
||||
onFolderSelect?.(folderPath);
|
||||
},
|
||||
[onFolderSelect],
|
||||
);
|
||||
|
||||
const handleSelectionChange = useCallback(
|
||||
(path: string, checked: boolean) => {
|
||||
const newSelection = new Set(selectedPaths);
|
||||
@@ -299,6 +312,9 @@ export const FileTree = memo((props: Props) => {
|
||||
checked={isPathSelected(fileOrFolder.fullPath) && !isPartiallySelected(fileOrFolder.fullPath)}
|
||||
partiallyChecked={isPartiallySelected(fileOrFolder.fullPath)}
|
||||
onCheckboxChange={handleSelectionChange}
|
||||
selectableMode={selectableFolders}
|
||||
onFolderSelect={handleFolderSelect}
|
||||
selected={selectedFolder === fileOrFolder.fullPath}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -321,6 +337,9 @@ interface FolderProps {
|
||||
checked?: boolean;
|
||||
partiallyChecked?: boolean;
|
||||
onCheckboxChange?: (path: string, checked: boolean) => void;
|
||||
selectableMode?: boolean;
|
||||
onFolderSelect?: (folderPath: string) => void;
|
||||
selected?: boolean;
|
||||
}
|
||||
|
||||
const Folder = memo(
|
||||
@@ -334,6 +353,9 @@ const Folder = memo(
|
||||
checked,
|
||||
partiallyChecked,
|
||||
onCheckboxChange,
|
||||
selectableMode,
|
||||
onFolderSelect,
|
||||
selected,
|
||||
}: FolderProps) => {
|
||||
const { depth, name, fullPath } = folder;
|
||||
const FolderIconComponent = collapsed ? FolderIcon : FolderOpen;
|
||||
@@ -359,9 +381,19 @@ const Folder = memo(
|
||||
[onCheckboxChange, fullPath],
|
||||
);
|
||||
|
||||
const handleFolderClick = useCallback(() => {
|
||||
if (selectableMode) {
|
||||
onFolderSelect?.(fullPath);
|
||||
}
|
||||
}, [selectableMode, onFolderSelect, fullPath]);
|
||||
|
||||
return (
|
||||
<NodeButton
|
||||
className={cn("group hover:bg-accent/50 text-foreground")}
|
||||
className={cn("group", {
|
||||
"hover:bg-accent/50 text-foreground": !selected,
|
||||
"bg-accent text-accent-foreground": selected,
|
||||
"cursor-pointer": selectableMode,
|
||||
})}
|
||||
depth={depth}
|
||||
icon={
|
||||
loading ? (
|
||||
@@ -372,6 +404,7 @@ const Folder = memo(
|
||||
<ChevronDown className="w-4 h-4 shrink-0 cursor-pointer" onClick={handleChevronClick} />
|
||||
)
|
||||
}
|
||||
onClick={selectableMode ? handleFolderClick : undefined}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
>
|
||||
{withCheckbox && (
|
||||
|
||||
Reference in New Issue
Block a user