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 placeholderCounts: Record = {};
let textCounter = 0;
const renderedParts = highlightedParts.map((part) => {
const isPlaceholder =
part === "%host" || part === "%port" || part === "%secret";
if (isPlaceholder) {
const count = placeholderCounts[part] ?? 0;
placeholderCounts[part] = count + 1;
return (
{part}
);
}
const key = `text-${textCounter}-${part || "empty"}`;
textCounter += 1;
return {part};
});
return (
<>
({
overflow: "hidden",
textOverflow: "ellipsis",
"> .placeholder": {
color: palette.primary.main,
},
})}
>
{renderedParts}
onOpenUrl?.(value)}
>
{
setEditing(true);
setEditValue(value);
}}
>
>
);
};