@@ -3,9 +3,12 @@ import type {
33 EnvironmentId ,
44 PullRequestRef ,
55 PullRequestReviewerCandidate ,
6- SourceControlProviderKind ,
76} from "@t3tools/contracts" ;
8- import { mergeReviewerCandidates } from "@t3tools/client-runtime/pull-request-reviewers" ;
7+ import {
8+ hostReviewerQuery ,
9+ reviewerCandidateView ,
10+ type ReviewerTruncation ,
11+ } from "@t3tools/client-runtime/pull-request-reviewers" ;
912import { CheckIcon , UserPlusIcon } from "lucide-react" ;
1013import { useState } from "react" ;
1114
@@ -19,16 +22,27 @@ import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker";
1922import { PullRequestActorLabel } from "./pullRequestPresentation" ;
2023import { readableFailure } from "./pullRequestDetail.logic" ;
2124
25+ const TRUNCATED_LABELS : Record < ReviewerTruncation , ( error : string | null ) => string > = {
26+ unsearchable : ( ) =>
27+ "This repository has more people with access than are listed here. Ask for the rest on the host." ,
28+ searchable : ( ) =>
29+ "This repository has more people with access than are listed here. Search to find them." ,
30+ "search-failed" : ( error ) =>
31+ `Only the people already listed are matched: the host search failed. ${ error ?? "" } ` ,
32+ "search-truncated" : ( ) =>
33+ "More people match than the host returned. Keep typing to narrow it down." ,
34+ } ;
35+
2236export function PullRequestReviewerPicker ( {
2337 environmentId,
2438 reference,
2539 allowed,
26- provider ,
40+ searchable ,
2741} : {
2842 environmentId : EnvironmentId ;
2943 reference : PullRequestRef ;
3044 allowed : boolean ;
31- provider : SourceControlProviderKind ;
45+ searchable : boolean ;
3246} ) {
3347 const [ open , setOpen ] = useState ( false ) ;
3448 const [ query , setQuery ] = useState ( "" ) ;
@@ -38,29 +52,25 @@ export function PullRequestReviewerPicker({
3852 open ? pullRequestEnvironment . reviewerCandidates ( { environmentId, input : reference } ) : null ,
3953 ) ;
4054 // Past the first page the host is asked, since what was loaded cannot answer for the rest.
41- const searchQuery = useDebouncedValue ( query . trim ( ) , 250 ) ;
42- const supportsSearch = provider === "github" || provider === "gitlab" ;
43- const searchesHost =
44- supportsSearch && candidatesQuery . data ?. truncated === true && searchQuery . length > 0 ;
45- // Only once the debounce has caught up, or rows matching the previous text would linger.
46- const searched = searchesHost && searchQuery === query . trim ( ) ;
55+ const debouncedQuery = useDebouncedValue ( query . trim ( ) , 250 ) ;
56+ const searchInput = { loaded : candidatesQuery . data , searchable, query, debouncedQuery } ;
57+ const hostQuery = hostReviewerQuery ( searchInput ) ;
4758 const searchResult = useEnvironmentQuery (
48- open && searchesHost
59+ open && hostQuery !== null
4960 ? pullRequestEnvironment . reviewerSearch ( {
5061 environmentId,
51- input : { ...reference , query : searchQuery } ,
62+ input : { ...reference , query : hostQuery } ,
5263 } )
5364 : null ,
5465 ) ;
5566 const requestReviewers = useAtomCommand ( pullRequestEnvironment . requestReviewers , {
5667 reportFailure : false ,
5768 } ) ;
5869
59- const candidates = mergeReviewerCandidates (
60- candidatesQuery . data ?. candidates ?? [ ] ,
61- searched ? ( searchResult . data ?. candidates ?? [ ] ) : [ ] ,
62- query ,
63- ) ;
70+ const { candidates, searchPending, searchError, truncation } = reviewerCandidateView ( {
71+ ...searchInput ,
72+ search : searchResult ,
73+ } ) ;
6474
6575 const toggle = async ( candidate : PullRequestReviewerCandidate ) => {
6676 if ( pending !== null ) return ;
@@ -89,7 +99,7 @@ export function PullRequestReviewerPicker({
8999 }
90100 // The command reads the loaded list again only for somebody it lacked, and a search result
91101 // is keyed by what was typed, so the one on screen is read here or it keeps the old request.
92- if ( searchesHost ) searchResult . refresh ( ) ;
102+ if ( hostQuery !== null ) searchResult . refresh ( ) ;
93103 toastManager . add ( {
94104 type : "success" ,
95105 title : candidate . isRequested
@@ -109,41 +119,20 @@ export function PullRequestReviewerPicker({
109119 query = { query }
110120 onQueryChange = { setQuery }
111121 searchLabel = "Search people with access"
112- isPending = {
113- ( candidatesQuery . isPending && candidatesQuery . data === null ) ||
114- // Nothing loaded matches yet, and the host has not answered: not a "nobody matches".
115- ( candidates . length === 0 &&
116- supportsSearch &&
117- candidatesQuery . data ?. truncated === true &&
118- query . trim ( ) . length > 0 &&
119- ( searchQuery !== query . trim ( ) || searchResult . isPending ) )
120- }
122+ isPending = { ( candidatesQuery . isPending && candidatesQuery . data === null ) || searchPending }
121123 error = {
122124 candidatesQuery . data === null
123125 ? candidatesQuery . error
124- : searched && candidates . length === 0
125- ? searchResult . error
126+ : candidates . length === 0
127+ ? searchError
126128 : null
127129 }
128130 candidates = { candidates }
129131 emptyLabel = "Nobody else has access to this repository."
130132 noMatchLabel = "Nobody with access matches that."
131133 errorLabel = "The people with access could not be read."
132- truncated = {
133- candidatesQuery . data ?. truncated === true &&
134- ( ! supportsSearch ||
135- query . trim ( ) . length === 0 ||
136- ( searched && ( searchResult . error !== null || searchResult . data ?. truncated === true ) ) )
137- }
138- truncatedLabel = {
139- ! supportsSearch
140- ? "The provider has more reviewers than are listed here. Find additional reviewers on the provider."
141- : searched && searchResult . error !== null
142- ? `Only the people already listed are matched: the host search failed. ${ searchResult . error } `
143- : searched
144- ? "More people match than the host returned. Keep typing to narrow it down."
145- : "This repository has more people with access than are listed here. Search to find them."
146- }
134+ truncated = { truncation !== null }
135+ truncatedLabel = { truncation === null ? "" : TRUNCATED_LABELS [ truncation ] ( searchError ) }
147136 candidateKey = { ( candidate ) => `${ candidate . kind } :${ candidate . id } ` }
148137 disabled = { pending !== null }
149138 onSelect = { ( candidate ) => void toggle ( candidate ) }
0 commit comments