import { useRpc } from "@getpaseo/plugin/client"; import { useMutation } from "@tanstack/react-query"; import { useState } from "react"; import { View } from "react-native"; import { publishPortRpc, unpublishPortRpc } from "../shared/rpc"; import { errorMessage, plural } from "./format"; import { Empty, Frame } from "./frame"; import { Actions, Button, Field, ListRow, Note, Pill, Section, SwitchRow, ago, useNow } from "./kit"; import type { PanelTabProps } from "./panel"; import { useAwaitOperation, useInvalidateAll, useRepoSettings } from "./queries"; const validPort = (value: number) => Number.isInteger(value) && value > 0 && value < 65536; export function ServicesTab({ data, theme, compact, header }: PanelTabProps) { const now = useNow(5_000); const invalidate = useInvalidateAll(); const publishRpc = useRpc(publishPortRpc); const unpublishRpc = useRpc(unpublishPortRpc); const awaitOperation = useAwaitOperation(); const publish = useMutation({ mutationFn: async (input: { guestPort: number; allInterfaces: boolean; hostPort: number | null }) => awaitOperation(await publishRpc({ name: data.name, ...input })), onSettled: invalidate, }); const unpublish = useMutation({ mutationFn: async (guestPort: number) => awaitOperation(await unpublishRpc({ name: data.name, guestPort })), onSettled: invalidate, }); const { repo, save } = useRepoSettings(data.repoKey); const [allInterfaces, setAllInterfaces] = useState(false); const [guestPort, setGuestPort] = useState(""); const [hostPort, setHostPort] = useState(""); const [repoError, setRepoError] = useState(null); const { services } = data; const running = data.status === "running"; const published = new Map(services.publications.map((publication) => [publication.guestPort, publication])); const parsedGuest = Number(guestPort); const parsedHost = hostPort.trim() === "" ? null : Number(hostPort); const hostInvalid = parsedHost !== null && !validPort(parsedHost); const guestInvalid = guestPort.trim() !== "" && !validPort(parsedGuest); const publishLabel = running ? "Publish" : "Publish and start the VM"; function toggleAuto(port: number, enabled: boolean) { if (!repo) return; const autoPublish = enabled ? [...new Set([...repo.autoPublish, port])] : repo.autoPublish.filter((entry) => entry !== port); setRepoError(null); save({ autoPublish }).catch((error: unknown) => setRepoError(errorMessage(error))); } const frame = { theme, compact, title: "Services", description: "Ports listening in the VM, and the ones published on this host.", ...header, notes: [ "The guest reports its listening ports while an agent or shell runs. Services that listen only on the VM's loopback can be published too.", "A published port keeps its host port across VM restarts. A port set to always publish is published in every sandbox of the repository at its next launch.", "When a fixed host port is taken as the VM starts, the port shows as not published: pick another, or leave it empty.", ], }; if (!data.status) { return ( ); } return (
{services.listeners.length === 0 ? ( {services.snapshotAt === null ? "The next agent or shell starts reporting the VM's listening ports." : "Nothing listens in the VM."} ) : ( {services.listeners.map((listener, index) => { const publication = published.get(listener.port); return ( ) : (
{services.publications.length === 0 ? ( No port is published. Publish one above, or by its number below. ) : ( {services.publications.map((publication, index) => { const auto = repo?.autoPublish.includes(publication.guestPort) ?? false; return ( {publication.hostPort === null ? : null} {publication.allInterfaces ? ( ) : ( )} {auto ? : null} } aside={ <> {repo ? (
); }