import { Icon } from "@iconify/react/dist/iconify.js";
import {
alpha,
Box,
Grow,
IconButton,
ListItemIcon,
ListItemText,
Menu,
styled,
Typography,
useTheme,
} from "@mui/material";
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { CustomProps, CustomPropsType } from "../../../../api/explorer.ts";
import { useAppDispatch } from "../../../../redux/hooks.ts";
import { searchMetadata } from "../../../../redux/thunks/filemanager.ts";
import { copyToClipboard } from "../../../../util";
import Clipboard from "../../../Icons/Clipboard.tsx";
import DeleteOutlined from "../../../Icons/DeleteOutlined.tsx";
import MoreVertical from "../../../Icons/MoreVertical.tsx";
import Search from "../../../Icons/Search.tsx";
import { SquareMenuItem } from "../../ContextMenu/ContextMenu.tsx";
import { FileManagerIndex } from "../../FileManager.tsx";
import { StyledButtonBase } from "../StyledButtonBase.tsx";
import BooleanPropsItem from "./BooleanPropsContent.tsx";
import { CustomPropsItem } from "./CustomProps.tsx";
import LinkPropsContent from "./LinkPropsContent.tsx";
import MultiSelectPropsContent from "./MultiSelectPropsContent.tsx";
import NumberPropsContent from "./NumberPropsContent.tsx";
import RatingPropsItem from "./RatingPropsItem.tsx";
import SelectPropsContent from "./SelectPropsContent.tsx";
import TextPropsContent from "./TextPropsContent.tsx";
export interface CustomPropsCardProps {
prop: CustomPropsItem;
loading?: boolean;
onPropPatch: (prop: CustomPropsItem) => void;
onPropDelete?: (prop: CustomPropsItem) => void;
readOnly?: boolean;
}
export interface PropsContentProps {
prop: CustomPropsItem;
onChange: (value: string) => void;
loading?: boolean;
readOnly?: boolean;
backgroundColor?: string;
fullSize?: boolean;
}
const PropsCard = styled(StyledButtonBase)(({ theme }) => ({
flexDirection: "column",
alignItems: "flex-start",
gap: 9,
}));
export const getPropsContent = (
prop: CustomPropsItem,
onChange: (value: string) => void,
loading?: boolean,
readOnly?: boolean,
fullSize?: boolean,
) => {
switch (prop.props.type) {
case CustomPropsType.text:
return (
);
case CustomPropsType.rating:
return (
);
case CustomPropsType.number:
return (
);
case CustomPropsType.boolean:
return (
);
case CustomPropsType.select:
return (
);
case CustomPropsType.multi_select:
return (
);
case CustomPropsType.link:
return (
);
default:
return null;
}
};
export const isCustomPropStrongMatch = (prop: CustomProps) => {
return prop.type === CustomPropsType.rating || prop.type === CustomPropsType.number;
};
const CustomPropsCard = ({ prop, loading, onPropPatch, onPropDelete, readOnly }: CustomPropsCardProps) => {
const { t } = useTranslation();
const theme = useTheme();
const dispatch = useAppDispatch();
const [mouseOver, setMouseOver] = useState(false);
const [anchorEl, setAnchorEl] = useState(null);
const handleMenuClose = () => {
setAnchorEl(null);
};
const handleCopy = () => {
const value = prop.value || "";
copyToClipboard(value);
handleMenuClose();
};
const handleSearch = () => {
if (prop.value) {
dispatch(
searchMetadata(
FileManagerIndex.main,
`props:${prop.props.id}`,
prop.value,
false,
isCustomPropStrongMatch(prop.props),
),
);
}
handleMenuClose();
};
const handleDelete = () => {
if (onPropDelete) {
onPropDelete(prop);
}
handleMenuClose();
};
const Content = useMemo(() => {
return getPropsContent(prop, (value) => onPropPatch({ ...prop, value }), loading, readOnly, true);
}, [prop, loading, onPropPatch, readOnly]);
return (
setMouseOver(true)} onMouseLeave={() => setMouseOver(false)}>
setAnchorEl(e.currentTarget)}>
{prop.props.icon && }
{prop.props.type === CustomPropsType.boolean ? Content : t(prop.props.name)}
{prop.props.type !== CustomPropsType.boolean && (
{Content}
)}
);
};
export default CustomPropsCard;