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" ;
28import { 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" ;
413import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
514import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences" ;
615import * as Cause from "effect/Cause" ;
716import * as Option from "effect/Option" ;
817import { AsyncResult } from "effect/unstable/reactivity" ;
9- import { useNavigation } from "@react-navigation/native" ;
18+ import { StackActions , useNavigation } from "@react-navigation/native" ;
1019import { 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" ;
1221import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1322
23+ import { useThreadShells } from "../state/entities" ;
1424import { linearEnvironment } from "../state/linear" ;
1525import { useDebouncedValue } from "../state/queries" ;
1626import { useEnvironmentQuery } from "../state/query" ;
@@ -20,7 +30,9 @@ import {
2030 insertComposerDraftContext ,
2131 type ComposerDraftInsertion ,
2232} from "../state/use-composer-drafts" ;
33+ import { confirmOpenExistingThread } from "./confirmOpenExistingThread" ;
2334import { AppText as Text , AppTextInput } from "./AppText" ;
35+ import { PickerSheet } from "./PickerSheet" ;
2436
2537/** Linear allows 30 searches a minute, so typing settles before it queries. */
2638const SEARCH_DEBOUNCE_MS = 300 ;
@@ -36,12 +48,17 @@ function linearErrorMessage(error: unknown, fallback: string): string {
3648 return error instanceof Error && error . message . trim ( ) ? error . message : fallback ;
3749}
3850
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+ */
4056export type LinearIssuePickerTarget =
4157 | {
4258 readonly mode ?: "attach" ;
4359 readonly environmentId : EnvironmentId ;
4460 readonly draftKey : string ;
61+ readonly startFrom ?: { readonly projectId : ProjectId } ;
4562 }
4663 | {
4764 readonly mode : "link" ;
@@ -54,10 +71,31 @@ type OpenedLinearIssuePicker =
5471 readonly mode : "attach" ;
5572 readonly environmentId : EnvironmentId ;
5673 readonly draftKey : string ;
74+ readonly startFrom ?: { readonly projectId : ProjectId } ;
5775 readonly insertion : ComposerDraftInsertion ;
5876 }
5977 | { readonly mode : "link" ; readonly environmentId : EnvironmentId ; readonly threadId : ThreadId } ;
6078
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+
6199/**
62100 * A Linear issue picker. The owner renders `sheet` somewhere that outlives composer focus:
63101 * opening the sheet blurs the editor, which can unmount a focus-dependent toolbar.
@@ -104,49 +142,48 @@ export function useLinearIssuePicker(target: LinearIssuePickerTarget | null): {
104142 * the thread's tab group.
105143 */
106144function LinearIssuePickerSheet ( props : OpenedLinearIssuePicker & { readonly onClose : ( ) => void } ) {
107- const insets = useSafeAreaInsets ( ) ;
108145 const connection = useEnvironmentQuery (
109146 linearEnvironment . connection ( { environmentId : props . environmentId , input : { } } ) ,
110147 ) ;
111148 const phase = connection . data ?. phase ;
112149 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+ ) }
129171 </ 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 >
145174 ) ;
146175}
147176
148177function LinearIssueSearch ( props : OpenedLinearIssuePicker & { readonly onClose : ( ) => void } ) {
149178 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 ;
150187 const [ query , setQuery ] = useState ( "" ) ;
151188 const trimmed = query . trim ( ) ;
152189 const settled = useDebouncedValue ( trimmed , SEARCH_DEBOUNCE_MS ) ;
@@ -173,8 +210,35 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose:
173210 ? linearErrorMessage ( Cause . squash ( result . cause ) , "Could not load Linear issues." )
174211 : null ;
175212
176- const attach = async ( issue : LinearIssueSummary ) => {
213+ const pick = async ( issue : LinearIssueSummary ) => {
177214 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 ) => {
178242 setAttaching ( issue . id ) ;
179243 try {
180244 if ( props . mode === "link" ) {
@@ -217,6 +281,7 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose:
217281 Alert . alert ( "Too many context items" , "Remove some context from the draft and try again." ) ;
218282 return ;
219283 }
284+ if ( startFrom ) startFromIssueIds . set ( props . draftKey , issue . id ) ;
220285 props . onClose ( ) ;
221286 } finally {
222287 setAttaching ( null ) ;
@@ -269,7 +334,7 @@ function LinearIssueSearch(props: OpenedLinearIssuePicker & { readonly onClose:
269334 accessibilityRole = "button"
270335 accessibilityLabel = { `${ item . identifier } ${ item . title } , ${ item . stateName } ` }
271336 disabled = { attaching !== null }
272- onPress = { ( ) => void attach ( item ) }
337+ onPress = { ( ) => void pick ( item ) }
273338 className = "flex-row items-center gap-3 px-4 py-3 active:bg-subtle disabled:opacity-60"
274339 >
275340 < View className = "min-w-0 flex-1 gap-0.5" >
0 commit comments