The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg, T, useGT, useMessages } from "gt-react-native"; |
| 2 | import { BranchNamingMode, type ServerSettingsPatch } from "@t3tools/contracts"; |
| 3 | import { useEffect, useEffectEvent, useRef } from "react"; |
| 4 | import { View } from "react-native"; |
| 5 | |
| 6 | import { AppText as Text, AppTextInput } from "../../../components/ui/AppText"; |
| 7 | import { ListChoiceRow as SettingsChoiceRow } from "../../../components/ui/ListChoiceRow"; |
| 8 | import { SettingsSection } from "./SettingsSection"; |
| 9 | |
| 10 | const MODES = { |
| 11 | static: { |
| 12 | label: msg("Static prefix"), |
| 13 | description: msg("Add your prefix to the generated branch name."), |
| 14 | }, |
| 15 | semantic: { |
| 16 | label: msg("Semantic prefix"), |
| 17 | description: msg("Let the model choose {examples}, or another prefix.", { |
| 18 | examples: "feat/, fix/, refactor/", |
| 19 | }), |
| 20 | }, |
| 21 | custom: { |
| 22 | label: msg("Custom instructions"), |
| 23 | description: msg("Generate the complete name with no added prefix or suffix."), |
| 24 | }, |
| 25 | } satisfies Record<BranchNamingMode, { label: string; description: string }>; |
| 26 | |
| 27 | export function BranchNamingSettings(props: { |
| 28 | mode: BranchNamingMode | null; |
| 29 | prefix: string | null; |
| 30 | instructions: string | null; |
| 31 | /** Per setting, since a policy can lock one and leave the others to the user. */ |
| 32 | disabled: Readonly<Record<"mode" | "prefix" | "instructions", boolean>>; |
| 33 | onChange: (patch: ServerSettingsPatch) => void; |
| 34 | }) { |
| 35 | const gt = useGT(); |
| 36 | const m = useMessages(); |
| 37 | // Save on exit too: Android Back hides the keyboard without ending editing. |
| 38 | type Field = "branchNamePrefix" | "branchNameInstructions"; |
| 39 | const drafts = useRef<Partial<Record<Field, { text: string; over: string | null }>>>({}); |
| 40 | const savedValue = (field: Field) => |
| 41 | field === "branchNamePrefix" ? props.prefix : props.instructions; |
| 42 | const edit = (field: Field, text: string) => { |
| 43 | drafts.current[field] = { text, over: savedValue(field) }; |
| 44 | }; |
| 45 | // Drop a draft typed over an older saved value: the updated value has already remounted its input. |
| 46 | const take = (field: Field): ServerSettingsPatch => { |
| 47 | const draft = drafts.current[field]; |
| 48 | delete drafts.current[field]; |
| 49 | const saved = savedValue(field); |
| 50 | // Save even while disabled; the route queues behind pending writes and checks access. |
| 51 | if (draft === undefined || draft.over !== saved) return {}; |
| 52 | const value = draft.text.trim(); |
| 53 | return saved === null || value !== saved ? { [field]: value } : {}; |
| 54 | }; |
| 55 | const send = (patch: ServerSettingsPatch) => { |
| 56 | if (Object.keys(patch).length > 0) props.onChange(patch); |
| 57 | }; |
| 58 | // One patch, because the screen drops writes while another is in flight. |
| 59 | const saveDrafts = useEffectEvent(() => |
| 60 | send({ ...take("branchNamePrefix"), ...take("branchNameInstructions") }), |
| 61 | ); |
| 62 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- saves unsent branch naming drafts to server settings when the section unmounts |
| 63 | useEffect(() => () => saveDrafts(), []); |
| 64 | return ( |
| 65 | <SettingsSection |
| 66 | title={gt("Worktree branch naming")} |
| 67 | trailing={ |
| 68 | props.mode === null ? ( |
| 69 | <Text className="text-xs text-foreground-muted"> |
| 70 | <T>Mixed</T> |
| 71 | </Text> |
| 72 | ) : null |
| 73 | } |
| 74 | > |
| 75 | {BranchNamingMode.literals.map((mode, index) => ( |
| 76 | <SettingsChoiceRow |
| 77 | key={mode} |
| 78 | label={m(MODES[mode].label)} |
| 79 | description={m(MODES[mode].description)} |
| 80 | selected={props.mode === mode} |
| 81 | separated={index > 0} |
| 82 | disabled={props.disabled.mode} |
| 83 | onPress={() => props.onChange({ branchNamingMode: mode })} |
| 84 | /> |
| 85 | ))} |
| 86 | {props.mode === "static" ? ( |
| 87 | <View className="gap-2 px-4 py-3"> |
| 88 | <Text className="text-sm text-foreground-muted"> |
| 89 | {gt("Use {short} or {slash} for {example}. Leave empty for no prefix.", { |
| 90 | short: "t3", |
| 91 | slash: "t3/", |
| 92 | example: "t3/add-search", |
| 93 | })} |
| 94 | </Text> |
| 95 | <AppTextInput |
| 96 | key={props.prefix} |
| 97 | accessibilityLabel={gt("Branch prefix")} |
| 98 | onChangeText={(text) => edit("branchNamePrefix", text)} |
| 99 | defaultValue={props.prefix ?? ""} |
| 100 | placeholder={props.prefix === null ? gt("Mixed") : gt("No prefix")} |
| 101 | editable={!props.disabled.prefix} |
| 102 | autoCapitalize="none" |
| 103 | autoCorrect={false} |
| 104 | className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground" |
| 105 | onEndEditing={() => send(take("branchNamePrefix"))} |
| 106 | /> |
| 107 | </View> |
| 108 | ) : null} |
| 109 | {props.mode === "custom" ? ( |
| 110 | <View className="gap-2 px-4 py-3"> |
| 111 | <Text className="text-sm text-foreground-muted"> |
| 112 | <T>Append instructions to the naming prompt.</T> |
| 113 | </Text> |
| 114 | <AppTextInput |
| 115 | key={props.instructions} |
| 116 | accessibilityLabel={gt("Branch naming instructions")} |
| 117 | onChangeText={(text) => edit("branchNameInstructions", text)} |
| 118 | defaultValue={props.instructions ?? ""} |
| 119 | placeholder={ |
| 120 | props.instructions === null |
| 121 | ? gt("Mixed. Enter instructions for all selected targets.") |
| 122 | : gt("Use {prefix} followed by the issue ID and a short description.", { |
| 123 | prefix: "julius/", |
| 124 | }) |
| 125 | } |
| 126 | editable={!props.disabled.instructions} |
| 127 | multiline |
| 128 | autoCapitalize="sentences" |
| 129 | className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground" |
| 130 | onEndEditing={() => send(take("branchNameInstructions"))} |
| 131 | /> |
| 132 | </View> |
| 133 | ) : null} |
| 134 | </SettingsSection> |
| 135 | ); |
| 136 | } |