import { useState, useRef } from "react";
import { uploadFile } from "@/shared/lib/uploadFile";
import type { CommentAction } from "@/entities/action/schema";
import { getActionAttachmentType } from "@/entities/action/lib";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Box from "@mui/material/Box";
import { Button, TextField } from "@mui/material";
import CircularProgress from "@mui/material/CircularProgress";
import TextFieldsIcon from "@mui/icons-material/TextFields";
import ImageIcon from "@mui/icons-material/Image";
// import RecordVoiceOverIcon from "@mui/icons-material/RecordVoiceOver";
interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
{value === index && {children}}
);
}
function a11yProps(index: number) {
return {
id: `simple-tab-${index}`,
"aria-controls": `simple-tabpanel-${index}`,
};
}
interface ActionEditorProps {
actionIndex?: number;
initialAction?: CommentAction;
onSave?: (actionIndex: number, action: CommentAction) => Promise | void;
onClose?: () => Promise | void;
canExit?: boolean;
}
export default function ActionEditor({
actionIndex = 0,
initialAction,
onSave,
onClose,
canExit = false,
}: ActionEditorProps) {
const imageRef = useRef(null);
const attachmentType = initialAction
? getActionAttachmentType(initialAction)
: "empty";
const [value, setValue] = useState(
attachmentType === "empty" ? 0 : attachmentType === "image" ? 1 : 2,
);
const [loading, setLoading] = useState(false);
const [imageUrl, setImageUrl] = useState(
initialAction && attachmentType === "image"
? initialAction.fileUrls[0]!
: null,
);
const [action, setAction] = useState(
initialAction ?? {
actionType: "comment",
text: "",
fileUrls: [],
},
);
const handleChange = (_: React.SyntheticEvent, newValue: number) => {
if (loading) return;
setValue(newValue);
};
async function handleFileChange(
event: React.ChangeEvent,
origin: "image" | "voice",
) {
if (!event.target.files) return;
const file = event.target.files[0];
setLoading(true);
const fileUrl = await uploadFile(file);
if (!fileUrl) {
alert("К сожалению, не удалось загрузить файл");
setLoading(false);
return;
}
if (origin === "image") {
setImageUrl(fileUrl);
}
setLoading(false);
}
function handleSave() {
if (!onSave) return;
if (value === 0) {
if (!action.text) {
alert("Введите текст");
return;
}
onSave(actionIndex, {
actionType: action.actionType,
text: action.text,
fileUrls: [],
});
} else if (value === 1) {
if (!imageUrl) {
alert("Добавьте картинку");
return;
}
onSave(actionIndex, {
actionType: action.actionType,
text: action.text === "" ? null : action.text,
fileUrls: [imageUrl],
});
} else if (value === 2) {
alert("Unsupported");
return;
}
}
return (
<>
} {...a11yProps(0)} />
} {...a11yProps(1)} />
{/* }
{...a11yProps(2)}
/> */}
setAction((currentAction) => ({
...currentAction,
text: event.target.value,
}))
}
/>
{imageUrl && (

)}
{loading && (
)}
await handleFileChange(e, "image")}
/>
setAction((currentAction) => ({
...currentAction,
text: event.target.value,
}))
}
/>
{/*
In progress
*/}
{canExit && (
)}
>
);
}