Skip to content

Commit 78e60e2

Browse files
authored
Bugfix/Undefined substring error (#2804)
fix undefined substring error
1 parent 9e88c45 commit 78e60e2

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

Diff for: packages/ui/src/views/chatmessage/ChatMessage.jsx

+26-18
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,22 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
214214
data: s,
215215
preview: s,
216216
type: 'url',
217-
name: s.substring(s.lastIndexOf('/') + 1)
217+
name: s ? s.substring(s.lastIndexOf('/') + 1) : ''
218218
}
219219
setPreviews((prevPreviews) => [...prevPreviews, upload])
220220
})
221221
} else if (item.kind === 'string' && item.type.match('^text/html')) {
222222
item.getAsString((s) => {
223223
if (s.indexOf('href') === -1) return
224224
//extract href
225-
let start = s.substring(s.indexOf('href') + 6)
225+
let start = s ? s.substring(s.indexOf('href') + 6) : ''
226226
let hrefStr = start.substring(0, start.indexOf('"'))
227227

228228
let upload = {
229229
data: hrefStr,
230230
preview: hrefStr,
231231
type: 'url',
232-
name: hrefStr.substring(hrefStr.lastIndexOf('/') + 1)
232+
name: hrefStr ? hrefStr.substring(hrefStr.lastIndexOf('/') + 1) : ''
233233
}
234234
setPreviews((prevPreviews) => [...prevPreviews, upload])
235235
})
@@ -282,7 +282,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
282282
if (pos === -1) {
283283
mimeType = blob.type
284284
} else {
285-
mimeType = blob.type.substring(0, pos)
285+
mimeType = blob.type ? blob.type.substring(0, pos) : ''
286286
}
287287
// read blob and add to previews
288288
const reader = new FileReader()
@@ -598,6 +598,26 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
598598
}
599599
}
600600

601+
const getLabel = (URL, source) => {
602+
if (URL && typeof URL === 'object') {
603+
if (URL.pathname && typeof URL.pathname === 'string') {
604+
if (URL.pathname.substring(0, 15) === '/') {
605+
return URL.host || ''
606+
} else {
607+
return `${URL.pathname.substring(0, 15)}...`
608+
}
609+
} else if (URL.host) {
610+
return URL.host
611+
}
612+
}
613+
614+
if (source && source.pageContent && typeof source.pageContent === 'string') {
615+
return `${source.pageContent.substring(0, 15)}...`
616+
}
617+
618+
return ''
619+
}
620+
601621
const downloadFile = async (fileAnnotation) => {
602622
try {
603623
const response = await axios.post(
@@ -1180,13 +1200,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
11801200
<Chip
11811201
size='small'
11821202
key={index}
1183-
label={
1184-
URL
1185-
? URL.pathname.substring(0, 15) === '/'
1186-
? URL.host
1187-
: `${URL.pathname.substring(0, 15)}...`
1188-
: `${source.pageContent.substring(0, 15)}...`
1189-
}
1203+
label={getLabel(URL, source) || ''}
11901204
component='a'
11911205
sx={{ mr: 1, mb: 1 }}
11921206
variant='outlined'
@@ -1390,13 +1404,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
13901404
<Chip
13911405
size='small'
13921406
key={index}
1393-
label={
1394-
URL
1395-
? URL.pathname.substring(0, 15) === '/'
1396-
? URL.host
1397-
: `${URL.pathname.substring(0, 15)}...`
1398-
: `${source.pageContent.substring(0, 15)}...`
1399-
}
1407+
label={getLabel(URL, source) || ''}
14001408
component='a'
14011409
sx={{ mr: 1, mb: 1 }}
14021410
variant='outlined'

0 commit comments

Comments
 (0)