1+ /* eslint-disable require-jsdoc */
12import {
23 Avatar ,
4+ createStyles ,
35 Icon ,
46 IconButton ,
57 ListItem ,
68 ListItemAvatar ,
79 ListItemIcon ,
810 ListItemSecondaryAction ,
911 ListItemText ,
10- Table ,
11- TableBody ,
12- TableCell ,
12+ makeStyles ,
1313} from '@material-ui/core'
1414import { InsertDriveFile } from '@material-ui/icons'
1515import { Repository } from '@sensenet/client-core'
@@ -18,6 +18,42 @@ import { GenericContent, Image, User } from '@sensenet/default-content-types'
1818import React from 'react'
1919import { renderIconDefault } from '../icon'
2020
21+ export type PathConfig = {
22+ appPath : string
23+ snPath ?: string
24+ }
25+
26+ export type Paths = Record < string , PathConfig >
27+
28+ export function getAppPathAndContent ( PATHS : Paths , targetPath : string ) {
29+ const matches = Object . entries ( PATHS )
30+ . filter ( ( [ , config ] ) => config . snPath && targetPath . startsWith ( config . snPath ) )
31+ . sort ( ( a , b ) => b [ 1 ] . snPath ! . length - a [ 1 ] . snPath ! . length )
32+
33+ if ( ! matches [ 0 ] ) return undefined
34+
35+ const [ , config ] = matches [ 0 ]
36+ const contentePath = targetPath . substring ( config . snPath ! . length )
37+
38+ return {
39+ appPath : config . appPath ,
40+ contentePath,
41+ }
42+ }
43+
44+ export function buildCustomPath ( path : string , action : string | undefined , contentePath : string ) {
45+ const customPath = path
46+ . replace ( ':browseType' , 'explorer' )
47+ . replace ( '/:path' , '' ) // Remove the path parameter
48+ . replace ( ':action?' , action || 'default' )
49+
50+ const url = new URL ( window . location . origin )
51+ url . pathname = customPath
52+ url . searchParams . set ( 'content' , contentePath )
53+
54+ return url . toString ( )
55+ }
56+
2157interface DefaultItemTemplateProps {
2258 content : GenericContent
2359 remove ?: ( id : number ) => void
@@ -27,13 +63,29 @@ interface DefaultItemTemplateProps {
2763 repository ?: Repository
2864 multiple : boolean
2965 renderIcon ?: ( name : string ) => JSX . Element
66+ paths : Paths
3067}
3168
69+ const useStyles = makeStyles ( ( ) =>
70+ createStyles ( {
71+ referenceItemText : {
72+ textAlign : 'left' ,
73+ paddingRight : 15 ,
74+ cursor : 'pointer' ,
75+ '&[data-clickable="true"]:hover' : {
76+ textDecoration : 'underline' ,
77+ } ,
78+ } ,
79+ } ) ,
80+ )
81+
3282/**
3383 * Represents a default renderer for reference grid row
3484 */
3585export const DefaultItemTemplate : React . FC < DefaultItemTemplateProps > = ( props ) => {
36- const { content, repository } = props
86+ const { content, repository, paths } = props
87+
88+ const classes = useStyles ( )
3789
3890 const renderIcon = ( item : GenericContent | User | Image ) => {
3991 if ( repository ?. schemas . isContentFromType < User > ( item , 'User' ) ) {
@@ -111,26 +163,24 @@ export const DefaultItemTemplate: React.FC<DefaultItemTemplateProps> = (props) =
111163 < ListItem style = { props . actionName === 'browse' ? { padding : 0 } : undefined } key = { content . Id } button = { false } >
112164 { content . Type ? renderIcon ( content ) : null }
113165 < ListItemText
114- primary = {
115- content . Path ?. trim ( ) === '' ? (
116- content . DisplayName
117- ) : (
118- < Table >
119- < TableBody >
120- < TableCell component = "th" title = { content . Path } scope = "row" >
121- { content . Path }
122- </ TableCell >
123- < TableCell component = "th" scope = "row" >
124- { content . DisplayName }
125- </ TableCell >
126- < TableCell component = "th" scope = "row" >
127- { content . Type }
128- </ TableCell >
129- </ TableBody >
130- </ Table >
131- )
132- }
133- style = { { textAlign : 'left' , paddingRight : 15 } }
166+ onClick = { ( ) => {
167+ if ( content . Id === - 1 || ! paths ) {
168+ return
169+ }
170+ const referencedItemPaths = getAppPathAndContent ( paths , content . Path )
171+
172+ if ( ! referencedItemPaths ) {
173+ return
174+ }
175+
176+ const { appPath, contentePath } = referencedItemPaths
177+
178+ const fullUrl = buildCustomPath ( appPath , props . actionName , contentePath )
179+ window . location . href = fullUrl
180+ } }
181+ primary = { content . DisplayName }
182+ className = { classes . referenceItemText }
183+ data-clickable = { content . Id !== - 1 }
134184 />
135185 { props . actionName && props . actionName !== 'browse' && ! props . readOnly ? (
136186 < ListItemSecondaryAction >
0 commit comments