Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

IssueEditScreen.tsx

102 lines4.0 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { useGT } from "gt-react-native";
3import { useRef, useState } from "react";
4​
5import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText";
6import { Button } from "../../components/ui/Button";
7import { issueEnvironment, refreshIssueLists } from "../../state/issues";
8import { useEnvironmentQuery } from "../../state/query";
9import { useAtomCommand } from "../../state/use-atom-command";
10import { PullRequestSheetContent } from "../pull-requests/PullRequestSheetContent";
11import { issueTarget, type IssueParams } from "./issueParams";
12import { issueCommandError, useIssueAccess } from "./issuePresentation";
13​
14type IssueEditParams = IssueParams & { readonly field: "title" | "body" };
15​
16export function IssueEditScreen({ route }: StaticScreenProps<IssueEditParams>) {
17 const params = route.params;
18 const gt = useGT();
19 const navigation = useNavigation();
20 const target = issueTarget(params);
21 const detailQuery = useEnvironmentQuery(issueEnvironment.detail(target));
22 const detail = detailQuery.data;
23 const access = useIssueAccess(params, detail);
24 const update = useAtomCommand(issueEnvironment.update, { reportFailure: false });
25 const isTitle = params.field === "title";
26 const original = detail ? (isTitle ? detail.title : detail.body) : null;
27 const [draft, setDraft] = useState<string | null>(null);
28 const value = draft ?? original ?? "";
29 const [busy, setBusy] = useState(false);
30 const [error, setError] = useState<string | null>(null);
31 const inFlight = useRef(false);
32 const allowed = access?.edit === true;
33 const unchanged = draft === null || draft === original;
34 const invalid = isTitle && value.trim().length === 0;
35 const save = async () => {
36 if (!allowed || unchanged || invalid || inFlight.current) return;
37 inFlight.current = true;
38 setBusy(true);
39 setError(null);
40 const failure = issueCommandError(
41 await update({
42 ...target,
43 input: { ...target.input, ...(isTitle ? { title: value.trim() } : { body: value }) },
44 }),
45 );
46 inFlight.current = false;
47 setBusy(false);
48 if (failure !== null) {
49 setError(failure);
50 return;
51 }
52 refreshIssueLists(target.environmentId);
53 navigation.goBack();
54 };
55 return (
56 <PullRequestSheetContent
57 busy={busy}
58 title={isTitle ? gt`Edit title` : gt`Edit description`}
59 subtitle={`${detail?.displayKey ?? params.issueKey} · ${params.scope}`}
60 footer={
61 <Button
62 fullWidth
63 tone="contrast"
64 label={busy ? gt`Saving…` : gt`Save`}
65 loading={busy}
66 disabled={!allowed || unchanged || invalid || original === null}
67 onPress={() => void save()}
68 />
69 }
70 >
71 <TextInput
72 multiline={!isTitle}
73 editable={!busy && original !== null}
74 autoFocus
75 accessibilityLabel={isTitle ? gt`Issue title` : gt`Issue description`}
76 placeholder={isTitle ? gt({ context: "issue title field" })`Title` : gt`Write in Markdown…`}
77 placeholderTextColorClassName="accent-foreground-muted"
78 maxLength={isTitle ? 1024 : 65536}
79 value={value}
80 onChangeText={setDraft}
81 returnKeyType={isTitle ? "done" : undefined}
82 onSubmitEditing={isTitle ? () => void save() : undefined}
83 className={
84 isTitle
85 ? "min-h-12 rounded-[20px] bg-grouped-card px-4 text-base text-foreground"
86 : "min-h-48 rounded-[20px] bg-grouped-card p-4 text-base text-foreground"
87 }
88 style={isTitle ? undefined : { textAlignVertical: "top" }}
89 />
90 {detail && !allowed ? (
91 <Text className="text-xs text-foreground-muted">
92 {access?.blocked ?? gt`You can't edit this issue.`}
93 </Text>
94 ) : null}
95 {error || detailQuery.error ? (
96 <Text selectable className="text-sm text-danger-foreground">
97 {error ?? detailQuery.error}
98 </Text>
99 ) : null}
100 </PullRequestSheetContent>
101 );