Initial commit: file-browser-picker Paseo plugin

This commit is contained in:
nandi committed 2026-11-18 15:22:40 -08:00
commit 069e1d2082
15 files changed
+5599

No files matched your search

+390
View File
@@ -0,0 +1,390 @@
import type { PluginScreenProps } from "@getpaseo/plugin/client";
import { useRpc } from "@getpaseo/plugin/client";
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";
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 [browserOpen, setBrowserOpen] = useState(false);
const [currentPath, setCurrentPath] = useState<string | undefined>(undefined);
const [showHidden, setShowHidden] = useState(false);
const [filter, setFilter] = useState("");
const lastTrigger = useRef<string | null>(null);
const detection = useQuery({
queryKey: ["picker-detect", host.id],
queryFn: () => detect({}),
});
const pick = useMutation({
mutationFn: () => pickAndOpen({}),
});
const openDirect = useMutation({
mutationFn: (path: string) => openFolder({ path }),
});
const hasGuiPicker = Boolean(detection.data?.effectivePicker);
const showBrowser = browserOpen || (detection.isSuccess && !hasGuiPicker);
const directory = useQuery({
queryKey: ["picker-list-dir", host.id, currentPath ?? "__default__", showHidden],
queryFn: () => listDir({ path: currentPath, showHidden }),
enabled: showBrowser,
});
useEffect(() => {
setBrowserOpen(false);
setCurrentPath(undefined);
setFilter("");
lastTrigger.current = null;
}, [host.id]);
useEffect(() => {
if (!detection.isSuccess || !hasGuiPicker) return;
if (params.autostart !== "1") return;
const triggerKey = `${host.id}:${params.trigger ?? "__mount__"}`;
if (lastTrigger.current === triggerKey) return;
lastTrigger.current = triggerKey;
if (!pick.isPending) {
pick.mutate();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [detection.isSuccess, hasGuiPicker, host.id, params.autostart, params.trigger]);
const openResult = openDirect.data ?? pick.data;
const openingPending = pick.isPending || openDirect.isPending;
const openError = openDirect.error ?? pick.error;
useEffect(() => {
if (openResult?.status === "opened") {
navigation?.openWorkspace({ workspaceId: openResult.workspaceId });
}
}, [openResult, navigation]);
const filteredEntries = useMemo(() => {
if (directory.data?.status !== "ok") return [];
const needle = filter.trim().toLowerCase();
if (!needle) return directory.data.entries;
return directory.data.entries.filter((entry) => entry.name.toLowerCase().includes(needle));
}, [directory.data, filter]);
const styles = useMemo(
() => ({
screen: { flex: 1, backgroundColor: theme.colors.surface0 },
body: { padding: layout.compact ? 16 : 24, gap: 12 },
row: { flexDirection: "row" as const, flexWrap: "wrap" as const, alignItems: "center" as const, gap: 8 },
between: {
flexDirection: "row" as const,
alignItems: "center" as const,
justifyContent: "space-between" as const,
gap: 8,
},
heading: { color: theme.colors.foreground, fontSize: 18, fontWeight: "600" as const },
text: { color: theme.colors.foreground, fontSize: 14 },
mono: { color: theme.colors.foregroundMuted, fontSize: 12 },
muted: { color: theme.colors.foregroundMuted, fontSize: 13 },
error: { color: theme.colors.statusDanger, fontSize: 14 },
success: { color: theme.colors.statusSuccess, fontSize: 14 },
button: {
paddingVertical: 10,
paddingHorizontal: 14,
borderRadius: 8,
backgroundColor: theme.colors.accent,
alignSelf: "flex-start" as const,
opacity: openingPending ? 0.6 : 1,
},
buttonText: { color: theme.colors.accentForeground, fontSize: 14, fontWeight: "600" as const },
secondaryButton: {
paddingVertical: 8,
paddingHorizontal: 12,
borderRadius: 8,
borderWidth: 1,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface1,
alignSelf: "flex-start" as const,
},
secondaryButtonText: { color: theme.colors.foreground, fontSize: 13 },
card: {
borderWidth: 1,
borderColor: theme.colors.border,
borderRadius: 8,
padding: 12,
gap: 8,
backgroundColor: theme.colors.surface1,
},
input: {
borderWidth: 1,
borderColor: theme.colors.border,
borderRadius: 8,
paddingHorizontal: 10,
paddingVertical: 8,
color: theme.colors.foreground,
backgroundColor: theme.colors.surface0,
fontSize: 14,
},
folderRow: {
flexDirection: "row" as const,
alignItems: "center" as const,
justifyContent: "space-between" as const,
gap: 8,
paddingVertical: 8,
paddingHorizontal: 10,
borderRadius: 6,
borderWidth: 1,
borderColor: theme.colors.border,
backgroundColor: theme.colors.surface0,
},
folderMain: {
flex: 1,
flexDirection: "row" as const,
alignItems: "center" as const,
gap: 8,
},
gitBadge: {
color: theme.colors.foregroundMuted,
fontSize: 11,
paddingHorizontal: 6,
paddingVertical: 2,
borderRadius: 4,
borderWidth: 1,
borderColor: theme.colors.border,
},
}),
[theme, layout.compact, openingPending],
);
function navigateTo(nextPath: string) {
setFilter("");
setCurrentPath(nextPath);
}
return (
<ScrollView style={styles.screen} contentContainerStyle={styles.body}>
<Text style={styles.heading}>Open a project from {host.label}</Text>
{detection.isLoading ? (
<Text style={styles.muted}>Checking available file browsers on {host.label}…</Text>
) : null}
{hasGuiPicker ? (
<>
<Text style={styles.text}>
Pick a folder in the file browser on {host.label}. Paseo adds it as a project and opens a
workspace there, without showing the built-in project dialog.
</Text>
<View style={styles.row}>
<Pressable
accessibilityRole="button"
accessibilityLabel="Choose a folder in the file browser"
style={styles.button}
disabled={openingPending}
onPress={() => pick.mutate()}
>
<Text style={styles.buttonText}>
{pick.isPending ? "File browser is open…" : "Choose folder in file browser"}
</Text>
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel="Toggle host folder browser"
style={styles.secondaryButton}
onPress={() => setBrowserOpen((value) => !value)}
>
<Text style={styles.secondaryButtonText}>
{browserOpen ? "Hide folder browser" : "Browse host folders here"}
</Text>
</Pressable>
</View>
</>
) : detection.isSuccess ? (
<Text style={styles.muted}>
{host.label} is headless (no desktop file dialog is available). Browse folders on{" "}
{host.label} below and open one as a Paseo project.
</Text>
) : null}
{pick.isPending ? (
<Text style={styles.muted}>Choose a folder in the file browser window on {host.label}.</Text>
) : null}
{openResult?.status === "cancelled" ? (
<Text style={styles.muted}>Cancelled. Nothing was opened.</Text>
) : null}
{openResult?.status === "error" ? <Text style={styles.error}>{openResult.message}</Text> : null}
{openError ? (
<Text style={styles.error}>
{openError instanceof Error ? openError.message : "Failed to open the selected folder."}
</Text>
) : null}
{openResult?.status === "opened" ? (
<View style={styles.card}>
<Text style={styles.success}>Opened {openResult.name}</Text>
<Text style={styles.muted}>{openResult.path}</Text>
{navigation ? (
<Pressable
accessibilityRole="button"
accessibilityLabel="Go to the new workspace"
style={styles.secondaryButton}
onPress={() => navigation.openWorkspace({ workspaceId: openResult.workspaceId })}
>
<Text style={styles.secondaryButtonText}>Go to workspace</Text>
</Pressable>
) : (
<Text style={styles.muted}>Find the new workspace in the sidebar.</Text>
)}
</View>
) : null}
{showBrowser ? (
<View style={styles.card}>
<View style={styles.between}>
<Text style={styles.text} numberOfLines={1}>
{directory.data?.status === "ok"
? directory.data.path
: currentPath ?? detection.data?.defaultStartDir ?? "~"}
</Text>
{directory.data?.status === "ok" ? (
<Pressable
accessibilityRole="button"
accessibilityLabel="Open current folder"
style={styles.button}
disabled={openingPending}
onPress={() => {
if (directory.data?.status === "ok") {
openDirect.mutate(directory.data.path);
}
}}
>
<Text style={styles.buttonText}>
{openDirect.isPending ? "Opening…" : "Open this folder"}
</Text>
</Pressable>
) : null}
</View>
<View style={styles.row}>
<Pressable
accessibilityRole="button"
accessibilityLabel="Go to parent folder"
style={styles.secondaryButton}
disabled={directory.data?.status !== "ok" || !directory.data.parent}
onPress={() => {
if (directory.data?.status === "ok" && directory.data.parent) {
navigateTo(directory.data.parent);
}
}}
>
<Text style={styles.secondaryButtonText}>Up</Text>
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel="Go to home folder"
style={styles.secondaryButton}
disabled={directory.data?.status !== "ok"}
onPress={() => {
if (directory.data?.status === "ok") {
navigateTo(directory.data.home);
}
}}
>
<Text style={styles.secondaryButtonText}>Home</Text>
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel="Toggle hidden folders"
style={styles.secondaryButton}
onPress={() => setShowHidden((value) => !value)}
>
<Text style={styles.secondaryButtonText}>
{showHidden ? "Hide dotfiles" : "Show dotfiles"}
</Text>
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel="Refresh folder list"
style={styles.secondaryButton}
onPress={() => void directory.refetch()}
>
<Text style={styles.secondaryButtonText}>Refresh</Text>
</Pressable>
</View>
<TextInput
value={filter}
onChangeText={setFilter}
placeholder="Filter folders in this directory…"
placeholderTextColor={theme.colors.foregroundMuted}
style={styles.input}
/>
{directory.isLoading ? <Text style={styles.muted}>Reading folders…</Text> : null}
{directory.data?.status === "error" ? (
<Text style={styles.error}>{directory.data.message}</Text>
) : null}
{directory.data?.status === "ok" && filteredEntries.length === 0 ? (
<Text style={styles.muted}>No subfolders match here.</Text>
) : null}
{filteredEntries.map((entry) => (
<View key={entry.path} style={styles.folderRow}>
<Pressable
accessibilityRole="button"
accessibilityLabel={`Browse ${entry.name}`}
style={styles.folderMain}
onPress={() => navigateTo(entry.path)}
>
<Icon
name={entry.isGitRepo ? "FolderGit2" : "Folder"}
size={16}
color={entry.isGitRepo ? theme.colors.accent : theme.colors.foregroundMuted}
/>
<Text style={styles.text} numberOfLines={1}>
{entry.name}
</Text>
{entry.isGitRepo ? <Text style={styles.gitBadge}>git</Text> : null}
</Pressable>
<Pressable
accessibilityRole="button"
accessibilityLabel={`Open ${entry.name}`}
style={styles.secondaryButton}
disabled={openingPending}
onPress={() => openDirect.mutate(entry.path)}
>
<Text style={styles.secondaryButtonText}>Open</Text>
</Pressable>
</View>
))}
</View>
) : null}
{detection.data ? (
<View style={styles.card}>
<Text style={styles.mono}>
Host: {detection.data.platform}
{detection.data.desktop ? ` · ${detection.data.desktop}` : ""}
{detection.data.effectivePicker
? ` · picker: ${detection.data.effectivePicker}`
: " · headless host browser"}
</Text>
{detection.data.pickers.map((picker) => (
<Text key={picker.id} style={styles.mono}>
{picker.available ? "•" : "○"} {picker.label}: {picker.detail}
</Text>
))}
<Text style={styles.mono}>Change the picker or the start folder in plugin settings.</Text>
</View>
) : null}
</ScrollView>
);
}
+180
View File
@@ -0,0 +1,180 @@
import type { PluginSurfaceProps } from "@getpaseo/plugin/client";
import { useSettings } from "@getpaseo/plugin/client";
import {
ScrollView,
SettingsAction,
SettingsCard,
SettingsInput,
SettingsRow,
SettingsSection,
SettingsSelect,
} from "@getpaseo/plugin/client/ui";
import { useEffect, useMemo, useState } from "react";
import { Text, View } from "react-native";
import { defaultPickerSettings, pickerSettings, type PickerSettings } from "../shared/settings";
const PICKER_OPTIONS: readonly { label: string; value: PickerSettings["picker"] }[] = [
{ label: "Automatic (host default)", value: "auto" },
{ label: "Desktop portal (Linux XDG)", value: "portal" },
{ label: "KDE dialog (kdialog)", value: "kdialog" },
{ label: "GTK dialog (zenity)", value: "zenity" },
{ label: "GTK dialog (yad)", value: "yad" },
{ label: "macOS Finder (osascript)", value: "osascript" },
{ label: "Windows FolderBrowserDialog", value: "powershell" },
{ label: "Custom command", value: "custom" },
];
export function PickerSettingsScreen({ theme, layout, host }: PluginSurfaceProps) {
const settings = useSettings(pickerSettings);
const [draft, setDraft] = useState<PickerSettings>(defaultPickerSettings);
const [statusMessage, setStatusMessage] = useState<string | null>(null);
const readyRevision = settings.status === "ready" ? settings.revision : null;
const readyValues = settings.status === "ready" ? settings.values : null;
useEffect(() => {
if (readyValues) {
setDraft(readyValues);
}
}, [readyRevision, readyValues]);
const styles = useMemo(
() => ({
screen: { flex: 1, backgroundColor: theme.colors.surface0 },
body: { padding: layout.compact ? 16 : 24, gap: 16 },
text: { color: theme.colors.foreground, fontSize: 14 },
muted: { color: theme.colors.foregroundMuted, fontSize: 13 },
error: { color: theme.colors.statusDanger, fontSize: 13 },
success: { color: theme.colors.statusSuccess, fontSize: 13 },
}),
[theme, layout.compact],
);
if (settings.status === "loading") {
return (
<View style={[styles.screen, styles.body]}>
<Text style={styles.muted}>Loading picker settings for {host.label}…</Text>
</View>
);
}
if (settings.status === "error") {
return (
<View style={[styles.screen, styles.body]}>
<Text style={styles.error}>Could not load settings: {settings.error}</Text>
</View>
);
}
const isInvalid = settings.status === "invalid";
const revision = settings.revision;
const isDirty =
settings.status === "ready" &&
(draft.picker !== settings.values.picker ||
draft.startDir !== settings.values.startDir ||
draft.prompt !== settings.values.prompt ||
draft.customCommand !== settings.values.customCommand);
async function saveDraft(next: PickerSettings) {
setStatusMessage(null);
const ok = await settings.save(next, revision);
if (ok) {
setStatusMessage("Saved.");
}
}
async function handleReset() {
setStatusMessage(null);
const ok = await settings.reset();
if (ok) {
setDraft(defaultPickerSettings);
setStatusMessage("Reset to defaults.");
}
}
return (
<ScrollView style={styles.screen} contentContainerStyle={styles.body}>
<SettingsSection
title="File browser picker"
info={<Text style={styles.muted}>Host settings for {host.label}.</Text>}
>
{isInvalid ? (
<SettingsCard>
<SettingsRow label="Invalid saved settings" error={settings.error} />
</SettingsCard>
) : null}
<SettingsCard key={revision}>
<SettingsSelect
label="Picker program"
hint="Automatic uses your desktop's native folder dialog."
value={draft.picker}
disabled={settings.saving}
options={PICKER_OPTIONS}
onValueChange={(picker) => {
const next = { ...draft, picker };
setDraft(next);
void saveDraft(next);
}}
/>
<SettingsInput
label="Start folder"
hint="Folder the file browser opens in. Leave blank for the host home folder."
initialValue={draft.startDir}
placeholder="~"
disabled={settings.saving}
onChangeText={(startDir) => {
setStatusMessage(null);
setDraft((prev) => ({ ...prev, startDir }));
}}
/>
<SettingsInput
label="Dialog title"
hint="Title shown in the OS folder picker window."
initialValue={draft.prompt}
placeholder={defaultPickerSettings.prompt}
disabled={settings.saving}
onChangeText={(prompt) => {
setStatusMessage(null);
setDraft((prev) => ({ ...prev, prompt }));
}}
/>
<SettingsInput
label="Custom command"
hint="Used when Picker program is Custom command. Print the selected folder path to stdout; {dir} expands to the start folder."
initialValue={draft.customCommand}
placeholder='kdialog --getexistingdirectory "{dir}"'
disabled={settings.saving}
onChangeText={(customCommand) => {
setStatusMessage(null);
setDraft((prev) => ({ ...prev, customCommand }));
}}
/>
</SettingsCard>
<SettingsCard>
<SettingsAction
label="Save text changes"
hint={isDirty ? "You have unsaved changes." : "Applies start folder, dialog title, and custom command."}
actionLabel={settings.saving ? "Saving…" : "Save"}
disabled={settings.saving || (!isDirty && !isInvalid)}
onPress={() => void saveDraft(draft)}
/>
<SettingsAction
label="Reset settings"
hint="Restore default picker settings on this host."
actionLabel="Reset"
disabled={settings.saving}
onPress={() => void handleReset()}
/>
</SettingsCard>
{settings.saveError ? <Text style={styles.error}>{settings.saveError}</Text> : null}
{statusMessage ? <Text style={styles.success}>{statusMessage}</Text> : null}
</SettingsSection>
</ScrollView>
);
}