import { useMemo, useState } from 'react'; import { Alert, StyleSheet, Switch, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import { Screen } from '@/components/layout/screen'; import { CodeEditor, languageForPath } from '@/components/git/code-editor'; import { AppText } from '@/components/ui/app-text'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { ChipSelect } from '@/components/ui/chip-select'; import { Field } from '@/components/ui/field'; import { TextArea } from '@/components/ui/text-area'; import { ErrorState, LoadingView } from '@/components/ui/state-views'; import { apiErrorMessage } from '@/core/api/error-text'; import type { Branch, ContentsEntry, Repository } from '@/core/api/types'; import { encodeBase64Utf8 } from '@/core/utils/base64'; import { flattenPages } from '@/core/utils/infinite'; import { useCreatePull } from '@/features/issues/queries'; import { useRepository } from '@/features/repositories/queries'; import { decodeBase64, useBranches, useCommitFile, useFile } from '@/features/repository/queries'; import { useTheme } from '@/theme/theme-provider'; import { spacing } from '@/theme/tokens'; import type { RepositoryStackParamList } from '@/navigation/types'; import type { ScreenProps } from '@/navigation/typing'; type Props = ScreenProps; /** * Quick Edit: edit a file in a monospace editor and commit it through the * Contents API. Supports committing to a new branch (which also handles * protected branches) and optionally opening a pull request afterwards. */ export function FileEditScreen({ navigation, route }: Props) { const { t } = useTranslation(); const { owner, name, path, ref } = route.params; const fileQuery = useFile(owner, name, path, ref); const refetchFile = fileQuery.refetch; const repoQuery = useRepository(owner, name); const branchesQuery = useBranches(owner, name, ''); const branches = flattenPages(branchesQuery.data); const entry = fileQuery.data; if (fileQuery.isPending) return ; if (fileQuery.isError) { return ( void refetchFile()} /> ); } if (!entry) { return ( void refetchFile()} /> ); } return ( navigation.goBack()} /> ); } interface FormProps { owner: string; name: string; path: string; initialRef?: string; entry: ContentsEntry; repo?: Repository; branches: Branch[]; onDone: () => void; } function FileEditForm({ owner, name, path, initialRef, entry, repo, branches, onDone }: FormProps) { const { t } = useTranslation(); const { colors } = useTheme(); const commitFile = useCommitFile(owner, name); const createPull = useCreatePull(owner, name); // Initialised once per file (the parent remounts this component via `key`). const [original] = useState(() => decodeBase64(entry.content ?? '')); const [draft, setDraft] = useState(original); const [message, setMessage] = useState(''); const [branch, setBranch] = useState(initialRef ?? null); const [useNewBranch, setUseNewBranch] = useState(false); const [newBranch, setNewBranch] = useState(''); const [createPr, setCreatePr] = useState(false); const [prTitle, setPrTitle] = useState(''); const [submitted, setSubmitted] = useState(false); const targetBranch = branch ?? repo?.default_branch ?? null; const branchOptions = useMemo( () => branches.map((b) => ({ id: b.name, label: b.name })), [branches], ); const currentBranch = branches.find((b) => b.name === targetBranch); const branchProtected = Boolean(currentBranch?.protected && currentBranch?.user_can_push !== true); const effectiveNewBranch = useNewBranch || branchProtected; const dirty = draft !== original; const canSubmit = dirty && message.trim().length > 0 && (effectiveNewBranch ? newBranch.trim().length > 0 : Boolean(targetBranch)) && !commitFile.isPending; const submit = () => { setSubmitted(true); if (!canSubmit || !entry.sha) return; const newBranchName = effectiveNewBranch ? newBranch.trim() : undefined; commitFile.mutate( { path, content: encodeBase64Utf8(draft), message: message.trim(), branch: targetBranch ?? undefined, newBranch: newBranchName, sha: entry.sha, }, { onSuccess: async () => { if (createPr && newBranchName && targetBranch && prTitle.trim().length > 0) { try { await createPull.mutateAsync({ title: prTitle.trim(), head: newBranchName, base: targetBranch, body: message.trim(), }); } catch { // The commit succeeded; a failed PR can be opened from the web UI. } } Alert.alert(t('edit.commitSuccessTitle'), t('edit.commitSuccessBody')); onDone(); }, }, ); }; return ( {path} {dirty ? (