Add ability to create new folder in host browser
This commit is contained in:
1 parent
069e1d2082
commit
0805551109
5 files changed
+158
-4
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
Open a project in Paseo using your operating system's native file browser instead of Paseo's built-in folder dialog, with an automatic host folder browser fallback on headless servers.
|
||||
|
||||
Selecting **Open from file browser** from the sidebar or running **Open project from file browser** in the Command Center opens a native folder selection window on desktop Paseo hosts. Once you choose a directory, Paseo registers it as a project and opens a workspace there. On headless hosts without an active desktop session (such as remote Linux servers or Kubernetes pods), the screen automatically opens a host folder browser so you can navigate directories and open projects directly.
|
||||
Selecting **Open from file browser** from the sidebar or running **Open project from file browser** in the Command Center opens a native folder selection window on desktop Paseo hosts. Once you choose a directory, Paseo registers it as a project and opens a workspace there. On headless hosts without an active desktop session (such as remote Linux servers or Kubernetes pods), the screen automatically opens a host folder browser so you can navigate directories and open projects directly. You can also create new folders in the current directory and open them as projects from there.
|
||||
|
||||
## Requirements
|
||||
|
||||
|
||||
@@ -4,18 +4,22 @@ import { Icon, ScrollView, TextInput } from "@getpaseo/plugin/client/ui";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { Pressable, Text, View } from "react-native";
|
||||
import { detectRpc, listDirRpc, openFolderRpc, pickAndOpenRpc } from "../shared/picker";
|
||||
import { createFolderRpc, detectRpc, listDirRpc, openFolderRpc, pickAndOpenRpc } from "../shared/picker";
|
||||
|
||||
export function PickerScreen({ theme, layout, host, params, navigation }: PluginScreenProps) {
|
||||
const pickAndOpen = useRpc(pickAndOpenRpc);
|
||||
const openFolder = useRpc(openFolderRpc);
|
||||
const detect = useRpc(detectRpc);
|
||||
const listDir = useRpc(listDirRpc);
|
||||
const createFolder = useRpc(createFolderRpc);
|
||||
|
||||
const [browserOpen, setBrowserOpen] = useState(false);
|
||||
const [currentPath, setCurrentPath] = useState<string | undefined>(undefined);
|
||||
const [showHidden, setShowHidden] = useState(false);
|
||||
const [filter, setFilter] = useState("");
|
||||
const [newFolderName, setNewFolderName] = useState("");
|
||||
const [createMessage, setCreateMessage] = useState<string | null>(null);
|
||||
const [createError, setCreateError] = useState<string | null>(null);
|
||||
const lastTrigger = useRef<string | null>(null);
|
||||
|
||||
const detection = useQuery({
|
||||
@@ -31,6 +35,25 @@ export function PickerScreen({ theme, layout, host, params, navigation }: Plugin
|
||||
mutationFn: (path: string) => openFolder({ path }),
|
||||
});
|
||||
|
||||
const createNewFolder = useMutation({
|
||||
mutationFn: (name: string) => createFolder({ parentPath: currentPath, name }),
|
||||
onSuccess: (result) => {
|
||||
if (result.status === "ok") {
|
||||
setNewFolderName("");
|
||||
setCreateError(null);
|
||||
setCreateMessage(`Created ${result.name}.`);
|
||||
void directory.refetch();
|
||||
} else {
|
||||
setCreateMessage(null);
|
||||
setCreateError(result.message);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
setCreateMessage(null);
|
||||
setCreateError(error instanceof Error ? error.message : "Failed to create the folder.");
|
||||
},
|
||||
});
|
||||
|
||||
const hasGuiPicker = Boolean(detection.data?.effectivePicker);
|
||||
const showBrowser = browserOpen || (detection.isSuccess && !hasGuiPicker);
|
||||
|
||||
@@ -44,6 +67,9 @@ export function PickerScreen({ theme, layout, host, params, navigation }: Plugin
|
||||
setBrowserOpen(false);
|
||||
setCurrentPath(undefined);
|
||||
setFilter("");
|
||||
setNewFolderName("");
|
||||
setCreateMessage(null);
|
||||
setCreateError(null);
|
||||
lastTrigger.current = null;
|
||||
}, [host.id]);
|
||||
|
||||
@@ -163,9 +189,13 @@ export function PickerScreen({ theme, layout, host, params, navigation }: Plugin
|
||||
|
||||
function navigateTo(nextPath: string) {
|
||||
setFilter("");
|
||||
setCreateMessage(null);
|
||||
setCreateError(null);
|
||||
setCurrentPath(nextPath);
|
||||
}
|
||||
|
||||
const canCreateFolder = directory.data?.status === "ok" && Boolean(newFolderName.trim()) && !createNewFolder.isPending;
|
||||
|
||||
return (
|
||||
<ScrollView style={styles.screen} contentContainerStyle={styles.body}>
|
||||
<Text style={styles.heading}>Open a project from {host.label}</Text>
|
||||
@@ -336,6 +366,34 @@ export function PickerScreen({ theme, layout, host, params, navigation }: Plugin
|
||||
<Text style={styles.muted}>No subfolders match here.</Text>
|
||||
) : null}
|
||||
|
||||
<View style={styles.row}>
|
||||
<TextInput
|
||||
value={newFolderName}
|
||||
onChangeText={(value) => {
|
||||
setNewFolderName(value);
|
||||
setCreateMessage(null);
|
||||
setCreateError(null);
|
||||
}}
|
||||
placeholder="New folder name…"
|
||||
placeholderTextColor={theme.colors.foregroundMuted}
|
||||
style={[styles.input, { flex: 1 }]}
|
||||
/>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel="Create new folder"
|
||||
style={styles.secondaryButton}
|
||||
disabled={!canCreateFolder}
|
||||
onPress={() => createNewFolder.mutate(newFolderName.trim())}
|
||||
>
|
||||
<Text style={styles.secondaryButtonText}>
|
||||
{createNewFolder.isPending ? "Creating…" : "New folder"}
|
||||
</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
|
||||
{createMessage ? <Text style={styles.success}>{createMessage}</Text> : null}
|
||||
{createError ? <Text style={styles.error}>{createError}</Text> : null}
|
||||
|
||||
{filteredEntries.map((entry) => (
|
||||
<View key={entry.path} style={styles.folderRow}>
|
||||
<Pressable
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { PluginHandlerContext, PluginServerContext } from "@getpaseo/plugin
|
||||
import { stat } from "node:fs/promises";
|
||||
import { basename } from "node:path";
|
||||
import {
|
||||
createFolder,
|
||||
detectPickers,
|
||||
listDirectory,
|
||||
pickFolder,
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
resolveStartDir,
|
||||
} from "./server/pick-folder";
|
||||
import {
|
||||
createFolderRpc,
|
||||
detectRpc,
|
||||
listDirRpc,
|
||||
openFolderRpc,
|
||||
@@ -94,6 +96,10 @@ export default function contribute(server: PluginServerContext) {
|
||||
return openWorkspaceFolder(paseo, result.path, result.picker);
|
||||
});
|
||||
|
||||
server.handle(createFolderRpc, async ({ parentPath, name }) => {
|
||||
return createFolder(parentPath, name);
|
||||
});
|
||||
|
||||
server.handle(listDirRpc, async ({ path, showHidden }) => {
|
||||
const current = await readSettings();
|
||||
return listDirectory(path ?? resolveStartDir(current.startDir), showHidden ?? false);
|
||||
|
||||
+73
-2
@@ -1,9 +1,9 @@
|
||||
/// <reference types="node" />
|
||||
import { execFile, execFileSync } from "node:child_process";
|
||||
import { accessSync, constants, existsSync } from "node:fs";
|
||||
import { access, readdir, stat } from "node:fs/promises";
|
||||
import { access, mkdir, readdir, stat } from "node:fs/promises";
|
||||
import { homedir } from "node:os";
|
||||
import { delimiter, dirname, join, resolve } from "node:path";
|
||||
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import type { PickerSettings } from "../shared/settings";
|
||||
import { PORTAL_PICK_SCRIPT } from "./portal-script";
|
||||
@@ -224,6 +224,77 @@ export function resolveStartDir(raw?: string): string {
|
||||
return existsSync(expanded) ? resolve(expanded) : home;
|
||||
}
|
||||
|
||||
export type CreateFolderResult =
|
||||
| { status: "ok"; path: string; name: string }
|
||||
| { status: "error"; message: string };
|
||||
|
||||
/**
|
||||
* Create one folder named `name` inside `parentPath` (defaults to the home
|
||||
* folder on the host). Reuses the same path-resolution and `~` expansion as
|
||||
* {@link listDirectory} so the two stay consistent about which directories
|
||||
* are reachable on the host.
|
||||
*/
|
||||
export async function createFolder(
|
||||
parentPath: string | undefined,
|
||||
rawName: string,
|
||||
): Promise<CreateFolderResult> {
|
||||
const name = rawName.trim();
|
||||
if (!name) {
|
||||
return { status: "error", message: "Enter a name for the new folder." };
|
||||
}
|
||||
// A single path segment only. Separators, "." and ".." would let a name
|
||||
// escape the current folder or overwrite a folder it does not name.
|
||||
if (name === "." || name === ".." || name.includes("/") || name.includes("\\") || name.includes("\0")) {
|
||||
return { status: "error", message: `Invalid folder name: ${name}` };
|
||||
}
|
||||
|
||||
const home = homedir();
|
||||
const parent = parentPath?.trim()
|
||||
? parentPath.trim() === "~"
|
||||
? home
|
||||
: parentPath.trim().startsWith("~/")
|
||||
? join(home, parentPath.trim().slice(2))
|
||||
: resolve(parentPath.trim())
|
||||
: home;
|
||||
|
||||
if (!isAbsolute(parent)) {
|
||||
return { status: "error", message: `Not a folder on this host: ${parent}` };
|
||||
}
|
||||
|
||||
try {
|
||||
const info = await stat(parent);
|
||||
if (!info.isDirectory()) {
|
||||
return { status: "error", message: `Not a folder on this host: ${parent}` };
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
|
||||
const fullPath = join(parent, name);
|
||||
try {
|
||||
// Not recursive: mkdir fails with EEXIST when a file or folder already
|
||||
// has that name, which is exactly the collision we want to report.
|
||||
await mkdir(fullPath);
|
||||
} catch (error) {
|
||||
const code = (error as { code?: string }).code;
|
||||
if (code === "EEXIST") {
|
||||
return {
|
||||
status: "error",
|
||||
message: `A file or folder named "${name}" already exists in ${parent}.`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: "error",
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
|
||||
return { status: "ok", path: fullPath, name };
|
||||
}
|
||||
|
||||
export type ListDirResult =
|
||||
| {
|
||||
status: "ok";
|
||||
|
||||
@@ -62,6 +62,25 @@ export const detectRpc = defineRpc({
|
||||
}),
|
||||
});
|
||||
|
||||
export const createFolderRpc = defineRpc({
|
||||
name: "picker.create_folder",
|
||||
input: z.object({
|
||||
parentPath: z.string().optional(),
|
||||
name: z.string(),
|
||||
}),
|
||||
output: z.discriminatedUnion("status", [
|
||||
z.object({
|
||||
status: z.literal("ok"),
|
||||
path: z.string(),
|
||||
name: z.string(),
|
||||
}),
|
||||
z.object({
|
||||
status: z.literal("error"),
|
||||
message: z.string(),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
|
||||
export const listDirRpc = defineRpc({
|
||||
name: "picker.list_dir",
|
||||
input: z.object({
|
||||
|
||||
Reference in new issue
Block a user