1import { useEnvironmentQuery } from "../../state/query";
2import { useSelectedThreadGitActions } from "../../state/use-selected-thread-git-actions";
3import { useSelectedThreadGitState } from "../../state/use-selected-thread-git-state";
4import { useThreadSelection } from "../../state/use-thread-selection";
5import { vcsEnvironment } from "../../state/vcs";
6import { useThreadGitMenuDefinition } from "../threads/ThreadGitControls";
7import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
8import type { ReviewSectionItem } from "./reviewModel";
9import type { ScreenHeaderMenuItem, ScreenHeaderMenu } from "../../components/ScreenHeader.types";
10import type { AppSymbolName } from "../../components/AppSymbol";
11
12interface ReviewHeaderPresentation {
13 readonly title: string;
14 readonly subtitle: string;
15 readonly gitMenu: ScreenHeaderMenu | null;
16 readonly menuIcon: AppSymbolName;
17 readonly refreshAction?: ScreenHeaderMenuItem;
18}
19
20export function useReviewHeaderPresentation(props: {
21 readonly environmentId: EnvironmentId;
22 readonly threadId: ThreadId;
23 readonly title: string;
24 readonly subtitle: string;
25 readonly androidSubtitle: string;
26 readonly selectedThreadCwd: string | null;
27 readonly selectedSection: ReviewSectionItem | null;
28 readonly onRefresh: () => Promise<void>;
29}): ReviewHeaderPresentation {
30 const { selectedThread } = useThreadSelection();
31 const gitState = useSelectedThreadGitState();
32 const gitActions = useSelectedThreadGitActions();
33 const gitStatusQuery = useEnvironmentQuery(
34 selectedThread !== null && props.selectedThreadCwd !== null
35 ? vcsEnvironment.status({
36 environmentId: selectedThread.environmentId,
37 input: { cwd: props.selectedThreadCwd },
38 })
39 : null,
40 );
41 // The selection-based git hooks only apply when this review belongs to the
42 // selected thread (it always does when reached from the thread's toolbar).
43 const gitMenuAvailable =
44 selectedThread !== null && String(selectedThread.id) === String(props.threadId);
45 const gitMenu = useThreadGitMenuDefinition({
46 environmentId: props.environmentId,
47 threadId: props.threadId,
48 currentBranch: selectedThread?.branch ?? null,
49 gitStatus: gitStatusQuery.data,
50 gitOperationLabel: gitState.gitOperationLabel,
51 onPull: gitActions.onPullSelectedThreadBranch,
52 onRunAction: gitActions.onRunSelectedThreadGitAction,
53 });
54 return {
55 title: props.title,
56 subtitle: props.subtitle,
57 menuIcon: "ellipsis",
58 gitMenu: gitMenuAvailable ? gitMenu : null,
59 };
60}