Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 56 additions & 19 deletions packages/ui/src/ui-component/cards/ItemCard.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { useSelector } from 'react-redux'

Expand Down Expand Up @@ -29,9 +30,36 @@ const CardWrapper = styled(MainCard)(({ theme }) => ({
whiteSpace: 'pre-line'
}))

const IconAvatar = ({ iconSrc, fallbackIconSrc }) => {
const [imgSrc, setImgSrc] = useState(iconSrc || fallbackIconSrc)

useEffect(() => {
setImgSrc(iconSrc || fallbackIconSrc)
}, [iconSrc, fallbackIconSrc])

if (!imgSrc) return null

return (
<img
src={imgSrc}
alt='item-icon'
onError={() => setImgSrc(fallbackIconSrc || '')}
Comment thread
sahil2448 marked this conversation as resolved.
style={{
width: 35,
height: 35,
display: 'flex',
flexShrink: 0,
marginRight: 10,
borderRadius: '50%',
objectFit: 'contain'
}}
/>
)
}

// ===========================|| CONTRACT CARD ||=========================== //

const ItemCard = ({ data, images, icons, scheduleStatus, onClick }) => {
const ItemCard = ({ data, images, icons, onClick, fallbackIconSrc }) => {
const theme = useTheme()
const customization = useSelector((state) => state.customization)

Expand All @@ -49,23 +77,27 @@ const ItemCard = ({ data, images, icons, scheduleStatus, onClick }) => {
overflow: 'hidden'
}}
>
{data.iconSrc && (
<div
style={{
width: 35,
height: 35,
display: 'flex',
flexShrink: 0,
marginRight: 10,
borderRadius: '50%',
backgroundImage: `url(${data.iconSrc})`,
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center center'
}}
></div>
{fallbackIconSrc ? (
<IconAvatar iconSrc={data.iconSrc} fallbackIconSrc={fallbackIconSrc} />
) : (
data.iconSrc && (
<div
style={{
width: 35,
height: 35,
display: 'flex',
flexShrink: 0,
marginRight: 10,
borderRadius: '50%',
backgroundImage: `url(${data.iconSrc})`,
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center center'
}}
></div>
)
)}
{!data.iconSrc && data.color && (
{!fallbackIconSrc && !data.iconSrc && data.color && (
<div
style={{
width: 35,
Expand Down Expand Up @@ -202,8 +234,13 @@ ItemCard.propTypes = {
data: PropTypes.object,
images: PropTypes.array,
icons: PropTypes.array,
scheduleStatus: PropTypes.object,
onClick: PropTypes.func
onClick: PropTypes.func,
fallbackIconSrc: PropTypes.string
}

IconAvatar.propTypes = {
iconSrc: PropTypes.string,
fallbackIconSrc: PropTypes.string
}

export default ItemCard
46 changes: 32 additions & 14 deletions packages/ui/src/ui-component/table/ToolsListTable.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { useSelector } from 'react-redux'
import { styled } from '@mui/material/styles'
Expand All @@ -15,6 +16,7 @@ import {
Typography,
useTheme
} from '@mui/material'
import toolSVG from '@/assets/images/tool.svg'

const StyledTableCell = styled(TableCell)(({ theme }) => ({
borderColor: theme.palette.grey[900] + 25,
Expand All @@ -35,6 +37,31 @@ const StyledTableRow = styled(TableRow)(() => ({
}
}))

const ToolIconAvatar = ({ iconSrc }) => {
const [imgSrc, setImgSrc] = useState(iconSrc || toolSVG)

useEffect(() => {
setImgSrc(iconSrc || toolSVG)
}, [iconSrc])

return (
<img
src={imgSrc}
alt='tool-icon'
onError={() => setImgSrc(toolSVG)}
style={{
width: 35,
height: 35,
display: 'flex',
flexShrink: 0,
marginRight: 10,
borderRadius: '50%',
objectFit: 'contain'
}}
/>
)
}
Comment thread
sahil2448 marked this conversation as resolved.

export const ToolsTable = ({ data, isLoading, onSelect }) => {
const theme = useTheme()
const customization = useSelector((state) => state.customization)
Expand Down Expand Up @@ -90,20 +117,7 @@ export const ToolsTable = ({ data, isLoading, onSelect }) => {
{data?.map((row, index) => (
<StyledTableRow key={index}>
<StyledTableCell sx={{ display: 'flex', alignItems: 'center', gap: 1 }} key='0'>
<div
style={{
width: 35,
height: 35,
display: 'flex',
flexShrink: 0,
marginRight: 10,
borderRadius: '50%',
backgroundImage: `url(${row.iconSrc})`,
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center center'
}}
></div>
<ToolIconAvatar iconSrc={row.iconSrc} />
<Typography
sx={{
display: '-webkit-box',
Expand Down Expand Up @@ -142,3 +156,7 @@ ToolsTable.propTypes = {
isLoading: PropTypes.bool,
onSelect: PropTypes.func
}

ToolIconAvatar.propTypes = {
iconSrc: PropTypes.string
}
74 changes: 69 additions & 5 deletions packages/ui/src/views/tools/ToolDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
const [toolName, setToolName] = useState('')
const [toolDesc, setToolDesc] = useState('')
const [toolIcon, setToolIcon] = useState('')
const [toolIconError, setToolIconError] = useState('')
const [toolSchema, setToolSchema] = useState([])
const [toolFunc, setToolFunc] = useState('')
const [showHowToDialog, setShowHowToDialog] = useState(false)
Expand All @@ -88,6 +89,44 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set

const [showPasteJSONDialog, setShowPasteJSONDialog] = useState(false)

const validateToolIconUrl = (iconSource) => {
const trimmedIconSource = iconSource?.trim()
if (!trimmedIconSource) return ''

try {
const parsedUrl = new URL(trimmedIconSource)
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
return 'Tool Icon Source must be a valid HTTP/HTTPS URL'
}
return ''
} catch {
return 'Tool Icon Source must be a valid HTTP/HTTPS URL'
}
}

const validateIconAndNotify = () => {
const iconError = validateToolIconUrl(toolIcon)
setToolIconError(iconError)

if (iconError) {
enqueueSnackbar({
message: iconError,
options: {
key: new Date().getTime() + Math.random(),
variant: 'error',
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
return false
}

return true
}

const deleteItem = useCallback(
(id) => () => {
setTimeout(() => {
Expand Down Expand Up @@ -177,6 +216,8 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
setToolId(getSpecificToolApi.data.id)
setToolName(getSpecificToolApi.data.name)
setToolDesc(getSpecificToolApi.data.description)
setToolIcon(getSpecificToolApi.data.iconSrc || '')
setToolIconError('')
setToolSchema(formatDataGridRows(getSpecificToolApi.data.schema))
if (getSpecificToolApi.data.func) setToolFunc(getSpecificToolApi.data.func)
else setToolFunc('')
Expand All @@ -196,7 +237,8 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
setToolId(dialogProps.data.id)
setToolName(dialogProps.data.name)
setToolDesc(dialogProps.data.description)
setToolIcon(dialogProps.data.iconSrc)
setToolIcon(dialogProps.data.iconSrc || '')
setToolIconError('')
setToolSchema(formatDataGridRows(dialogProps.data.schema))
if (dialogProps.data.func) setToolFunc(dialogProps.data.func)
else setToolFunc('')
Expand All @@ -207,15 +249,17 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
// When tool dialog is to import existing tool
setToolName(dialogProps.data.name)
setToolDesc(dialogProps.data.description)
setToolIcon(dialogProps.data.iconSrc)
setToolIcon(dialogProps.data.iconSrc || '')
setToolIconError('')
setToolSchema(formatDataGridRows(dialogProps.data.schema))
if (dialogProps.data.func) setToolFunc(dialogProps.data.func)
else setToolFunc('')
} else if (dialogProps.type === 'TEMPLATE' && dialogProps.data) {
// When tool dialog is a template
setToolName(dialogProps.data.name)
setToolDesc(dialogProps.data.description)
setToolIcon(dialogProps.data.iconSrc)
setToolIcon(dialogProps.data.iconSrc || '')
setToolIconError('')
setToolSchema(formatDataGridRows(dialogProps.data.schema))
if (dialogProps.data.func) setToolFunc(dialogProps.data.func)
else setToolFunc('')
Expand All @@ -225,6 +269,7 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
setToolName('')
setToolDesc('')
setToolIcon('')
setToolIconError('')
setToolSchema([])
setToolFunc('')
}
Expand Down Expand Up @@ -277,6 +322,8 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
}

const addNewTool = async () => {
if (!validateIconAndNotify()) return

try {
const obj = {
name: toolName,
Expand Down Expand Up @@ -323,6 +370,8 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
}

const saveTool = async () => {
if (!validateIconAndNotify()) return

try {
const saveResp = await toolsApi.updateTool(toolId, {
name: toolName,
Expand Down Expand Up @@ -513,8 +562,23 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
placeholder='https://raw.githubusercontent.com/gilbarbara/logos/main/logos/airtable.svg'
value={toolIcon}
name='toolIcon'
onChange={(e) => setToolIcon(e.target.value)}
error={!!toolIconError}
onBlur={() => setToolIconError(validateToolIconUrl(toolIcon))}
onChange={(e) => {
const iconSource = e.target.value
setToolIcon(iconSource)
setToolIconError(validateToolIconUrl(iconSource))
}}
/>
{toolIconError ? (
<Typography sx={{ mt: 0.5 }} variant='caption' color='error'>
{toolIconError}
</Typography>
) : (
<Typography sx={{ mt: 0.5 }} variant='caption' color='text.secondary'>
Optional. Leave empty to use the default tool icon.
</Typography>
)}
</Box>
<Box>
<Stack sx={{ position: 'relative', justifyContent: 'space-between' }} direction='row'>
Expand Down Expand Up @@ -583,7 +647,7 @@ const ToolDialog = ({ show, dialogProps, onUseTemplate, onCancel, onConfirm, set
{dialogProps.type !== 'TEMPLATE' && (
<StyledPermissionButton
permissionId={'tools:update,tools:create'}
disabled={!(toolName && toolDesc)}
disabled={!(toolName && toolDesc) || !!validateToolIconUrl(toolIcon)}
Comment thread
sahil2448 marked this conversation as resolved.
variant='contained'
onClick={() => (dialogProps.type === 'ADD' || dialogProps.type === 'IMPORT' ? addNewTool() : saveTool())}
>
Expand Down
Loading
Loading