181 lines
6.3 KiB
TypeScript
181 lines
6.3 KiB
TypeScript
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>
|
|
);
|
|
}
|