@@ -5,9 +5,12 @@ import type {
55} from "@pierre/trees" ;
66import type { EnvironmentId , ProjectEntry } from "@t3tools/contracts" ;
77import { FileTree , useFileTree , useFileTreeSearch , useFileTreeSelector } from "@pierre/trees/react" ;
8+ import { settlePromise } from "@t3tools/client-runtime/state/runtime" ;
89import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger" ;
10+ import * as AsyncResult from "effect/reactivity/AsyncResult" ;
11+ import * as Cause from "effect/Cause" ;
912import { ChevronsDownUp , ChevronsUpDown } from "lucide" ;
10- import { useEffect , useMemo , useRef , useState } from "react" ;
13+ import { useEffect , useRef , useState , type RefObject } from "react" ;
1114
1215import { Button } from "~/components/ui/button" ;
1316import { InputGroup , InputGroupInput } from "~/components/ui/input-group" ;
@@ -16,6 +19,7 @@ import { toastManager } from "~/components/ui/toast";
1619import { Tooltip , TooltipPopup , TooltipTrigger } from "~/components/ui/tooltip" ;
1720import { useComposerHandleContext } from "~/composerHandleContext" ;
1821import { writeTextToClipboard } from "~/hooks/useCopyToClipboard" ;
22+ import { useStableCallback } from "~/hooks/useStableCallback" ;
1923import { useTheme } from "~/hooks/useTheme" ;
2024import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh" ;
2125import { useFileContextMenu , type FileContextMenuAction } from "~/fileContextMenu" ;
@@ -101,6 +105,47 @@ function FileSearchField(props: {
101105 ) ;
102106}
103107
108+ // The tree diffs `treePaths` by identity, so these derivations must stay cached
109+ // across the hooks that read them.
110+ function useTreeEntries (
111+ directoryEntries : readonly ProjectEntry [ ] ,
112+ searchEntries : readonly ProjectEntry [ ] ,
113+ searchPending : boolean ,
114+ query : string ,
115+ ) {
116+ "use memo" ;
117+ const result = new Map ( directoryEntries . map ( ( entry ) => [ entry . path , entry ] ) ) ;
118+ if ( query . trim ( ) && ! searchPending ) {
119+ for ( const entry of searchEntries ) {
120+ if ( ! result . has ( entry . path ) ) result . set ( entry . path , entry ) ;
121+ const segments = entry . path . split ( "/" ) ;
122+ for ( let index = 1 ; index < segments . length ; index ++ ) {
123+ const path = segments . slice ( 0 , index ) . join ( "/" ) ;
124+ if ( ! result . has ( path ) ) result . set ( path , { path, kind : "directory" } ) ;
125+ }
126+ }
127+ }
128+ const entries = [ ...result . values ( ) ] ;
129+ const entryKinds : ReadonlyMap < string , ProjectEntry [ "kind" ] > = new Map (
130+ entries . map ( ( entry ) => [ entry . path , entry . kind ] as const ) ,
131+ ) ;
132+ const treePaths = entries . map ( treePath ) ;
133+ const directoryPaths = entries . filter ( ( entry ) => entry . kind === "directory" ) . map ( treePath ) ;
134+ return { entries, entryKinds, treePaths, directoryPaths } ;
135+ }
136+
137+ function useDragMentionController (
138+ cwd : string ,
139+ hostFolder : boolean ,
140+ treeModelRef : RefObject < ReturnType < typeof useFileTree > [ "model" ] | null > ,
141+ ) {
142+ "use memo" ;
143+ return createFileTreeDragMentionController ( {
144+ deselect : ( path ) => treeModelRef . current ?. getItem ( path ) ?. deselect ( ) ,
145+ ...( hostFolder ? { mentionPath : ( path : string ) => joinHostPath ( cwd , path ) } : { } ) ,
146+ } ) ;
147+ }
148+
104149export default function FileBrowserPanel ( {
105150 environmentId,
106151 cwd,
@@ -132,30 +177,13 @@ export default function FileBrowserPanel({
132177 { environmentId, cwd : hostFolder ? null : cwd , query : query . slice ( 0 , 256 ) } ,
133178 200 ,
134179 ) ;
135- const entries = useMemo ( ( ) => {
136- const result = new Map ( directoryEntries . map ( ( entry ) => [ entry . path , entry ] ) ) ;
137- if ( query . trim ( ) && ! pathSearch . isPending ) {
138- for ( const entry of pathSearch . entries ) {
139- if ( ! result . has ( entry . path ) ) result . set ( entry . path , entry ) ;
140- const segments = entry . path . split ( "/" ) ;
141- for ( let index = 1 ; index < segments . length ; index ++ ) {
142- const path = segments . slice ( 0 , index ) . join ( "/" ) ;
143- if ( ! result . has ( path ) ) result . set ( path , { path, kind : "directory" } ) ;
144- }
145- }
146- }
147- return [ ...result . values ( ) ] ;
148- } , [ directoryEntries , pathSearch . entries , pathSearch . isPending , query ] ) ;
149- const entryKinds = useMemo (
150- ( ) => new Map ( entries . map ( ( entry ) => [ entry . path , entry . kind ] as const ) ) ,
151- [ entries ] ,
152- ) ;
153- const entryKindsRef = useRef < ReadonlyMap < string , ProjectEntry [ "kind" ] > > ( entryKinds ) ;
154- const treePaths = useMemo ( ( ) => entries . map ( treePath ) , [ entries ] ) ;
155- const directoryPaths = useMemo (
156- ( ) => entries . filter ( ( entry ) => entry . kind === "directory" ) . map ( treePath ) ,
157- [ entries ] ,
180+ const { entries, entryKinds, treePaths, directoryPaths } = useTreeEntries (
181+ directoryEntries ,
182+ pathSearch . entries ,
183+ pathSearch . isPending ,
184+ query ,
158185 ) ;
186+ const entryKindsRef = useRef ( entryKinds ) ;
159187 const previousTreePathsRef = useRef < readonly string [ ] | null > ( null ) ;
160188 const syncingSelectionRef = useRef ( false ) ;
161189 const treeSelectionPathRef = useRef < string | null > ( null ) ;
@@ -173,100 +201,94 @@ export default function FileBrowserPanel({
173201 return ( ) => document . removeEventListener ( "contextmenu" , capturePointer , true ) ;
174202 } , [ ] ) ;
175203
176- const showEntryContextMenu = async (
177- item : TreeContextMenuItem ,
178- context : TreeContextMenuOpenContext ,
179- ) => {
180- const api = readLocalApi ( ) ;
181- if ( ! api ) {
182- context . close ( ) ;
183- return ;
184- }
185- const relativePath = item . path . replace ( / \/ $ / , "" ) ;
186- const mention = serializeComposerFileLink ( panelPath ( relativePath ) ) ;
187- const pointer = contextMenuPointerRef . current ;
188- const pointerIsFresh = pointer !== null && performance . now ( ) - pointer . at < 1000 ;
189- const anchorRect = context . anchorElement . getBoundingClientRect ( ) ;
190- const position = pointerIsFresh
191- ? { x : pointer . x , y : pointer . y }
192- : { x : anchorRect . left , y : anchorRect . bottom } ;
193- const fileTarget = { environmentId, filePath : relativePath , workspaceRoot : cwd } ;
194- const fileMenuItems = fileContextMenu . buildItems ( fileTarget ) ;
195- try {
196- const clicked = await api . contextMenu . show (
197- [
198- ...fileMenuItems ,
199- { id : "copy-mention" , label : "Copy mention" } ,
200- { id : "add-to-chat" , label : "Add to chat" } ,
201- ] ,
202- position ,
203- ) ;
204- if ( clicked === null ) return ;
205- // "Open with" submenu selections report the child id ("editor:<id>"),
206- // which is not present in the top-level item list.
207- const isFileMenuAction =
208- fileMenuItems . some ( ( entry ) => entry . id === clicked ) || clicked . startsWith ( "editor:" ) ;
209- if ( isFileMenuAction ) {
210- await fileContextMenu . activate ( clicked as FileContextMenuAction , fileTarget ) ;
204+ const showEntryContextMenu = useStableCallback (
205+ ( item : TreeContextMenuItem , context : TreeContextMenuOpenContext ) => {
206+ const api = readLocalApi ( ) ;
207+ if ( ! api ) {
208+ context . close ( ) ;
211209 return ;
212210 }
213- if ( clicked === "copy-mention" ) {
214- try {
215- await writeTextToClipboard ( mention ) ;
216- toastManager . add ( { type : "success" , title : "Mention copied" , description : relativePath } ) ;
217- } catch ( error ) {
218- toastManager . add ( {
219- type : "error" ,
220- title : "Failed to copy mention" ,
221- description : error instanceof Error ? error . message : "An error occurred." ,
222- } ) ;
211+ const relativePath = item . path . replace ( / \/ $ / , "" ) ;
212+ const mention = serializeComposerFileLink ( panelPath ( relativePath ) ) ;
213+ const pointer = contextMenuPointerRef . current ;
214+ const pointerIsFresh = pointer !== null && performance . now ( ) - pointer . at < 1000 ;
215+ const anchorRect = context . anchorElement . getBoundingClientRect ( ) ;
216+ const position = pointerIsFresh
217+ ? { x : pointer . x , y : pointer . y }
218+ : { x : anchorRect . left , y : anchorRect . bottom } ;
219+ const fileTarget = { environmentId, filePath : relativePath , workspaceRoot : cwd } ;
220+ const fileMenuItems = fileContextMenu . buildItems ( fileTarget ) ;
221+ void settlePromise ( async ( ) => {
222+ const clicked = await api . contextMenu . show (
223+ [
224+ ...fileMenuItems ,
225+ { id : "copy-mention" , label : "Copy mention" } ,
226+ { id : "add-to-chat" , label : "Add to chat" } ,
227+ ] ,
228+ position ,
229+ ) ;
230+ if ( clicked === null ) return ;
231+ // "Open with" submenu selections report the child id ("editor:<id>"),
232+ // which is not present in the top-level item list.
233+ const isFileMenuAction =
234+ fileMenuItems . some ( ( entry ) => entry . id === clicked ) || clicked . startsWith ( "editor:" ) ;
235+ if ( isFileMenuAction ) {
236+ await fileContextMenu . activate ( clicked as FileContextMenuAction , fileTarget ) ;
237+ return ;
223238 }
224- return ;
225- }
226- if ( clicked === "add-to-chat" ) {
227- const composer = composerRef ?. current ;
228- if ( ! composer ) {
229- toastManager . add ( {
230- type : "error" ,
231- title : "Unable to add to chat" ,
232- description : "Open a chat for this project and try again." ,
233- } ) ;
239+ if ( clicked === "copy-mention" ) {
240+ try {
241+ await writeTextToClipboard ( mention ) ;
242+ toastManager . add ( {
243+ type : "success" ,
244+ title : "Mention copied" ,
245+ description : relativePath ,
246+ } ) ;
247+ } catch ( error ) {
248+ toastManager . add ( {
249+ type : "error" ,
250+ title : "Failed to copy mention" ,
251+ description : error instanceof Error ? error . message : "An error occurred." ,
252+ } ) ;
253+ }
234254 return ;
235255 }
236- const inserted = composer . insertTextAtEnd ( `${ mention } ` , { ensureLeadingBoundary : true } ) ;
237- if ( ! inserted ) {
238- toastManager . add ( {
239- type : "error" ,
240- title : "Unable to add to chat" ,
241- description : "The chat isn't ready to accept input right now." ,
242- } ) ;
256+ if ( clicked === "add-to-chat" ) {
257+ const composer = composerRef ?. current ;
258+ if ( ! composer ) {
259+ toastManager . add ( {
260+ type : "error" ,
261+ title : "Unable to add to chat" ,
262+ description : "Open a chat for this project and try again." ,
263+ } ) ;
264+ return ;
265+ }
266+ const inserted = composer . insertTextAtEnd ( `${ mention } ` , { ensureLeadingBoundary : true } ) ;
267+ if ( ! inserted ) {
268+ toastManager . add ( {
269+ type : "error" ,
270+ title : "Unable to add to chat" ,
271+ description : "The chat isn't ready to accept input right now." ,
272+ } ) ;
273+ }
243274 }
244- }
245- } finally {
246- context . close ( ) ;
247- }
248- } ;
249- const showEntryContextMenuRef = useRef ( showEntryContextMenu ) ;
250- useEffect ( ( ) => {
251- showEntryContextMenuRef . current = showEntryContextMenu ;
252- } ) ;
275+ } ) . then ( ( result ) => {
276+ context . close ( ) ;
277+ // Keep surfacing unexpected failures as unhandled rejections.
278+ if ( ! AsyncResult . isSuccess ( result ) ) throw Cause . squash ( result . cause ) ;
279+ } ) ;
280+ } ,
281+ ) ;
253282
254283 const loadingDirectoriesRef = useRef ( loadingDirectories ) ;
255284 const treeModelRef = useRef < ReturnType < typeof useFileTree > [ "model" ] | null > ( null ) ;
256- const dragMention = useMemo (
257- ( ) =>
258- createFileTreeDragMentionController ( {
259- deselect : ( path ) => treeModelRef . current ?. getItem ( path ) ?. deselect ( ) ,
260- ...( hostFolder ? { mentionPath : ( path : string ) => joinHostPath ( cwd , path ) } : { } ) ,
261- } ) ,
262- [ cwd , hostFolder ] ,
263- ) ;
285+ const dragMention = useDragMentionController ( cwd , hostFolder , treeModelRef ) ;
264286 const { model } = useFileTree ( {
265287 composition : {
266288 contextMenu : {
267289 triggerMode : "right-click" ,
268290 onOpen : ( item , context ) => {
269- void showEntryContextMenuRef . current ( item , context ) ;
291+ showEntryContextMenu ( item , context ) ;
270292 } ,
271293 } ,
272294 } ,
0 commit comments