391 lines
14 KiB
TypeScript
391 lines
14 KiB
TypeScript
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>
|
|
);
|
|
}
|