1+ import type { ThreadPullRequestLink } from "@t3tools/contracts" ;
2+
3+ import { visibleThreadPullRequests } from "./threadPullRequests.ts" ;
4+
15export interface ComposerPullRequestMatch {
26 readonly number : number ;
37 readonly projectId : string ;
@@ -6,37 +10,78 @@ export interface ComposerPullRequestMatch {
610}
711
812/**
9- * Pull requests matching the numeric fragment typed after `#`, de-duplicated. An exact number
10- * match always outranks a substring match so the result limit can never drop the pull request
11- * the user typed in full; the rest stay newest first.
13+ * Suggestion rows for the pull requests a thread links, from any repository. Built from the
14+ * synced snapshot, so a link the server has not read yet is left out until it has one.
15+ */
16+ export function composerPullRequestsFromLinks (
17+ links : ReadonlyArray < ThreadPullRequestLink > ,
18+ projectId : string ,
19+ ) {
20+ return visibleThreadPullRequests ( links ) . flatMap ( ( link ) =>
21+ link . snapshot === null
22+ ? [ ]
23+ : [
24+ {
25+ projectId,
26+ repository : link . repository ,
27+ number : link . number ,
28+ url : link . url ,
29+ title : link . snapshot . title ,
30+ headBranch : link . snapshot . headBranch ,
31+ baseBranch : link . snapshot . baseBranch ,
32+ state : link . snapshot . state ,
33+ isDraft : link . snapshot . isDraft ,
34+ updatedAt : link . snapshot . updatedAt ?? link . linkedAt ,
35+ } ,
36+ ] ,
37+ ) ;
38+ }
39+
40+ /**
41+ * Pull requests matching the numeric fragment typed after `#`, de-duplicated. `entries` are kept
42+ * to the project's repository; `linked` rows (the thread's own pull requests) may come from any
43+ * repository. An exact number match always outranks a substring match so the result limit can
44+ * never drop the pull request the user typed in full; then linked rows lead, newest first.
1245 */
13- export function filterComposerPullRequestMatches < Entry extends ComposerPullRequestMatch > ( input : {
46+ export function filterComposerPullRequestMatches <
47+ Entry extends ComposerPullRequestMatch ,
48+ Linked extends ComposerPullRequestMatch = never ,
49+ > ( input : {
1450 readonly entries : ReadonlyArray < Entry > ;
51+ readonly linked ?: ReadonlyArray < Linked > ;
1552 readonly projectId : string ;
1653 readonly repository : string ;
1754 readonly query : string ;
1855 readonly limit : number ;
19- } ) : ReadonlyArray < Entry > {
56+ } ) : ReadonlyArray < Entry | Linked > {
2057 const repository = input . repository . trim ( ) . toLowerCase ( ) ;
58+ const keyOf = ( entry : ComposerPullRequestMatch ) =>
59+ `${ entry . repository . trim ( ) . toLowerCase ( ) } #${ entry . number } ` ;
60+ const linked = ( input . linked ?? [ ] ) . filter ( ( entry ) => String ( entry . number ) . includes ( input . query ) ) ;
61+ const linkedKeys = new Set ( linked . map ( keyOf ) ) ;
2162 const matchingEntries = input . entries . filter (
2263 ( entry ) =>
2364 entry . projectId === input . projectId &&
2465 entry . repository . trim ( ) . toLowerCase ( ) === repository &&
2566 String ( entry . number ) . includes ( input . query ) ,
2667 ) ;
27- const uniqueEntries = new Map < number , Entry > ( ) ;
28- for ( const entry of matchingEntries ) {
29- if ( ! uniqueEntries . has ( entry . number ) ) {
30- uniqueEntries . set ( entry . number , entry ) ;
68+ // Project rows go first so a linked pull request the listing also has keeps the fresher row.
69+ const uniqueEntries = new Map < string , Entry | Linked > ( ) ;
70+ for ( const entry of [ ...matchingEntries , ...linked ] ) {
71+ if ( ! uniqueEntries . has ( keyOf ( entry ) ) ) {
72+ uniqueEntries . set ( keyOf ( entry ) , entry ) ;
3173 }
3274 }
33- const isExactMatch = ( entry : Entry ) => String ( entry . number ) === input . query ;
75+ const isExactMatch = ( entry : ComposerPullRequestMatch ) => String ( entry . number ) === input . query ;
76+ const isLinked = ( entry : ComposerPullRequestMatch ) => linkedKeys . has ( keyOf ( entry ) ) ;
3477 // `.sort()` on a copy, not `.toSorted()`: this runs on Hermes, which has no ES2023 array
3578 // methods, and reaching for one here crashed the composer as the suggestions loaded.
3679 return [ ...uniqueEntries . values ( ) ]
37- . sort ( ( left , right ) => {
38- const exactness = Number ( isExactMatch ( right ) ) - Number ( isExactMatch ( left ) ) ;
39- return exactness !== 0 ? exactness : right . updatedAt . localeCompare ( left . updatedAt ) ;
40- } )
80+ . sort (
81+ ( left , right ) =>
82+ Number ( isExactMatch ( right ) ) - Number ( isExactMatch ( left ) ) ||
83+ Number ( isLinked ( right ) ) - Number ( isLinked ( left ) ) ||
84+ right . updatedAt . localeCompare ( left . updatedAt ) ,
85+ )
4186 . slice ( 0 , input . limit ) ;
4287}
0 commit comments