1- import React , { useEffect , useMemo , useRef } from "react"
1+ import React , { useEffect , useMemo , useRef , useState } from "react"
2+ import { getIconForFilePath , getIconUrlByName , getIconForDirectoryPath } from "vscode-material-icons"
23import {
34 ContextMenuOptionType ,
45 ContextMenuQueryItem ,
@@ -33,6 +34,7 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
3334 loading = false ,
3435 dynamicSearchResults = [ ] ,
3536} ) => {
37+ const [ materialIconsBaseUri , setMaterialIconsBaseUri ] = useState ( "" )
3638 const menuRef = useRef < HTMLDivElement > ( null )
3739
3840 const filteredOptions = useMemo ( ( ) => {
@@ -55,6 +57,12 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
5557 }
5658 } , [ selectedIndex ] )
5759
60+ // get the icons base uri on mount
61+ useEffect ( ( ) => {
62+ const w = window as any
63+ setMaterialIconsBaseUri ( w . MATERIAL_ICONS_BASE_URI )
64+ } , [ ] )
65+
5866 const renderOptionContent = ( option : ContextMenuQueryItem ) => {
5967 switch ( option . type ) {
6068 case ContextMenuOptionType . Mode :
@@ -173,6 +181,15 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
173181 }
174182 }
175183
184+ const getMaterialIconForOption = ( option : ContextMenuQueryItem ) : string => {
185+ // only take the last part of the path to handle both file and folder icons
186+ // since material-icons have specific folder icons, we use them if available
187+ const name = option . value ?. split ( "/" ) . filter ( Boolean ) . at ( - 1 ) ?? ""
188+ const iconName =
189+ option . type === ContextMenuOptionType . Folder ? getIconForDirectoryPath ( name ) : getIconForFilePath ( name )
190+ return getIconUrlByName ( iconName , materialIconsBaseUri )
191+ }
192+
176193 const isOptionSelectable = ( option : ContextMenuQueryItem ) : boolean => {
177194 return option . type !== ContextMenuOptionType . NoResults && option . type !== ContextMenuOptionType . URL
178195 }
@@ -229,17 +246,35 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
229246 overflow : "hidden" ,
230247 paddingTop : 0 ,
231248 } } >
232- { option . type !== ContextMenuOptionType . Mode && getIconForOption ( option ) && (
233- < i
234- className = { `codicon codicon-${ getIconForOption ( option ) } ` }
249+ { ( option . type === ContextMenuOptionType . File ||
250+ option . type === ContextMenuOptionType . Folder ||
251+ option . type === ContextMenuOptionType . OpenedFile ) && (
252+ < img
253+ src = { getMaterialIconForOption ( option ) }
254+ alt = "Mode"
235255 style = { {
236256 marginRight : "6px" ,
237257 flexShrink : 0 ,
238- fontSize : "14px " ,
239- marginTop : 0 ,
258+ width : "16px " ,
259+ height : "16px" ,
240260 } }
241261 />
242262 ) }
263+ { option . type !== ContextMenuOptionType . Mode &&
264+ option . type !== ContextMenuOptionType . File &&
265+ option . type !== ContextMenuOptionType . Folder &&
266+ option . type !== ContextMenuOptionType . OpenedFile &&
267+ getIconForOption ( option ) && (
268+ < i
269+ className = { `codicon codicon-${ getIconForOption ( option ) } ` }
270+ style = { {
271+ marginRight : "6px" ,
272+ flexShrink : 0 ,
273+ fontSize : "14px" ,
274+ marginTop : 0 ,
275+ } }
276+ />
277+ ) }
243278 { renderOptionContent ( option ) }
244279 </ div >
245280 { ( option . type === ContextMenuOptionType . File ||
@@ -251,18 +286,6 @@ const ContextMenu: React.FC<ContextMenuProps> = ({
251286 style = { { fontSize : "10px" , flexShrink : 0 , marginLeft : 8 } }
252287 />
253288 ) }
254- { ( option . type === ContextMenuOptionType . Problems ||
255- option . type === ContextMenuOptionType . Terminal ||
256- ( ( option . type === ContextMenuOptionType . File ||
257- option . type === ContextMenuOptionType . Folder ||
258- option . type === ContextMenuOptionType . OpenedFile ||
259- option . type === ContextMenuOptionType . Git ) &&
260- option . value ) ) && (
261- < i
262- className = "codicon codicon-add"
263- style = { { fontSize : "10px" , flexShrink : 0 , marginLeft : 8 } }
264- />
265- ) }
266289 </ div >
267290 ) )
268291 ) : (
0 commit comments