import { DownloadConsentPrompt, useDownloadConsent } from "./consent";
import { useState } from "react";
import { Pressable, Text, View } from "react-native";
import type { MountSource } from "../shared/spec";
import { invalidEnvNames, invalidMounts } from "../shared/spec";
import { errorMessage, formatMiB, homeOf, plural, resourceChangeLines, resourceChangeTitle, tildify } from "./format";
import { Figures, Frame } from "./frame";
import {
Actions,
Button,
ConfirmButton,
Field,
ListRow,
Note,
Pill,
Section,
Stat,
SwitchRow,
mono,
text,
tint,
toneColor,
type Theme,
} from "./kit";
import { pendingMounts, type PanelTabProps } from "./panel";
import { useRepoSettings, useSandboxAction, useStatus, type Description } from "./queries";
/** Offers Recreate where a change waits for it, or says what keeps it from running. */
function RecreateCallout({
data,
theme,
title,
detail,
}: {
data: Description;
theme: Theme;
title: string;
detail?: string | null;
}) {
const action = useSandboxAction();
const consent = useDownloadConsent();
const blocked = data.recreateBlockers.length > 0;
return (
{title}
{detail ? {detail} : null}
consent.request(() => action.mutate({ name: data.name, action: "recreate" }), data.template.configured === null)
}
/>
{blocked ? {`First: ${data.recreateBlockers.join("; ")}.`} : null}
{action.error ? (
{errorMessage(action.error)}
) : null}
);
}
export function ResourcesTab({ data, theme, compact, header }: PanelTabProps) {
const { repo, save, settings } = useRepoSettings(data.repoKey);
const status = useStatus();
const hostCpus = status.data?.hostCpus ?? null;
const defaults = settings.status === "ready" ? settings.values : null;
return (
{data.resourcesApplied === false ? (
) : null}
{repo && defaults && status.data ? (
// Keyed so a different repo or a saved value starts from a fresh draft.
save(patch)}
/>
) : null}
);
}
function ResourcesForm({
data,
theme,
hostMemoryMiB,
defaultMemoryMiB,
hostCpus,
defaultCpus,
saving,
onSave,
}: {
data: Description;
theme: Theme;
hostMemoryMiB: number;
defaultMemoryMiB: number;
hostCpus: number;
defaultCpus: number;
saving: boolean;
onSave(patch: { memoryGiB?: number; cpus?: number }): Promise;
}) {
const [memoryDraft, setMemoryDraft] = useState(data.repoMemoryGiB === null ? "" : String(data.repoMemoryGiB));
const [cpusDraft, setCpusDraft] = useState(data.repoCpus === null ? "" : String(data.repoCpus));
const [error, setError] = useState(null);
const maxGiB = Math.floor((hostMemoryMiB * 0.75) / 102.4) / 10;
const memory = memoryDraft.trim() === "" ? null : Number(memoryDraft);
const memoryInvalid = memory !== null && !(Number.isFinite(memory) && memory >= 0.5 && memory <= maxGiB);
const cpus = cpusDraft.trim() === "" ? null : Number(cpusDraft);
const cpusInvalid = cpus !== null && !(Number.isInteger(cpus) && cpus >= 0 && cpus <= hostCpus);
const changed = memory !== data.repoMemoryGiB || cpus !== data.repoCpus;
return (
{error ? (
{error}
) : null}
);
}
const MOUNT_SOURCE_LABELS: Record = {
settings: "All repositories",
repo: "This repository",
"gsd-path": "GSD Path",
member: "GSD Path member",
git: "Git metadata",
claude: "Shared Claude config",
agents: "Agents' shared homes",
};
export function MountsTab({ data, theme, compact, header }: PanelTabProps) {
const { repo, save, settings } = useRepoSettings(data.repoKey);
const home = homeOf(data.workspace);
const pending = pendingMounts(data);
const changes = data.status === null ? 0 : pending.added.length + pending.removed.length;
return (
{changes > 0 ? (
`+ ${tildify(path, home)}`),
...pending.removed.map((path) => `− ${tildify(path, home)}`),
].join(" ")}
/>
) : null}
}
/>
{data.mounts.map((mount) => (
{!mount.exists ? (
) : mount.applied === false ? (
) : null}
>
}
/>
))}
{data.removedMounts.map((path) => (
}
/>
))}
{repo && settings.status === "ready" ? (
item.endsWith(":ro") ? { title: item.slice(0, -3), pill: "Read-only" } : { title: item, pill: "Writable" }
}
validate={(item) => (invalidMounts([item]).length > 0 ? "Use an absolute or ~/ path" : null)}
empty="No mounts of its own. The ones above come from the plugin settings and the workspace."
onSave={(mounts) => save({ mounts })}
/>
) : null}
{data.gsdPath ? (
save({ gsdPath: gsdPath ?? undefined })}
/>
) : null}
);
}
const SESSIONS_VIEW: Record["sessions"], { tone: "success" | "neutral" | "warning"; label: string }> = {
listed: { tone: "success", label: "Counted in GSD Path" },
"not-listed": { tone: "neutral", label: "Listed at the next session" },
"no-config": { tone: "neutral", label: "GSD Path is not set up on this host" },
"invalid-config": { tone: "warning", label: "GSD Path's config is unreadable" },
};
function GsdPath({
gsdPath,
theme,
compact,
home,
ready,
onChoose,
}: {
gsdPath: NonNullable;
theme: Theme;
compact: boolean;
home: string | null;
ready: boolean;
onChoose(on: boolean | null): Promise;
}) {
const [error, setError] = useState(null);
const sessions = gsdPath.enabled ? SESSIONS_VIEW[gsdPath.sessions] : { tone: "neutral" as const, label: "Not counted" };
function choose(on: boolean | null) {
setError(null);
onChoose(on).catch((caught) => setError(errorMessage(caught)));
}
return (
}
gap={10}
>
choose(next)}
hint="Mounts ~/.gsd-path and the member checkouts, and lists this sandbox's sessions in GSD Path's usage."
/>
{gsdPath.repoChoice !== null ? (
{tildify(gsdPath.sessionsFolder, home)}}
aside={}
/>
Members · .project/MEMBERS.md
{gsdPath.members.length === 0 ? (
MEMBERS.md lists no members.
) : (
{gsdPath.members.map((member, index) => (
{tildify(member.path, home)}}
aside={
!gsdPath.enabled ? (
) : member.available ? (
) : (
)
}
/>
))}
)}
{gsdPath.enabled
? `The sandbox mounts ~/.gsd-path, also in the agent's home, and the member checkouts. New worktrees go under your ~/.gsd-path/projects. The sessions folder is listed in ${tildify(gsdPath.configPath, home)}, so GSD Path's usage and cost count this sandbox's agents. Mount changes apply when the sandbox is recreated.`
: "Turn it on to give the sandbox ~/.gsd-path and the member checkouts, and to count its sessions in GSD Path's usage."}
{error ? (
{error}
) : null}
);
}
export function VariablesTab({ data, theme, compact, header }: PanelTabProps) {
const { repo, save, settings } = useRepoSettings(data.repoKey);
const status = useStatus();
const envFile = status.data?.envFile ?? "the variables file";
const missing = data.env.filter((entry) => !entry.present && !entry.reserved).length;
return (
0
? `${plural(data.env.length, "variable")} · ${missing} without a value`
: plural(data.env.length, "variable")
}
title="Forwarded to sessions"
gap={4}
>
{data.env.length === 0 ? (
No variables are forwarded.
) : (
{data.env.map((entry, index) => (
) : entry.present ? (
) : (
)
}
/>
))}
)}
{missing > 0 ? {`Add the missing values to ${envFile}.`} : null}
{repo && settings.status === "ready" ? (
({ title: item })}
validate={(item) => (invalidEnvNames([item]).length > 0 ? "Not a variable you can forward" : null)}
empty="No variables of its own."
onSave={(envNames) => save({ envNames })}
/>
) : null}
);
}
/** A list setting edited an item at a time; every change saves. */
function ItemsEditor({
theme,
items,
label,
placeholder,
option,
describe,
validate,
empty,
onSave,
}: {
theme: Theme;
items: readonly string[];
label: string;
placeholder: string;
/** A flag an item carries as a suffix, such as `:ro`. */
option?: { label: string; suffix: string };
describe(item: string): { title: string; pill?: string };
validate(item: string): string | null;
empty: string;
onSave(items: string[]): Promise;
}) {
const [draft, setDraft] = useState("");
const [flag, setFlag] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState(null);
const entry = draft.trim() + (flag && option && !draft.trim().endsWith(option.suffix) ? option.suffix : "");
const invalid = draft.trim() === "" ? null : (validate(entry) ?? (items.includes(entry) ? "Already in the list" : null));
const accent = toneColor(theme, "accent");
function commit(next: string[]) {
setSaving(true);
setError(null);
onSave(next)
.catch((caught: unknown) => setError(errorMessage(caught)))
.finally(() => setSaving(false));
}
return (
{items.length === 0 ? (
{empty}
) : (
{items.map((item) => {
const view = describe(item);
return (
{view.title}
{view.pill ? {view.pill} : null}
commit(items.filter((other) => other !== item))}
style={(state) => ({
width: 26,
height: 26,
borderRadius: 13,
alignItems: "center",
justifyContent: "center",
backgroundColor: (state as { hovered?: boolean }).hovered
? tint(theme, toneColor(theme, "danger"), 0.18)
: "transparent",
})}
>
×
);
})}
)}
{
if (draft.trim() && !invalid) commit([...items, entry]);
}}
/>
{option ? (
setFlag(!flag)}
style={{
minHeight: 36,
justifyContent: "center",
paddingHorizontal: 12,
borderRadius: 999,
borderWidth: 1,
borderColor: flag ? accent : "transparent",
backgroundColor: flag ? tint(theme, accent, 0.14) : theme.colors.surface2,
}}
>
{`${flag ? "✓ " : ""}${option.label}`}
) : null}
{error ? (
{error}
) : null}
);
}