6
6
Notice ,
7
7
SearchBar ,
8
8
} from '@scientist-softserv/webstore-component-library'
9
+ import Markdown from 'react-markdown'
9
10
import {
10
11
buttonBg ,
11
12
configureErrors ,
@@ -18,6 +19,7 @@ const Browse = ({ session }) => {
18
19
const [ query , setQuery ] = useState ( '' )
19
20
const existingQuery = router . query . q
20
21
const accessToken = session ?. accessToken
22
+ const [ open , setOpen ] = useState ( false )
21
23
22
24
useEffect ( ( ) => {
23
25
if ( existingQuery ) setQuery ( existingQuery )
@@ -46,6 +48,13 @@ const Browse = ({ session }) => {
46
48
)
47
49
}
48
50
51
+ const truncateDescription = ( desc = '' , maxLength , isOpen ) => {
52
+ if ( desc . length <= maxLength || isOpen ) return { truncated : desc , cutOffIndex : desc . length } ;
53
+ const lastSpaceIndex = desc . substring ( 0 , maxLength ) . lastIndexOf ( ' ' ) ;
54
+ const ellipsis = isOpen ? '' : '...' ;
55
+ return { truncated : desc . slice ( 0 , lastSpaceIndex ) + ellipsis , cutOffIndex : lastSpaceIndex } ;
56
+ }
57
+
49
58
return (
50
59
< div className = 'container' >
51
60
< SearchBar onSubmit = { handleOnSubmit } initialValue = { existingQuery } />
@@ -60,19 +69,32 @@ const Browse = ({ session }) => {
60
69
< >
61
70
{ ( services . length > 0 ) ? (
62
71
< >
63
- { services . map ( service => (
64
- < Item
65
- key = { service . id }
66
- item = { service }
67
- withButtonLink = { true }
68
- buttonLink = { service . href }
69
- orientation = 'horizontal'
70
- buttonProps = { {
71
- backgroundColor : buttonBg ,
72
- label : 'Request this item' ,
73
- } }
74
- />
75
- ) ) }
72
+ { services . map ( service => {
73
+ const { truncated, cutOffIndex } = truncateDescription ( service ?. description , 300 , open )
74
+ return (
75
+ < Item
76
+ key = { service . id }
77
+ markdownDescriptionTruncated = { (
78
+ < Markdown >
79
+ { truncated }
80
+ </ Markdown >
81
+ ) }
82
+ markdownDescriptionExtended = { (
83
+ < Markdown >
84
+ { service ?. description ?. slice ( cutOffIndex ) . trimStart ( ) }
85
+ </ Markdown >
86
+ ) }
87
+ item = { service }
88
+ withButtonLink = { true }
89
+ buttonLink = { service . href }
90
+ orientation = 'horizontal'
91
+ buttonProps = { {
92
+ backgroundColor : buttonBg ,
93
+ label : 'Request this item' ,
94
+ } }
95
+ />
96
+ )
97
+ } ) }
76
98
</ >
77
99
) : (
78
100
< p data-cy = 'no-results' >
0 commit comments