@@ -5,7 +5,7 @@ import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime/envir
55import { isScratchProject } from "@t3tools/client-runtime/state/projects" ;
66import { FolderPlusIcon , MessageSquareDashedIcon } from "lucide-react" ;
77import { useAtomValue } from "@effect/atom-react" ;
8- import { useCallback , useEffect , useMemo , useRef } from "react" ;
8+ import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
99
1010import { openCommandPalette } from "~/commandPaletteBus" ;
1111import { shortcutLabelForCommand } from "~/keybindings" ;
@@ -29,20 +29,28 @@ import { ProjectEnvironmentBadge } from "../ProjectEnvironmentBadge";
2929import { ProjectFavicon } from "../ProjectFavicon" ;
3030import { sortLogicalProjectsForSidebar } from "../Sidebar.logic" ;
3131import {
32- Menu ,
33- MenuItem ,
34- MenuPopup ,
35- MenuRadioGroup ,
36- MenuRadioItem ,
37- MenuSeparator ,
38- MenuTrigger ,
39- } from "../ui/menu" ;
32+ Combobox ,
33+ ComboboxEmpty ,
34+ ComboboxItem ,
35+ ComboboxList ,
36+ ComboboxPopup ,
37+ ComboboxSearchInput ,
38+ ComboboxTrigger ,
39+ useComboboxFilter ,
40+ } from "../ui/combobox" ;
4041import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
4142import { InlineButton } from "../ui/button" ;
4243import { resolveProjectSettings } from "@t3tools/shared/projectSettings" ;
4344
44- // Menu value for "No project"; real entries are keyed by logical project key.
45+ // Picker values for the non-project rows; real entries are keyed by logical
46+ // project key.
4547const NO_PROJECT_VALUE = "no-project" ;
48+ const ADD_PROJECT_VALUE = "add-project" ;
49+
50+ interface PickerItem {
51+ readonly value : string ;
52+ readonly label : string ;
53+ }
4654
4755interface DraftHeroHeadlineProps {
4856 readonly draftId : DraftId | null ;
@@ -147,9 +155,13 @@ export function DraftHeroHeadline({
147155 const shouldShowProjectMenu = canChooseProject ;
148156 // The project that hosts threads without a project appears once, as the
149157 // "No project" item, not as a project row.
150- const menuEntries = projectPickerEntries . filter (
151- ( { targetProject } ) =>
152- ! isScratchProject ( targetProject , scratchWorkspaceRootFor ( targetProject . environmentId ) ) ,
158+ const menuEntries = useMemo (
159+ ( ) =>
160+ projectPickerEntries . filter (
161+ ( { targetProject } ) =>
162+ ! isScratchProject ( targetProject , scratchWorkspaceRootFor ( targetProject . environmentId ) ) ,
163+ ) ,
164+ [ projectPickerEntries , scratchWorkspaceRootFor ] ,
153165 ) ;
154166 const activeProject =
155167 activeProjectRef === null
@@ -166,6 +178,32 @@ export function DraftHeroHeadline({
166178 const isScratchDraft =
167179 activeProject !== null && isScratchProject ( activeProject , scratchWorkspaceRoot ) ;
168180
181+ // {value, label} items let Base UI drive the combobox selection while the
182+ // popup search filters the same collection. "Add project" is an action, not
183+ // a project, so it only trails the unfiltered list.
184+ const pickerItems = useMemo < readonly PickerItem [ ] > (
185+ ( ) => [
186+ ...( scratchWorkspaceRoot === null ? [ ] : [ { value : NO_PROJECT_VALUE , label : "No project" } ] ) ,
187+ ...menuEntries . map ( ( { group } ) => ( { value : group . projectKey , label : group . displayName } ) ) ,
188+ { value : ADD_PROJECT_VALUE , label : "Add project" } ,
189+ ] ,
190+ [ menuEntries , scratchWorkspaceRoot ] ,
191+ ) ;
192+ const [ pickerOpen , setPickerOpen ] = useState ( false ) ;
193+ const [ pickerQuery , setPickerQuery ] = useState ( "" ) ;
194+ const pickerFilter = useComboboxFilter ( ) ;
195+ const filteredPickerItems = useMemo ( ( ) => {
196+ const query = pickerQuery . trim ( ) ;
197+ if ( query . length === 0 ) return pickerItems ;
198+ return pickerItems . filter (
199+ ( item ) =>
200+ item . value !== ADD_PROJECT_VALUE &&
201+ pickerFilter . contains ( item , query , ( candidate ) => candidate . label ) ,
202+ ) ;
203+ } , [ pickerFilter , pickerItems , pickerQuery ] ) ;
204+ const selectedPickerValue = isScratchDraft ? NO_PROJECT_VALUE : activeProjectKey ;
205+ const selectedPickerItem = pickerItems . find ( ( item ) => item . value === selectedPickerValue ) ?? null ;
206+
169207 // The picker can change the draft's target while the no-project home is
170208 // still being opened; a stale continuation must not retarget it again.
171209 const latestTargetRef = useRef ( { draftId, activeProjectKey, scratchTargetEnvironmentId } ) ;
@@ -226,15 +264,44 @@ export function DraftHeroHeadline({
226264 } ;
227265
228266 const projectSelector = shouldShowProjectMenu ? (
229- < Menu >
267+ < Combobox
268+ items = { pickerItems }
269+ filteredItems = { filteredPickerItems }
270+ autoHighlight
271+ itemToStringLabel = { ( item ) => item . label }
272+ isItemEqualToValue = { ( a , b ) => a . value === b . value }
273+ open = { pickerOpen }
274+ onOpenChange = { ( open ) => {
275+ setPickerOpen ( open ) ;
276+ setPickerQuery ( "" ) ;
277+ } }
278+ value = { selectedPickerItem }
279+ onValueChange = { ( item ) => {
280+ if ( ! item ) return ;
281+ setPickerOpen ( false ) ;
282+ if ( item . value === ADD_PROJECT_VALUE ) {
283+ openAddProject ( ) ;
284+ return ;
285+ }
286+ if ( item . value === NO_PROJECT_VALUE ) {
287+ void startScratch ( ) ;
288+ return ;
289+ }
290+ const entry = projectEntryByKey . get ( item . value ) ;
291+ if ( ! entry || item . value === activeProjectKey ) {
292+ return ;
293+ }
294+ selectProject ( entry . targetProject , entry . group . projectKey ) ;
295+ } }
296+ >
230297 < Tooltip >
231298 < TooltipTrigger
232299 render = {
233300 // The trigger's accessible name comes from its visible text (the
234301 // project title) so the hero sentence reads naturally: an
235302 // aria-label here would replace the title with an action phrase
236303 // mid-sentence and baffle screen-reader users.
237- < MenuTrigger
304+ < ComboboxTrigger
238305 render = { < InlineButton tone = "picker" /> }
239306 data-draft-project-trigger = ""
240307 className = "pointer-events-auto max-w-64 align-baseline"
@@ -249,65 +316,46 @@ export function DraftHeroHeadline({
249316 < TooltipPopup side = "top" > { activeProjectDisplayName } </ TooltipPopup >
250317 ) : null }
251318 </ Tooltip >
252- < MenuPopup align = "center" className = "max-h-80 overflow-y-auto" >
253- < MenuRadioGroup
254- value = { isScratchDraft ? NO_PROJECT_VALUE : activeProjectKey }
255- onValueChange = { ( value ) => {
256- if ( value === NO_PROJECT_VALUE ) {
257- void startScratch ( ) ;
258- return ;
259- }
260- const entry = projectEntryByKey . get ( value as string ) ;
261- if ( ! entry || value === activeProjectKey ) {
262- return ;
263- }
264- selectProject ( entry . targetProject , entry . group . projectKey ) ;
265- } }
266- >
267- { scratchWorkspaceRoot === null ? null : (
268- < MenuRadioItem value = { NO_PROJECT_VALUE } closeOnClick >
269- < span className = "flex min-w-0 items-center gap-2" >
270- { /* Boxed like ProjectFavicon so the label lines up with project rows. */ }
271- < span
272- aria-hidden = "true"
273- className = { `inline-flex size-4 shrink-0 ${ projectIconColorClassName ( "gray" ) } ` }
274- >
275- < MessageSquareDashedIcon className = "size-full" />
276- </ span >
277- No project
278- </ span >
279- </ MenuRadioItem >
280- ) }
281- { menuEntries . map ( ( { group } ) => {
319+ < ComboboxPopup align = "center" className = "w-72 overflow-hidden" >
320+ < ComboboxSearchInput
321+ aria-label = "Search projects"
322+ placeholder = "Search projects..."
323+ value = { pickerQuery }
324+ onChange = { ( event ) => setPickerQuery ( event . target . value ) }
325+ />
326+ < ComboboxEmpty > No matching projects.</ ComboboxEmpty >
327+ < ComboboxList >
328+ { ( item : PickerItem ) => {
329+ const entry = projectEntryByKey . get ( item . value ) ;
282330 return (
283- < MenuRadioItem key = { group . projectKey } value = { group . projectKey } closeOnClick >
284- < span className = "flex min-w-0 items-center gap-2" >
285- < ProjectFavicon project = { group } className = "size-4 shrink-0" />
286- < Tooltip >
287- < TooltipTrigger render = { < span className = "block min-w-0 truncate" /> } >
288- { group . displayName }
289- </ TooltipTrigger >
290- < TooltipPopup side = "top" > { group . displayName } </ TooltipPopup >
291- </ Tooltip >
292- { showProjectEnvironments ? (
293- < ProjectEnvironmentBadge
294- group = { group }
295- primaryEnvironmentId = { primaryEnvironmentId }
296- machineByEnvironmentId = { environmentMachineById }
297- />
298- ) : null }
299- </ span >
300- </ MenuRadioItem >
331+ < ComboboxItem key = { item . value } hideIndicator value = { item } >
332+ { item . value === ADD_PROJECT_VALUE ? (
333+ < FolderPlusIcon className = "size-4 shrink-0" />
334+ ) : item . value === NO_PROJECT_VALUE ? (
335+ // Boxed like ProjectFavicon so the label lines up with project rows.
336+ < span
337+ aria-hidden = "true"
338+ className = { `inline-flex size-4 shrink-0 ${ projectIconColorClassName ( "gray" ) } ` }
339+ >
340+ < MessageSquareDashedIcon className = "size-full" />
341+ </ span >
342+ ) : entry ? (
343+ < ProjectFavicon project = { entry . group } className = "size-4 shrink-0" />
344+ ) : null }
345+ < span className = "min-w-0 flex-1 truncate text-sm" > { item . label } </ span >
346+ { entry && showProjectEnvironments ? (
347+ < ProjectEnvironmentBadge
348+ group = { entry . group }
349+ primaryEnvironmentId = { primaryEnvironmentId }
350+ machineByEnvironmentId = { environmentMachineById }
351+ />
352+ ) : null }
353+ </ ComboboxItem >
301354 ) ;
302- } ) }
303- </ MenuRadioGroup >
304- { projectPickerEntries . length > 0 ? < MenuSeparator /> : null }
305- < MenuItem onClick = { openAddProject } >
306- < FolderPlusIcon />
307- Add project
308- </ MenuItem >
309- </ MenuPopup >
310- </ Menu >
355+ } }
356+ </ ComboboxList >
357+ </ ComboboxPopup >
358+ </ Combobox >
311359 ) : (
312360 < button
313361 type = "button"
0 commit comments