@@ -2,10 +2,10 @@ import './documentNode.scss';
22
33import axios from 'axios' ;
44import React , { FunctionComponent , useContext , useEffect , useMemo , useState } from 'react' ;
5- import { Link , useHistory } from 'react-router-dom' ;
5+ import { Link , useHistory , useLocation } from 'react-router-dom' ;
66import { Icon } from 'semantic-ui-react' ;
77
8- import { TYPE_AUTOLINKED_TO , TYPE_CONTAINS , TYPE_IS_PART_OF , TYPE_RELATED } from '../../const' ;
8+ import { CRE , TYPE_AUTOLINKED_TO , TYPE_CONTAINS , TYPE_IS_PART_OF , TYPE_RELATED } from '../../const' ;
99import { useEnvironment } from '../../hooks' ;
1010import { applyFilters } from '../../hooks/applyFilters' ;
1111import { Document } from '../../types' ;
@@ -14,6 +14,7 @@ import { getApiEndpoint, getDocumentTypeText, getInternalUrl } from '../../utils
1414import { FilterButton } from '../FilterButton/FilterButton' ;
1515import { LoadingAndErrorIndicator } from '../LoadingAndErrorIndicator' ;
1616
17+ const MAX_LENGTH_FOR_AUTO_EXPAND = 5 ;
1718export interface DocumentNode {
1819 node : Document ;
1920 linkType : string ;
@@ -30,6 +31,7 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({
3031 hasLinktypeRelatedParent,
3132} ) => {
3233 const [ expanded , setExpanded ] = useState < boolean > ( false ) ;
34+ const [ showAll , setShowAll ] = useState < Record < number , boolean > > ( { } ) ;
3335 const isStandard = node . doctype in [ 'Tool' , 'Code' , 'Standard' ] ;
3436 const { apiUrl } = useEnvironment ( ) ;
3537 const [ nestedNode , setNestedNode ] = useState < Document > ( ) ;
@@ -42,6 +44,27 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({
4244 const hasExternalLink = Boolean ( usedNode . hyperlink ) ;
4345 const linksByType = useMemo ( ( ) => groupLinksByType ( usedNode ) , [ usedNode ] ) ;
4446 let currentUrlParams = new URLSearchParams ( window . location . search ) ;
47+ const { pathname } = useLocation ( ) ;
48+ const isCrePath = useMemo ( ( ) => pathname . includes ( CRE ) , [ pathname ] ) ;
49+
50+ const isNestedInRelated = hasLinktypeRelatedParent || linkType === TYPE_RELATED ;
51+
52+ const getTopicsToDisplayOrderdByLinkType = ( ) => {
53+ return Object . entries ( linksByType )
54+ . filter ( ( [ type , _ ] ) => ! linkTypesExcludedInNesting . includes ( type ) )
55+ . filter ( ( [ type , _ ] ) =>
56+ isNestedInRelated ? ! linkTypesExcludedWhenNestingRelatedTo . includes ( type ) : true
57+ ) ;
58+ } ;
59+
60+ const topicsToDisplay = useMemo ( ( ) => getTopicsToDisplayOrderdByLinkType ( ) , [ linksByType , isNestedInRelated ] ) ;
61+
62+ useEffect ( ( ) => {
63+ const isAllowedToAutoExpandByLength =
64+ topicsToDisplay . map ( ( [ , links ] ) => links ) . reduce ( ( prev , cur ) => prev . concat ( cur ) , [ ] ) . length <=
65+ MAX_LENGTH_FOR_AUTO_EXPAND ;
66+ setExpanded ( isCrePath ? isAllowedToAutoExpandByLength : false ) ;
67+ } , [ topicsToDisplay , isCrePath ] ) ;
4568
4669 useEffect ( ( ) => {
4770 if ( ! isStandard && linkTypesToNest . includes ( linkType ) ) {
@@ -66,19 +89,7 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({
6689 } ;
6790
6891 const hasActiveLinks = ( ) => {
69- return getTopicsToDisplayOrderdByLinkType ( ) . length > 0 ;
70- } ;
71-
72- const isNestedInRelated = ( ) : Boolean => {
73- return hasLinktypeRelatedParent || linkType === TYPE_RELATED ;
74- } ;
75-
76- const getTopicsToDisplayOrderdByLinkType = ( ) => {
77- return Object . entries ( linksByType )
78- . filter ( ( [ type , _ ] ) => ! linkTypesExcludedInNesting . includes ( type ) )
79- . filter ( ( [ type , _ ] ) =>
80- isNestedInRelated ( ) ? ! linkTypesExcludedWhenNestingRelatedTo . includes ( type ) : true
81- ) ;
92+ return topicsToDisplay . length > 0 ;
8293 } ;
8394
8495 const Hyperlink = ( hyperlink ) => {
@@ -134,37 +145,39 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({
134145 < div className = { `content${ active } document-node` } >
135146 < Hyperlink hyperlink = { usedNode . hyperlink } />
136147 { expanded &&
137- getTopicsToDisplayOrderdByLinkType ( ) . map ( ( [ type , links ] , idx ) => {
138- const sortedResults = links . sort ( ( a , b ) =>
148+ topicsToDisplay . map ( ( [ type , links ] , idx ) => {
149+ const sortedResults = [ ... links ] . sort ( ( a , b ) =>
139150 getDocumentDisplayName ( a . document ) . localeCompare ( getDocumentDisplayName ( b . document ) )
140151 ) ;
141- let lastDocumentName = sortedResults [ 0 ] . document . name ;
142152 return (
143- < div className = "document-node__link-type-container" key = { type } >
153+ < div className = "document-node__link-type-container" key = { ` ${ type } - ${ idx } ` } >
144154 { idx > 0 && < hr style = { { borderColor : 'transparent' , margin : '20px 0' } } /> }
145155 < div >
146156 < b > Which { getDocumentTypeText ( type , links [ 0 ] . document . doctype , node . doctype ) } </ b > :
147157 { /* Risk here of mixed doctype in here causing odd output */ }
148158 </ div >
149159 < div >
150160 < div className = "accordion ui fluid styled f0" >
151- { sortedResults . map ( ( link , i ) => {
152- const temp = (
153- < div key = { Math . random ( ) } >
154- { lastDocumentName !== link . document . name && < span style = { { margin : '5px' } } /> }
155- < DocumentNode
156- node = { link . document }
157- linkType = { type }
158- hasLinktypeRelatedParent = { isNestedInRelated ( ) }
159- key = { Math . random ( ) }
160- />
161- < FilterButton document = { link . document } />
162- </ div >
163- ) ;
164- lastDocumentName = link . document . name ;
165- return temp ;
166- } ) }
161+ { sortedResults . slice ( 0 , showAll [ idx ] ? sortedResults . length : MAX_LENGTH_FOR_AUTO_EXPAND ) . map ( ( link , i ) => (
162+ < div key = { `document-node-container-${ type } -${ idx } -${ i } ` } style = { { marginBottom : '4px' } } >
163+ < DocumentNode
164+ node = { link . document }
165+ linkType = { type }
166+ hasLinktypeRelatedParent = { isNestedInRelated as boolean }
167+ key = { `document-sub-node-${ type } -${ idx } -${ i } ` }
168+ />
169+ < FilterButton document = { link . document } />
170+ </ div >
171+ ) ) }
167172 </ div >
173+ { sortedResults . length > MAX_LENGTH_FOR_AUTO_EXPAND && (
174+ < button
175+ onClick = { ( ) => setShowAll ( prev => ( { ...prev , [ idx ] : ! prev [ idx ] } ) ) }
176+ style = { { marginTop : '8px' , cursor : 'pointer' } }
177+ >
178+ { showAll [ idx ] ? 'Show less ▲' : 'Show more ▼' }
179+ </ button >
180+ ) }
168181 </ div >
169182 </ div >
170183 ) ;
0 commit comments