import {
CheckRounded,
CloseRounded,
DeleteRounded,
EditRounded,
OpenInNewRounded,
} from '@mui/icons-material'
import {
Divider,
IconButton,
Stack,
TextField,
Typography,
} from '@mui/material'
import { useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
interface Props {
value?: string
onlyEdit?: boolean
onChange: (value?: string) => void
onOpenUrl?: (value?: string) => void
onDelete?: () => void
onCancel?: () => void
}
export const WebUIItem = (props: Props) => {
const {
value,
onlyEdit = false,
onChange,
onDelete,
onOpenUrl,
onCancel,
} = props
const [editing, setEditing] = useState(false)
const [editValue, setEditValue] = useState(value)
const { t } = useTranslation()
const highlightedParts = useMemo(() => {
const placeholderRegex = /(%host|%port|%secret)/g
if (!value) {
return ['NULL']
}
return value.split(placeholderRegex).filter((part) => part !== '')
}, [value])
if (editing || onlyEdit) {
return (
<>
setEditValue(e.target.value)}
placeholder={t(
'settings.modals.webUI.messages.supportedPlaceholders',
)}
/>
{
onChange(editValue)
setEditing(false)
}}
>
{
onCancel?.()
setEditing(false)
}}
>
>
)
}
const renderedParts = highlightedParts.map((part, index) => {
const isPlaceholder =
part === '%host' || part === '%port' || part === '%secret'
const repeatIndex = highlightedParts
.slice(0, index)
.filter((prev) => prev === part).length
const key = `${part || 'empty'}-${repeatIndex}`
return (
{part}
)
})
return (
<>
({
overflow: 'hidden',
textOverflow: 'ellipsis',
'> .placeholder': {
color: palette.primary.main,
},
})}
>
{renderedParts}
onOpenUrl?.(value)}
>
{
setEditing(true)
setEditValue(value)
}}
>
>
)
}