|
1 | | -import type { EnvironmentId, LinearIssueSummary, ThreadId } from "@t3tools/contracts"; |
| 1 | +import type { |
| 2 | + EnvironmentId, |
| 3 | + LinearIssueSummary, |
| 4 | + OrchestrationMessageContext, |
| 5 | + ProjectId, |
| 6 | + ThreadId, |
| 7 | +} from "@t3tools/contracts"; |
2 | 8 | import { useAtomValue } from "@effect/atom-react"; |
3 | | -import { linearIssueContextRecord } from "@t3tools/client-runtime/state/linear"; |
| 9 | +import { |
| 10 | + linearIssueContextRecord, |
| 11 | + threadsForLinearIssue, |
| 12 | +} from "@t3tools/client-runtime/state/linear"; |
4 | 13 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
5 | 14 | import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; |
6 | 15 | import * as Cause from "effect/Cause"; |
7 | 16 | import * as Option from "effect/Option"; |
8 | 17 | import { AsyncResult } from "effect/unstable/reactivity"; |
9 | | -import { useNavigation } from "@react-navigation/native"; |
| 18 | +import { StackActions, useNavigation } from "@react-navigation/native"; |
10 | 19 | import { useState, type ReactNode } from "react"; |
11 | | -import { ActivityIndicator, Alert, FlatList, Modal, Platform, Pressable, View } from "react-native"; |
| 20 | +import { ActivityIndicator, Alert, FlatList, Pressable, View } from "react-native"; |
12 | 21 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
13 | 22 |
|
| 23 | +import { useThreadShells } from "../state/entities"; |
14 | 24 | import { linearEnvironment } from "../state/linear"; |
15 | 25 | import { useDebouncedValue } from "../state/queries"; |
16 | 26 | import { useEnvironmentQuery } from "../state/query"; |
@@ -20,7 +30,9 @@ import { |
20 | 30 | insertComposerDraftContext, |
21 | 31 | type ComposerDraftInsertion, |
22 | 32 | } from "../state/use-composer-drafts"; |
| 33 | +import { confirmOpenExistingThread } from "./confirmOpenExistingThread"; |
23 | 34 | import { AppText as Text, AppTextInput } from "./AppText"; |
| 35 | +import { PickerSheet } from "./PickerSheet"; |
24 | 36 |
|
25 | 37 | /** Linear allows 30 searches a minute, so typing settles before it queries. */ |
26 | 38 | const SEARCH_DEBOUNCE_MS = 300; |
@@ -36,12 +48,17 @@ function linearErrorMessage(error: unknown, fallback: string): string { |
36 | 48 | return error instanceof Error && error.message.trim() ? error.message : fallback; |
37 | 49 | } |
38 | 50 |
|
39 | | -/** Attach inserts the issue into a composer draft; link links it to the thread's tab group. */ |
| 51 | +/** |
| 52 | + * Attach inserts the issue into a composer draft; link links it to the thread's tab group. An |
| 53 | + * attach with `startFrom` starts a new thread from the issue: it first offers threads already |
| 54 | + * linked to it, and the thread the draft creates is linked once it sends. |
| 55 | + */ |
40 | 56 | export type LinearIssuePickerTarget = |
41 | 57 | | { |
42 | 58 | readonly mode?: "attach"; |
43 | 59 | readonly environmentId: EnvironmentId; |
44 | 60 | readonly draftKey: string; |
| 61 | + readonly startFrom?: { readonly projectId: ProjectId }; |
45 | 62 | } |
46 | 63 | | { |
47 | 64 | readonly mode: "link"; |
@@ -54,10 +71,31 @@ type OpenedLinearIssuePicker = |
54 | 71 | readonly mode: "attach"; |
55 | 72 | readonly environmentId: EnvironmentId; |
56 | 73 | readonly draftKey: string; |
| 74 | + readonly startFrom?: { readonly projectId: ProjectId }; |
57 | 75 | readonly insertion: ComposerDraftInsertion; |
58 | 76 | } |
59 | 77 | | { readonly mode: "link"; readonly environmentId: EnvironmentId; readonly threadId: ThreadId }; |
60 | 78 |
|
| 79 | +/** Issues picked to start a draft's thread from, by draft key, until the draft sends. */ |
| 80 | +const startFromIssueIds = new Map<string, string>(); |
| 81 | + |
| 82 | +/** |
| 83 | + * The issue the draft was started from, if its chip is still in the draft. Clears it: the caller |
| 84 | + * links the thread the draft just created. |
| 85 | + */ |
| 86 | +export function takeStartFromLinearIssue( |
| 87 | + draftKey: string, |
| 88 | + context: OrchestrationMessageContext | undefined, |
| 89 | +): string | null { |
| 90 | + const issueId = startFromIssueIds.get(draftKey); |
| 91 | + startFromIssueIds.delete(draftKey); |
| 92 | + if (issueId === undefined) return null; |
| 93 | + const attached = context?.records.some( |
| 94 | + (record) => record.kind === "linear-issue" && "issueId" in record && record.issueId === issueId, |
| 95 | + ); |
| 96 | + return attached ? issueId : null; |
| 97 | +} |
| 98 | + |
61 | 99 | /** |
62 | 100 | * A Linear issue picker. The owner renders `sheet` somewhere that outlives composer focus: |
63 | 101 | * opening the sheet blurs the editor, which can unmount a focus-dependent toolbar. |
@@ -104,49 +142,48 @@ export function useLinearIssuePicker(target: LinearIssuePickerTarget | null): { |
104 | 142 | * the thread's tab group. |
105 | 143 | */ |
106 | 144 | function LinearIssuePickerSheet(props: OpenedLinearIssuePicker & { readonly onClose: () => void }) { |
107 | | - const insets = useSafeAreaInsets(); |
108 | 145 | const connection = useEnvironmentQuery( |
109 | 146 | linearEnvironment.connection({ environmentId: props.environmentId, input: {} }), |
110 | 147 | ); |
111 | 148 | const phase = connection.data?.phase; |
112 | 149 | return ( |
113 | | - <Modal presentationStyle="pageSheet" animationType="slide" onRequestClose={props.onClose}> |
114 | | - <View |
115 | | - className="flex-1 bg-sheet-solid" |
116 | | - style={ |
117 | | - Platform.OS === "android" |
118 | | - ? { paddingTop: insets.top, paddingBottom: insets.bottom } |
119 | | - : undefined |
120 | | - } |
121 | | - > |
122 | | - <View className="flex-row items-center justify-between p-4"> |
123 | | - <Text className="text-lg text-foreground"> |
124 | | - {props.mode === "link" ? "Link Linear issue" : "Linear issue"} |
125 | | - </Text> |
126 | | - <Pressable accessibilityRole="button" onPress={props.onClose} className="p-3"> |
127 | | - <Text className="text-foreground">Cancel</Text> |
128 | | - </Pressable> |
| 150 | + <PickerSheet |
| 151 | + title={ |
| 152 | + props.mode === "link" |
| 153 | + ? "Link Linear issue" |
| 154 | + : props.startFrom |
| 155 | + ? "Start from issue" |
| 156 | + : "Linear issue" |
| 157 | + } |
| 158 | + onClose={props.onClose} |
| 159 | + > |
| 160 | + {phase === "connected" ? ( |
| 161 | + <LinearIssueSearch {...props} /> |
| 162 | + ) : ( |
| 163 | + <View className="flex-1 items-center justify-center p-6"> |
| 164 | + {phase === undefined && !connection.error ? ( |
| 165 | + <ActivityIndicator /> |
| 166 | + ) : ( |
| 167 | + <Text className="text-center text-foreground-muted"> |
| 168 | + {connection.error ?? "Linear is not connected. Connect it in Settings > Linear."} |
| 169 | + </Text> |
| 170 | + )} |
129 | 171 | </View> |
130 | | - {phase === "connected" ? ( |
131 | | - <LinearIssueSearch {...props} /> |
132 | | - ) : ( |
133 | | - <View className="flex-1 items-center justify-center p-6"> |
134 | | - {phase === undefined && !connection.error ? ( |
135 | | - <ActivityIndicator /> |
136 | | - ) : ( |
137 | | - <Text className="text-center text-foreground-muted"> |
138 | | - {connection.error ?? "Linear is not connected. Connect it in Settings > Linear."} |
139 | | - </Text> |
140 | | - )} |
141 | | - </View> |
142 | | - )} |
143 | | - </View> |
144 | | - </Modal> |
| 172 | + )} |
| 173 | + </PickerSheet> |
145 | 174 | ); |
146 | 175 | } |
147 | 176 |
|
148 | 177 | function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose: () => void }) { |
149 | 178 | const insets = useSafeAreaInsets(); |
| 179 | + const navigation = useNavigation(); |
| 180 | + const startFrom = props.mode === "attach" ? props.startFrom : undefined; |
| 181 | + const threadShells = useThreadShells(); |
| 182 | + const threadLinks = useEnvironmentQuery( |
| 183 | + startFrom |
| 184 | + ? linearEnvironment.threadLinks({ environmentId: props.environmentId, input: {} }) |
| 185 | + : null, |
| 186 | + ).data; |
150 | 187 | const [query, setQuery] = useState(""); |
151 | 188 | const trimmed = query.trim(); |
152 | 189 | const settled = useDebouncedValue(trimmed, SEARCH_DEBOUNCE_MS); |
@@ -173,8 +210,35 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose: |
173 | 210 | ? linearErrorMessage(Cause.squash(result.cause), "Could not load Linear issues.") |
174 | 211 | : null; |
175 | 212 |
|
176 | | - const attach = async (issue: LinearIssueSummary) => { |
| 213 | + const pick = async (issue: LinearIssueSummary) => { |
177 | 214 | if (attaching) return; |
| 215 | + if (startFrom) { |
| 216 | + const existing = threadsForLinearIssue( |
| 217 | + threadShells.filter( |
| 218 | + (thread) => |
| 219 | + thread.environmentId === props.environmentId && |
| 220 | + thread.projectId === startFrom.projectId, |
| 221 | + ), |
| 222 | + threadLinks, |
| 223 | + issue.id, |
| 224 | + ); |
| 225 | + const choice = await confirmOpenExistingThread(issue.identifier, existing); |
| 226 | + if (choice === "cancel") return; |
| 227 | + if (choice !== "start") { |
| 228 | + props.onClose(); |
| 229 | + navigation.dispatch( |
| 230 | + StackActions.replace("Thread", { |
| 231 | + environmentId: String(choice.environmentId), |
| 232 | + threadId: String(choice.id), |
| 233 | + }), |
| 234 | + ); |
| 235 | + return; |
| 236 | + } |
| 237 | + } |
| 238 | + await attach(issue); |
| 239 | + }; |
| 240 | + |
| 241 | + const attach = async (issue: LinearIssueSummary) => { |
178 | 242 | setAttaching(issue.id); |
179 | 243 | try { |
180 | 244 | if (props.mode === "link") { |
@@ -217,6 +281,7 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose: |
217 | 281 | Alert.alert("Too many context items", "Remove some context from the draft and try again."); |
218 | 282 | return; |
219 | 283 | } |
| 284 | + if (startFrom) startFromIssueIds.set(props.draftKey, issue.id); |
220 | 285 | props.onClose(); |
221 | 286 | } finally { |
222 | 287 | setAttaching(null); |
@@ -269,7 +334,7 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose: |
269 | 334 | accessibilityRole="button" |
270 | 335 | accessibilityLabel={`${item.identifier} ${item.title}, ${item.stateName}`} |
271 | 336 | disabled={attaching !== null} |
272 | | - onPress={() => void attach(item)} |
| 337 | + onPress={() => void pick(item)} |
273 | 338 | className="flex-row items-center gap-3 px-4 py-3 active:bg-subtle disabled:opacity-60" |
274 | 339 | > |
275 | 340 | <View className="min-w-0 flex-1 gap-0.5"> |
|