1+ import type {
2+ ContextMenuItem as TreeContextMenuItem ,
3+ ContextMenuOpenContext as TreeContextMenuOpenContext ,
4+ } from "@pierre/trees" ;
15import type { EnvironmentId , ProjectEntry } from "@t3tools/contracts" ;
26import { FileTree , useFileTree } from "@pierre/trees/react" ;
7+ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger" ;
38import { RefreshCw , Search } from "lucide-react" ;
49import { useEffect , useMemo , useRef } from "react" ;
510
11+ import { toastManager } from "~/components/ui/toast" ;
12+ import { useComposerHandleContext } from "~/composerHandleContext" ;
13+ import { writeTextToClipboard } from "~/hooks/useCopyToClipboard" ;
614import { useTheme } from "~/hooks/useTheme" ;
715import { cn } from "~/lib/utils" ;
16+ import { readLocalApi } from "~/localApi" ;
817import { T3_PIERRE_ICONS } from "~/pierre-icons" ;
918
1019import { useProjectEntriesQuery } from "./projectFilesQueryState" ;
@@ -39,6 +48,7 @@ export default function FileBrowserPanel({
3948 onOpenFile,
4049} : FileBrowserPanelProps ) {
4150 const { resolvedTheme } = useTheme ( ) ;
51+ const composerRef = useComposerHandleContext ( ) ;
4252 const entriesQuery = useProjectEntriesQuery ( environmentId , cwd ) ;
4353 const entries = entriesQuery . data ?. entries ?? [ ] ;
4454 const entryKinds = useMemo (
@@ -49,7 +59,93 @@ export default function FileBrowserPanel({
4959 const treePaths = useMemo ( ( ) => entries . map ( treePath ) , [ entries ] ) ;
5060 const previousTreePathsRef = useRef < readonly string [ ] > ( [ ] ) ;
5161
62+ // The tree renders rows in shadow DOM and its anchor rect is unreliable, so
63+ // capture the right-click position ourselves; contextmenu is a composed
64+ // event, so a capture-phase listener sees it with viewport coordinates.
65+ const contextMenuPointerRef = useRef < { x : number ; y : number ; at : number } | null > ( null ) ;
66+ useEffect ( ( ) => {
67+ const capturePointer = ( event : MouseEvent ) => {
68+ contextMenuPointerRef . current = { x : event . clientX , y : event . clientY , at : event . timeStamp } ;
69+ } ;
70+ document . addEventListener ( "contextmenu" , capturePointer , true ) ;
71+ return ( ) => document . removeEventListener ( "contextmenu" , capturePointer , true ) ;
72+ } , [ ] ) ;
73+
74+ const showEntryContextMenu = async (
75+ item : TreeContextMenuItem ,
76+ context : TreeContextMenuOpenContext ,
77+ ) => {
78+ const api = readLocalApi ( ) ;
79+ if ( ! api ) {
80+ context . close ( ) ;
81+ return ;
82+ }
83+ const relativePath = item . path . replace ( / \/ $ / , "" ) ;
84+ const mention = serializeComposerFileLink ( relativePath ) ;
85+ const pointer = contextMenuPointerRef . current ;
86+ const pointerIsFresh = pointer !== null && performance . now ( ) - pointer . at < 1000 ;
87+ const anchorRect = context . anchorElement . getBoundingClientRect ( ) ;
88+ const position = pointerIsFresh
89+ ? { x : pointer . x , y : pointer . y }
90+ : { x : anchorRect . left , y : anchorRect . bottom } ;
91+ try {
92+ const clicked = await api . contextMenu . show (
93+ [
94+ { id : "copy-mention" , label : "Copy mention" } ,
95+ { id : "add-to-chat" , label : "Add to chat" } ,
96+ ] ,
97+ position ,
98+ ) ;
99+ if ( clicked === "copy-mention" ) {
100+ try {
101+ await writeTextToClipboard ( mention ) ;
102+ toastManager . add ( { type : "success" , title : "Mention copied" , description : relativePath } ) ;
103+ } catch ( error ) {
104+ toastManager . add ( {
105+ type : "error" ,
106+ title : "Failed to copy mention" ,
107+ description : error instanceof Error ? error . message : "An error occurred." ,
108+ } ) ;
109+ }
110+ return ;
111+ }
112+ if ( clicked === "add-to-chat" ) {
113+ const composer = composerRef ?. current ;
114+ if ( ! composer ) {
115+ toastManager . add ( {
116+ type : "error" ,
117+ title : "Unable to add to chat" ,
118+ description : "Open a chat for this project and try again." ,
119+ } ) ;
120+ return ;
121+ }
122+ const inserted = composer . insertTextAtEnd ( `${ mention } ` , { ensureLeadingBoundary : true } ) ;
123+ if ( ! inserted ) {
124+ toastManager . add ( {
125+ type : "error" ,
126+ title : "Unable to add to chat" ,
127+ description : "The chat isn't ready to accept input right now." ,
128+ } ) ;
129+ }
130+ }
131+ } finally {
132+ context . close ( ) ;
133+ }
134+ } ;
135+ const showEntryContextMenuRef = useRef ( showEntryContextMenu ) ;
136+ useEffect ( ( ) => {
137+ showEntryContextMenuRef . current = showEntryContextMenu ;
138+ } ) ;
139+
52140 const { model } = useFileTree ( {
141+ composition : {
142+ contextMenu : {
143+ triggerMode : "right-click" ,
144+ onOpen : ( item , context ) => {
145+ void showEntryContextMenuRef . current ( item , context ) ;
146+ } ,
147+ } ,
148+ } ,
53149 density : "compact" ,
54150 fileTreeSearchMode : "hide-non-matches" ,
55151 flattenEmptyDirectories : true ,
0 commit comments