import { useCallback, useEffect, useState } from "react"; import { Image, Pressable, Switch, Text, View } from "react-native"; import { useRpc, useSettings, useTranslation, type PluginSurfaceProps } from "@getpaseo/plugin/client"; import { Icon, SettingsAction, SettingsCard, SettingsInput, SettingsRow, SettingsSection } from "@getpaseo/plugin/client/ui"; import { appsRpc, mergeEntries, openWithSettings, type App, type Entry } from "../shared/open-with"; import { messages } from "./messages"; type Props = PluginSurfaceProps & { onSaved(): void }; export function SettingsScreen(props: Props) { const state = useSettings(openWithSettings); const { t } = useTranslation(messages); const c = props.theme.colors; if (state.status !== "ready") { return ( {state.status === "loading" ? t("loading") : state.error} {state.status !== "loading" && ( void state.reload()} /> {state.status === "invalid" && ( void state.reset()} /> )} )} ); } return ( state.save({ entries }, state.revision)} /> ); } function Editor({ theme, stored, saving, saveError, save, onSaved, }: Props & { stored: Entry[]; saving: boolean; saveError: string | null; save(entries: Entry[]): Promise; }) { const { t } = useTranslation(messages); const c = theme.colors; const listApps = useRpc(appsRpc); const [draft, setDraft] = useState(() => mergeEntries(stored)); const [apps, setApps] = useState>(new Map()); const [editing, setEditing] = useState(null); const [error, setError] = useState(""); const [saved, setSaved] = useState(false); const loadApps = useCallback(() => { void listApps({}) .then((result) => setApps(new Map(result.apps.map((app) => [app.id, app])))) .catch(() => {}); }, [listApps]); useEffect(loadApps, [loadApps]); const change = (next: Entry[]) => { setDraft(next); setSaved(false); setError(""); }; const patch = (id: string, fields: Partial) => change(draft.map((entry) => (entry.id === id ? { ...entry, ...fields } : entry))); const move = (index: number, by: number) => { const next = [...draft]; const [entry] = next.splice(index, 1); next.splice(index + by, 0, entry!); change(next); }; const add = () => { const id = `custom-${Date.now().toString(36)}`; change([...draft, { id, preset: "", name: "", icon: "", command: "", args: "", enabled: true }]); setEditing(id); }; const saveNow = async () => { if (draft.some((entry) => !entry.preset && !entry.command.trim())) { setError(t("commandRequired")); return; } if (await save(draft)) { setSaved(true); onSaved(); } }; const iconButton = (name: string, label: string, onPress: () => void, disabled = false) => ( ); return ( {draft.map((entry, index) => { const app = apps.get(entry.id); const name = entry.name || app?.defaults.name || entry.command || t("newApp"); const open = editing === entry.id; return ( setEditing(open ? null : entry.id)} style={{ flexDirection: "row", alignItems: "center", gap: 10, paddingHorizontal: 16, paddingVertical: 10 }} > {app?.image ? ( ) : ( )} {name} {app && ( {app.command ?? t("notFound")} )} {iconButton("ChevronUp", t("moveUp"), () => move(index, -1), index === 0)} {iconButton("ChevronDown", t("moveDown"), () => move(index, 1), index === draft.length - 1)} patch(entry.id, { enabled })} /> {open && ( patch(entry.id, fields)} remove={() => change(draft.filter((e) => e.id !== entry.id))} /> )} ); })} {(error || saveError) && {error || saveError}} void saveNow()} /> ); } function EntryFields({ entry, app, patch, remove, }: { entry: Entry; app: App | undefined; patch(fields: Partial): void; remove(): void; }) { const { t } = useTranslation(messages); const defaults = app?.defaults; return ( patch({ name })} /> patch({ command })} /> patch({ args })} /> patch({ icon })} /> {entry.preset ? ( patch({ name: "", command: "", args: "", icon: "" })} /> ) : ( )} ); }