Skip to content

Commit

Permalink
Bugfix/Undefined substring error (#2804)
Browse files Browse the repository at this point in the history
fix undefined substring error
  • Loading branch information
HenryHengZJ authored Jul 15, 2024
1 parent 9e88c45 commit 78e60e2
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/ui/src/views/chatmessage/ChatMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,22 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
data: s,
preview: s,
type: 'url',
name: s.substring(s.lastIndexOf('/') + 1)
name: s ? s.substring(s.lastIndexOf('/') + 1) : ''
}
setPreviews((prevPreviews) => [...prevPreviews, upload])
})
} else if (item.kind === 'string' && item.type.match('^text/html')) {
item.getAsString((s) => {
if (s.indexOf('href') === -1) return
//extract href
let start = s.substring(s.indexOf('href') + 6)
let start = s ? s.substring(s.indexOf('href') + 6) : ''
let hrefStr = start.substring(0, start.indexOf('"'))

let upload = {
data: hrefStr,
preview: hrefStr,
type: 'url',
name: hrefStr.substring(hrefStr.lastIndexOf('/') + 1)
name: hrefStr ? hrefStr.substring(hrefStr.lastIndexOf('/') + 1) : ''
}
setPreviews((prevPreviews) => [...prevPreviews, upload])
})
Expand Down Expand Up @@ -282,7 +282,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
if (pos === -1) {
mimeType = blob.type
} else {
mimeType = blob.type.substring(0, pos)
mimeType = blob.type ? blob.type.substring(0, pos) : ''
}
// read blob and add to previews
const reader = new FileReader()
Expand Down Expand Up @@ -598,6 +598,26 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
}
}

const getLabel = (URL, source) => {
if (URL && typeof URL === 'object') {
if (URL.pathname && typeof URL.pathname === 'string') {
if (URL.pathname.substring(0, 15) === '/') {
return URL.host || ''
} else {
return `${URL.pathname.substring(0, 15)}...`
}
} else if (URL.host) {
return URL.host
}
}

if (source && source.pageContent && typeof source.pageContent === 'string') {
return `${source.pageContent.substring(0, 15)}...`
}

return ''
}

const downloadFile = async (fileAnnotation) => {
try {
const response = await axios.post(
Expand Down Expand Up @@ -1180,13 +1200,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
<Chip
size='small'
key={index}
label={
URL
? URL.pathname.substring(0, 15) === '/'
? URL.host
: `${URL.pathname.substring(0, 15)}...`
: `${source.pageContent.substring(0, 15)}...`
}
label={getLabel(URL, source) || ''}
component='a'
sx={{ mr: 1, mb: 1 }}
variant='outlined'
Expand Down Expand Up @@ -1390,13 +1404,7 @@ export const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, preview
<Chip
size='small'
key={index}
label={
URL
? URL.pathname.substring(0, 15) === '/'
? URL.host
: `${URL.pathname.substring(0, 15)}...`
: `${source.pageContent.substring(0, 15)}...`
}
label={getLabel(URL, source) || ''}
component='a'
sx={{ mr: 1, mb: 1 }}
variant='outlined'
Expand Down

0 comments on commit 78e60e2

Please sign in to comment.