11import { scopeProjectRef , scopeThreadRef } from "@t3tools/client-runtime/environment" ;
22import { getCloneDestinationPath } from "@t3tools/client-runtime/operations/projects" ;
33import {
4- findPromptLinkProject ,
4+ findPromptLinkProjects ,
55 findPromptLinkPullRequestThread ,
6+ pickPromptLinkPullRequestThread ,
67 resolvePromptLinkModelSelection ,
78} from "@t3tools/client-runtime/prompt-links" ;
89import {
910 isAtomCommandInterrupted ,
1011 squashAtomCommandFailure ,
1112} from "@t3tools/client-runtime/state/runtime" ;
1213import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell" ;
14+ import type { EnvironmentId , ProjectId } from "@t3tools/contracts" ;
1315import {
1416 type NewThreadLink ,
1517 type PromptLinkRepository ,
1618 promptLinkPullRequestUrl ,
1719} from "@t3tools/shared/promptLinks" ;
1820import { useAtomValue } from "@effect/atom-react" ;
1921import { useLocation , useNavigate } from "@tanstack/react-router" ;
22+ import { Atom } from "effect/reactivity" ;
2023import { type ReactNode , useEffect , useEffectEvent , useRef , useState } from "react" ;
2124
2225import { useComposerDraftStore } from "~/composerDraftStore" ;
@@ -28,10 +31,9 @@ import { useProjects, useThreadShells, waitForProject } from "~/state/entities";
2831import { appAtomRegistry } from "~/rpc/atomRegistry" ;
2932import { useEnvironments , usePrimaryEnvironmentId } from "~/state/environments" ;
3033import { environmentPresentations } from "~/state/presentation" ;
31- import { allReachableShellsLiveAtom } from "~/state/shell" ;
34+ import { allReachableShellsLiveAtom , environmentShell } from "~/state/shell" ;
3235import { sourceControlEnvironment } from "~/state/sourceControl" ;
3336import { pullRequestEnvironment } from "~/state/pullRequests" ;
34- import { useEnvironmentQuery } from "~/state/query" ;
3537import { useAtomCommand } from "~/state/use-atom-command" ;
3638
3739import { usePullRequestHandoffs } from "../pullRequest/usePullRequestActions" ;
@@ -64,6 +66,36 @@ type LinkLaunch =
6466
6567const openedLinkKeys = new Set < string > ( ) ;
6668
69+ type PullRequestThreadLookup = {
70+ readonly targets : ReadonlyArray < readonly [ environmentId : EnvironmentId , projectId : ProjectId ] > ;
71+ readonly repository : string ;
72+ readonly number : number ;
73+ } ;
74+
75+ // A pull request's thread can be an older settled thread no shell holds, so ask every
76+ // environment with the repository.
77+ const pullRequestThreadLookupAtom = Atom . family ( ( key : string ) => {
78+ const { targets, repository, number } = JSON . parse ( key ) as PullRequestThreadLookup ;
79+ return Atom . make ( ( get ) =>
80+ pickPromptLinkPullRequestThread (
81+ targets
82+ // A shell that is not live cannot answer, and its lookup would wait forever.
83+ . filter ( ( [ environmentId ] ) => get ( environmentShell . statusAtom ( environmentId ) ) === "live" )
84+ . map ( ( [ environmentId , projectId ] ) => ( {
85+ environmentId,
86+ result : get (
87+ pullRequestEnvironment . linkedThreads ( {
88+ environmentId,
89+ input : { projectId, repository, number } ,
90+ } ) ,
91+ ) ,
92+ } ) ) ,
93+ ) ,
94+ ) . pipe ( Atom . withLabel ( `web-prompt-link-pull-request-thread:${ key } ` ) ) ;
95+ } ) ;
96+
97+ const NO_PULL_REQUEST_THREAD_LOOKUP = Atom . make ( null ) ;
98+
6799export function NewThreadLinkView ( { link } : { readonly link : NewThreadLink } ) {
68100 const locationKey = useLocation ( { select : ( location ) => location . state . __TSR_key ?? "" } ) ;
69101 const navigate = useNavigate ( ) ;
@@ -223,41 +255,32 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
223255 await openDraft ( project ) ;
224256 } ;
225257
226- // A pull request's thread can be an older settled thread the shell left out; the server knows.
227258 const localPullRequestThread =
228259 ready && link . kind === "pull-request"
229260 ? findPromptLinkPullRequestThread ( threads , link . pullRequest )
230261 : undefined ;
231- const pullRequestProject =
262+ const lookupProjects =
232263 ready && link . kind === "pull-request" && localPullRequestThread === undefined
233- ? findPromptLinkProject ( recentProjects ( ) , link . pullRequest )
234- : undefined ;
235- const linkedThreads = useEnvironmentQuery (
236- pullRequestProject === undefined || link . kind !== "pull-request"
237- ? null
238- : pullRequestEnvironment . linkedThreads ( {
239- environmentId : pullRequestProject . environmentId ,
240- input : {
241- projectId : pullRequestProject . id ,
264+ ? findPromptLinkProjects ( recentProjects ( ) , link . pullRequest )
265+ : [ ] ;
266+ const serverPullRequestThread = useAtomValue (
267+ link . kind === "pull-request" && lookupProjects . length > 0
268+ ? pullRequestThreadLookupAtom (
269+ JSON . stringify ( {
270+ targets : lookupProjects . map ( ( project ) => [ project . environmentId , project . id ] ) ,
242271 repository : `${ link . pullRequest . owner } /${ link . pullRequest . name } ` ,
243272 number : link . pullRequest . number ,
244- } ,
245- } ) ,
273+ } satisfies PullRequestThreadLookup ) ,
274+ )
275+ : NO_PULL_REQUEST_THREAD_LOOKUP ,
246276 ) ;
247- const linkedThread = linkedThreads . data ?. threads . find ( ( thread ) => thread . archivedAt === null ) ;
248- const linkedThreadsDone =
249- pullRequestProject === undefined || linkedThreads . data !== null || linkedThreads . error !== null ;
250277
251- if ( ready && linkedThreadsDone && state . status === "opening" && ! started ) {
278+ if ( ready && serverPullRequestThread !== undefined && state . status === "opening" && ! started ) {
252279 setStarted ( true ) ;
253- const pullRequestThread =
254- localPullRequestThread ??
255- ( linkedThread === undefined || pullRequestProject === undefined
256- ? undefined
257- : { environmentId : pullRequestProject . environmentId , id : linkedThread . id } ) ;
280+ const pullRequestThread = localPullRequestThread ?? serverPullRequestThread ?? undefined ;
258281 const repository = link . kind === "new" ? link . repository : link . pullRequest ;
259282 const project =
260- repository === null ? undefined : findPromptLinkProject ( recentProjects ( ) , repository ) ;
283+ repository === null ? undefined : findPromptLinkProjects ( recentProjects ( ) , repository ) [ 0 ] ;
261284 if ( pullRequestThread !== undefined ) {
262285 setState ( { status : "opened" } ) ;
263286 setLaunch ( { kind : "thread" , thread : pullRequestThread } ) ;
0 commit comments