Open History to see the last commit
1import { StackActions, useNavigation } from "@react-navigation/native";
2import { getCloneDirectoryName } from "@t3tools/client-runtime/operations/projects";
3import {
4 AuthOrchestrationOperateScope,
5 AuthSourceControlWriteScope,
6 type EnvironmentId,
7 type ProjectId,
8 type ScopedThreadRef,
9 type ThreadHandoffProjectChoice,
10 type TurnItemId,
11} from "@t3tools/contracts";
12import { withoutRemoteUrlCredentials } from "@t3tools/shared/cloneUrl";
13import { useGT } from "gt-react-native";
14import { useState } from "react";
15
16import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
17import { useProject, useProjects, useThreadShell } from "../../state/entities";
18import { useEnvironmentScope } from "../../state/session";
19import { SettingsSection } from "../settings/components/SettingsSection";
20
21export function useHandoffProject(
22 threadRef: ScopedThreadRef,
23 targetId: EnvironmentId | undefined,
24 needsProject: boolean,
25 disabled: boolean,
26 projectChoices?: ReadonlyArray<ThreadHandoffProjectChoice>,
27 turnItemId?: TurnItemId,
28) {
29 const gt = useGT();
30 const navigation = useNavigation();
31 const thread = useThreadShell(threadRef);
32 const source = useProject(
33 thread ? { environmentId: threadRef.environmentId, projectId: thread.projectId } : null,
34 );
35 const projects = useProjects();
36 const mayClone = useEnvironmentScope(targetId ?? null, AuthSourceControlWriteScope);
37 const mayCreate = useEnvironmentScope(targetId ?? null, AuthOrchestrationOperateScope);
38 const [picked, setPicked] = useState<{ environmentId: EnvironmentId; projectId: ProjectId }>();
39 const synced = needsProject
40 ? projects.filter((project) => project.environmentId === targetId)
41 : [];
42 const candidates =
43 needsProject && projectChoices
44 ? [
45 ...projectChoices,
46 ...synced.filter((project) => !projectChoices.some(({ id }) => id === project.id)),
47 ]
48 : synced;
49 const key = source?.repositoryIdentity?.canonicalKey;
50 const matches =
51 key && !projectChoices
52 ? synced.filter((project) => project.repositoryIdentity?.canonicalKey === key)
53 : [];
54 const project =
55 candidates.find(
56 (candidate) => picked?.environmentId === targetId && picked?.projectId === candidate.id,
57 ) ?? (matches.length === 1 ? matches[0] : undefined);
58 const remoteUrl = withoutRemoteUrlCredentials(
59 source?.repositoryIdentity?.origin?.remoteUrl ??
60 source?.repositoryIdentity?.locator.remoteUrl ??
61 "",
62 );
63
64 return {
65 projectId: project?.id,
66 clone:
67 needsProject && project === undefined && targetId && mayClone && mayCreate && remoteUrl
68 ? () =>
69 navigation.dispatch(
70 StackActions.push("AddProjectDestination", {
71 environmentId: targetId,
72 remoteUrl,
73 repositoryTitle: source?.title,
74 repositoryName: getCloneDirectoryName(remoteUrl),
75 handoff: { ...threadRef, turnItemId },
76 }),
77 )
78 : undefined,
79 picker:
80 needsProject && candidates.length > 0 ? (
81 <SettingsSection title={gt`Choose a project`}>
82 {candidates.map((candidate, index) => (
83 <ListChoiceRow
84 key={candidate.id}
85 label={candidate.title}
86 description={candidate.workspaceRoot}
87 selected={project?.id === candidate.id}
88 separated={index > 0}
89 disabled={disabled}
90 onPress={() => {
91 if (targetId) setPicked({ environmentId: targetId, projectId: candidate.id });
92 }}
93 />
94 ))}
95 </SettingsSection>
96 ) : null,
97 };
98}