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 { 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(undefined); const [showHidden, setShowHidden] = useState(false); const [filter, setFilter] = useState(""); const [newFolderName, setNewFolderName] = useState(""); const [createMessage, setCreateMessage] = useState(null); const [createError, setCreateError] = useState(null); const lastTrigger = useRef(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 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); 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(""); setNewFolderName(""); setCreateMessage(null); setCreateError(null); 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(""); setCreateMessage(null); setCreateError(null); setCurrentPath(nextPath); } const canCreateFolder = directory.data?.status === "ok" && Boolean(newFolderName.trim()) && !createNewFolder.isPending; return ( Open a project from {host.label} {detection.isLoading ? ( Checking available file browsers on {host.label}… ) : null} {hasGuiPicker ? ( <> 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. pick.mutate()} > {pick.isPending ? "File browser is open…" : "Choose folder in file browser"} setBrowserOpen((value) => !value)} > {browserOpen ? "Hide folder browser" : "Browse host folders here"} ) : detection.isSuccess ? ( {host.label} is headless (no desktop file dialog is available). Browse folders on{" "} {host.label} below and open one as a Paseo project. ) : null} {pick.isPending ? ( Choose a folder in the file browser window on {host.label}. ) : null} {openResult?.status === "cancelled" ? ( Cancelled. Nothing was opened. ) : null} {openResult?.status === "error" ? {openResult.message} : null} {openError ? ( {openError instanceof Error ? openError.message : "Failed to open the selected folder."} ) : null} {openResult?.status === "opened" ? ( Opened {openResult.name} {openResult.path} {navigation ? ( navigation.openWorkspace({ workspaceId: openResult.workspaceId })} > Go to workspace ) : ( Find the new workspace in the sidebar. )} ) : null} {showBrowser ? ( {directory.data?.status === "ok" ? directory.data.path : currentPath ?? detection.data?.defaultStartDir ?? "~"} {directory.data?.status === "ok" ? ( { if (directory.data?.status === "ok") { openDirect.mutate(directory.data.path); } }} > {openDirect.isPending ? "Opening…" : "Open this folder"} ) : null} { if (directory.data?.status === "ok" && directory.data.parent) { navigateTo(directory.data.parent); } }} > Up { if (directory.data?.status === "ok") { navigateTo(directory.data.home); } }} > Home setShowHidden((value) => !value)} > {showHidden ? "Hide dotfiles" : "Show dotfiles"} void directory.refetch()} > Refresh {directory.isLoading ? Reading folders… : null} {directory.data?.status === "error" ? ( {directory.data.message} ) : null} {directory.data?.status === "ok" && filteredEntries.length === 0 ? ( No subfolders match here. ) : null} { setNewFolderName(value); setCreateMessage(null); setCreateError(null); }} placeholder="New folder name…" placeholderTextColor={theme.colors.foregroundMuted} style={[styles.input, { flex: 1 }]} /> createNewFolder.mutate(newFolderName.trim())} > {createNewFolder.isPending ? "Creating…" : "New folder"} {createMessage ? {createMessage} : null} {createError ? {createError} : null} {filteredEntries.map((entry) => ( navigateTo(entry.path)} > {entry.name} {entry.isGitRepo ? git : null} openDirect.mutate(entry.path)} > Open ))} ) : null} {detection.data ? ( Host: {detection.data.platform} {detection.data.desktop ? ` · ${detection.data.desktop}` : ""} {detection.data.effectivePicker ? ` · picker: ${detection.data.effectivePicker}` : " · headless host browser"} {detection.data.pickers.map((picker) => ( {picker.available ? "•" : "○"} {picker.label}: {picker.detail} ))} Change the picker or the start folder in plugin settings. ) : null} ); }