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

PullRequestStackCard.tsx

88 lines3.5 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { useState } from "react";
3import { Platform, View } from "react-native";
4import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
5import { AppText as Text } from "../../components/ui/AppText";
6import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls";
7import { pullRequestLayerParams } from "./pullRequestStack";
8import { pullRequestStatus } from "./pullRequestStatus";
9import type { PullRequestParams } from "./usePullRequestDiff";
10import type { PullRequestStackState } from "./usePullRequestStack";
11import { PullRequestGlyph } from "./pullRequestGlyphs";
12​
13export function PullRequestStackCard(props: {
14 readonly params: PullRequestParams;
15 readonly state: PullRequestStackState;
16}) {
17 const navigation = useNavigation();
18 const selectDetail = useSelectWorkspaceDetail();
19 const [expanded, setExpanded] = useState(false);
20 const stack = props.state.stack;
21 if (!stack) {
22 const summary =
23 props.params.initialSummary?.number === props.params.number
24 ? props.params.initialSummary.stack
25 : undefined;
26 if (!summary) return null;
27 return (
28 <PullRequestCardGroup>
29 <PullRequestCardRow
30 title="Stack"
31 icon={PullRequestGlyph.stack}
32 value={`${summary.position} of ${summary.size}`}
33 last
34 />
35 </PullRequestCardGroup>
36 );
37 }
38 const position = stack.layers.findIndex((layer) => layer.number === props.params.number) + 1;
39 return (
40 <PullRequestCardGroup>
41 <PullRequestCardRow
42 title="Stack"
43 icon={PullRequestGlyph.stack}
44 value={`${position} of ${stack.layers.length}`}
45 accessibilityLabel={`Stack, ${position} of ${stack.layers.length}${props.state.isFresh ? "" : ", showing saved stack"}`}
46 expanded={expanded}
47 onPress={() => setExpanded(!expanded)}
48 last={!expanded}
49 />
50 {expanded ? (
51 <View>
52 {stack.layers.map((layer, index) => {
53 const selected = layer.number === props.params.number;
54 const status = pullRequestStatus({
55 state: layer.state,
56 isDraft: layer.isDraft === true,
57 });
58 return (
59 <View key={layer.number} className={selected ? "bg-thread-selected" : undefined}>
60 <PullRequestCardRow
61 title={layer.title ?? layer.headBranch}
62 icon={status.icon}
63 tint={status.tint}
64 value={selected ? "Current" : status.label}
65 detail={`#${layer.number} · ${index + 1} of ${stack.layers.length}`}
66 onPress={
67 selected
68 ? undefined
69 : () => {
70 const params = pullRequestLayerParams(props.params, layer.number);
71 if (Platform.OS === "android")
72 selectDetail({ name: "PullRequest", params });
73 else navigation.dispatch(StackActions.push("PullRequest", params));
74 }
75 }
76 last={index === stack.layers.length - 1}
77 />
78 </View>
79 );
80 })}
81 <Text className="px-4 py-3 text-xs text-foreground-muted">
82 Base: {stack.base}. Open a layer to review it or merge the stack through that layer.
83 </Text>
84 </View>
85 ) : null}
86 </PullRequestCardGroup>
87 );