@@ -4,35 +4,45 @@ import type {
44 PullRequestRef ,
55 PullRequestReviewerCandidate ,
66} from "@t3tools/contracts" ;
7+ import {
8+ hostReviewerQuery ,
9+ reviewerCandidateView ,
10+ type ReviewerTruncation ,
11+ } from "@t3tools/client-runtime/pull-request-reviewers" ;
712import { CheckIcon , UserPlusIcon } from "lucide-react" ;
813import { useState } from "react" ;
914
1015import { pullRequestEnvironment } from "~/state/pullRequests" ;
1116import { useEnvironmentQuery } from "~/state/query" ;
17+ import { useDebouncedValue } from "~/state/queries" ;
1218import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
1319
1420import { toastManager } from "../ui/toast" ;
1521import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker" ;
1622import { PullRequestActorLabel } from "./pullRequestPresentation" ;
1723import { readableFailure } from "./pullRequestDetail.logic" ;
1824
19- function matches ( candidate : PullRequestReviewerCandidate , query : string ) : boolean {
20- if ( query . length === 0 ) return true ;
21- const needle = query . toLowerCase ( ) ;
22- return (
23- candidate . login . toLowerCase ( ) . includes ( needle ) ||
24- ( candidate . name ?? "" ) . toLowerCase ( ) . includes ( needle )
25- ) ;
26- }
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+ } ;
2735
2836export function PullRequestReviewerPicker ( {
2937 environmentId,
3038 reference,
3139 allowed,
40+ searchable,
3241} : {
3342 environmentId : EnvironmentId ;
3443 reference : PullRequestRef ;
3544 allowed : boolean ;
45+ searchable : boolean ;
3646} ) {
3747 const [ open , setOpen ] = useState ( false ) ;
3848 const [ query , setQuery ] = useState ( "" ) ;
@@ -41,13 +51,26 @@ export function PullRequestReviewerPicker({
4151 const candidatesQuery = useEnvironmentQuery (
4252 open ? pullRequestEnvironment . reviewerCandidates ( { environmentId, input : reference } ) : null ,
4353 ) ;
54+ // Past the first page the host is asked, since what was loaded cannot answer for the rest.
55+ const debouncedQuery = useDebouncedValue ( query . trim ( ) , 250 ) ;
56+ const searchInput = { loaded : candidatesQuery . data , searchable, query, debouncedQuery } ;
57+ const hostQuery = hostReviewerQuery ( searchInput ) ;
58+ const searchResult = useEnvironmentQuery (
59+ open && hostQuery !== null
60+ ? pullRequestEnvironment . reviewerSearch ( {
61+ environmentId,
62+ input : { ...reference , query : hostQuery } ,
63+ } )
64+ : null ,
65+ ) ;
4466 const requestReviewers = useAtomCommand ( pullRequestEnvironment . requestReviewers , {
4567 reportFailure : false ,
4668 } ) ;
4769
48- const candidates = ( candidatesQuery . data ?. candidates ?? [ ] ) . filter ( ( entry ) =>
49- matches ( entry , query ) ,
50- ) ;
70+ const { candidates, searchPending, searchError, truncation } = reviewerCandidateView ( {
71+ ...searchInput ,
72+ search : searchResult ,
73+ } ) ;
5174
5275 const toggle = async ( candidate : PullRequestReviewerCandidate ) => {
5376 if ( pending !== null ) return ;
@@ -74,6 +97,9 @@ export function PullRequestReviewerPicker({
7497 } ) ;
7598 return ;
7699 }
100+ // The command reads the loaded list again only for somebody it lacked, and a search result
101+ // is keyed by what was typed, so the one on screen is read here or it keeps the old request.
102+ if ( hostQuery !== null ) searchResult . refresh ( ) ;
77103 toastManager . add ( {
78104 type : "success" ,
79105 title : candidate . isRequested
@@ -93,14 +119,20 @@ export function PullRequestReviewerPicker({
93119 query = { query }
94120 onQueryChange = { setQuery }
95121 searchLabel = "Search people with access"
96- isPending = { candidatesQuery . isPending && candidatesQuery . data === null }
97- error = { candidatesQuery . data === null ? candidatesQuery . error : null }
122+ isPending = { ( candidatesQuery . isPending && candidatesQuery . data === null ) || searchPending }
123+ error = {
124+ candidatesQuery . data === null
125+ ? candidatesQuery . error
126+ : candidates . length === 0
127+ ? searchError
128+ : null
129+ }
98130 candidates = { candidates }
99131 emptyLabel = "Nobody else has access to this repository."
100132 noMatchLabel = "Nobody with access matches that."
101133 errorLabel = "The people with access could not be read."
102- truncated = { candidatesQuery . data ?. truncated === true }
103- truncatedLabel = "This repository has more people with access than are listed here. Ask for the rest on the host."
134+ truncated = { truncation !== null }
135+ truncatedLabel = { truncation === null ? "" : TRUNCATED_LABELS [ truncation ] ( searchError ) }
104136 candidateKey = { ( candidate ) => `${ candidate . kind } :${ candidate . id } ` }
105137 disabled = { pending !== null }
106138 onSelect = { ( candidate ) => void toggle ( candidate ) }
0 commit comments