Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
import { useEffect, useState } from "react";
import { portalToBody } from "../lib/portal-visibility";
import { useTranslation } from "react-i18next";
import { useBlockingOverlay } from "../lib/blocking-overlay";
import type { AgentInstructionFile } from "@pi-desktop/shared";
import { api } from "../lib/api";
import { OverlayBackdrop, Button, Textarea, TooltipButton } from "./ui";
import { IconClose } from "./icons";
export function ProjectInstructionsDialog({
project,
onClose,
onSaved,
onError,
}: {
project: { name: string; path: string; groupId?: string; legacy?: boolean };
onClose: () => void;
onSaved: () => void;
onError: (error: unknown) => void;
}) {
useBlockingOverlay();
const { t } = useTranslation();
const [file, setFile] = useState<AgentInstructionFile | null>(null);
const [draft, setDraft] = useState("");
const [saving, setSaving] = useState(false);
useEffect(() => {
let cancelled = false;
const load = project.groupId && !project.legacy
? api.getProjectGroupInstructions(project.groupId).then((result) => ({
project: {
scope: "project" as const,
path: "ChatGPT Project instructions",
content: result.content,
exists: true,
},
}))
: api.getAgentInstructions(project.path);
void load.then((result) => {
if (cancelled || !result.project) return;
setFile(result.project);
setDraft(result.project.content);
}).catch((error) => {
if (!cancelled) onError(error);
});
return () => {
cancelled = true;
};
}, [project.groupId, project.legacy, project.path]);
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape" && !saving) onClose();
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [onClose, saving]);
const save = async () => {
setSaving(true);
try {
if (project.groupId && !project.legacy) {
const result = await api.saveProjectGroupInstructions(project.groupId, draft);
setFile((current) => current ? { ...current, content: result.content, exists: true } : current);
} else {
const result = await api.saveAgentInstructions("project", draft, project.path);
setFile(result.file);
}
onSaved();
} catch (error) {
onError(error);
} finally {
setSaving(false);
}
};
const dirty = file !== null && draft !== file.content;
const dialog = (
<OverlayBackdrop
className="overlay project-instructions-dialog-overlay"
role="presentation"
onClick={() => {
if (!saving) onClose();
}}
>
<div
className="dialog project-instructions-dialog"
role="dialog"
aria-modal="true"
aria-labelledby="project-instructions-dialog-title"
onClick={(event) => event.stopPropagation()}
>
<div className="project-instructions-dialog-head">
<div>
<h3 id="project-instructions-dialog-title" className="project-instructions-dialog-title">
{t("project.editInstructions")}
</h3>
<div className="project-instructions-dialog-project">{project.name}</div>
</div>
<TooltipButton
type="button"
className="project-instructions-dialog-close"
tooltip={t("settings.cancel")}
ariaLabel={t("settings.cancel")}
disabled={saving}
onClick={onClose}
>
<IconClose size={16} />
</TooltipButton>
</div>
<div className="project-instructions-dialog-path">{file?.path ?? ""}</div>
<Textarea
className="settings-instruction-editor project-instructions-dialog-editor"
value={draft}
onChange={(event) => setDraft(event.target.value)}
aria-label={t("project.editInstructions")}
disabled={!file || saving}
/>
<div className="project-instructions-dialog-actions">
<Button variant="ghost" disabled={saving} onClick={onClose}>
{t("settings.cancel")}
</Button>
<Button variant="primary" disabled={!dirty || saving} onClick={() => void save()}>
{saving ? t("settings.saving") : t("settings.instructionsSave")}
</Button>
</div>
</div>
</OverlayBackdrop>
);
return portalToBody(dialog);
}