import { Link, useNavigate } from "@tanstack/react-router"; import { lazy, Suspense, useState } from "react"; import { commitFile } from "~/fns/git-write"; import { useAction } from "~/hooks/use-action"; import { Button } from "~/ui/Button"; import { CheckboxField, TextField } from "~/ui/Field"; import { ErrorBanner } from "~/ui/Callout"; import { Panel } from "~/ui/Panel"; import { buttonClass } from "~/ui/Button"; const CodeEditor = lazy(() => import("./CodeEditor").then((module) => ({ default: module.CodeEditor }))); type Props = { owner: string; repo: string; branch: string; headSha: string | null; initialPath: string; previousPath: string | null; initialContent: string; allowDelete: boolean; }; export function FileEditor({ owner, repo, branch, headSha, initialPath, previousPath, initialContent, allowDelete, }: Props) { const navigate = useNavigate(); const [path, setPath] = useState(initialPath); const [content, setContent] = useState(initialContent); const [newBranch, setNewBranch] = useState(false); const save = useAction(commitFile, { invalidate: false }); const verb = previousPath ? "Update" : "Create"; async function commit(element: HTMLFormElement, deleting: boolean) { const form = new FormData(element); const message = String(form.get("message") ?? "").trim() || `${deleting ? "Delete" : verb} ${path}`; const branchName = String(form.get("branchName") ?? "").trim(); const result = await save.run({ data: { owner, repo, branch, path: deleting && previousPath ? previousPath : path, previousPath: deleting ? null : previousPath, content: deleting ? null : content, message, expectedHeadSha: headSha, newBranch: newBranch && branchName ? branchName : null, }, }); if (!result) return; if (newBranch && branchName) { await navigate({ to: "/$owner/$repo/compare/$", params: { owner, repo, _splat: `${branch}...${result.branch}` }, }); } else if (deleting) { await navigate({ to: "/$owner/$repo/tree/$", params: { owner, repo, _splat: result.branch } }); } else { await navigate({ to: "/$owner/$repo/blob/$", params: { owner, repo, _splat: `${result.branch}/${path}` } }); } } return (
); }