import { Component, useCallback, useMemo, useState, type ReactNode } from "react";
import { Pressable, ScrollView, Text, View } from "react-native";
import { useMutation } from "@tanstack/react-query";
import {
useHosts,
useTerminals,
useTerminal,
usePaseo,
type TerminalRef,
type PluginSidebarViewProps,
} from "@getpaseo/plugin/client";
import { Heading } from "./rows";
class TerminalError extends Component<{ children: ReactNode }, { error: Error | null }> {
state = { error: null as Error | null };
static getDerivedStateFromError(error: Error) {
return { error };
}
render() {
return this.state.error ? {this.state.error.message} : this.props.children;
}
}
function TerminalRow({
reference,
view,
}: {
reference: TerminalRef;
view: PluginSidebarViewProps;
}) {
const terminal = useTerminal(reference);
const paseo = usePaseo();
const focus = useCallback(() => paseo.terminals.ref(reference).focus(), [paseo, reference]);
const close = useMutation({ mutationFn: () => paseo.terminals.ref(reference).kill() });
const kill = useCallback(() => close.mutate(), [close]);
const style = useMemo(
() => ({ color: view.theme.colors.foreground, fontSize: 12, padding: 12 }),
[view.theme],
);
if (!terminal) return null;
return (
{terminal.title}
{terminal.isScript ? " (script)" : ""}
Close terminal
{close.error ? {close.error.message} : null}
);
}
function HostTerminals({ hostId, view }: { hostId: string; view: PluginSidebarViewProps }) {
const terminals = useTerminals({ hosts: [hostId], orderBy: "createdAt" });
return (
{terminals.length ? (
terminals.map((reference) => (
))
) : (
)}
);
}
function HostButton({
hostId,
label,
select,
view,
}: {
hostId: string;
label: string;
select: (id: string) => void;
view: PluginSidebarViewProps;
}) {
const choose = useCallback(() => select(hostId), [select, hostId]);
return (
);
}
export function Terminals(view: PluginSidebarViewProps) {
const hosts = useHosts();
const [chosen, select] = useState(null);
const selected = chosen;
return (
{hosts.map((host) => (
))}
{selected ? (
) : (
)}
);
}