import { Box, Button, Paper, Typography, Menu, MenuItem, TextField, } from "@mui/material"; import { authInstance, BackButton, editSightStore, languageStore, SelectArticleModal, SelectMediaDialog, TabPanel, UploadMediaDialog, } from "@shared"; import { DeleteModal, LanguageSwitcher, MediaArea, MediaAreaForSight, ReactMarkdownComponent, ReactMarkdownEditor, } from "@widgets"; import { ImagePlus, Plus, Save, Trash2, Unlink, X } from "lucide-react"; import { observer } from "mobx-react-lite"; import { useEffect, useState } from "react"; import { toast } from "react-toastify"; import { MediaViewer } from "../../MediaViewer/index"; import { DragDropContext, Droppable, Draggable, DropResult, } from "@hello-pangea/dnd"; export const RightWidgetTab = observer( ({ value, index }: { value: number; index: number }) => { const [anchorEl, setAnchorEl] = useState(null); const { sight, updateRightArticleInfo, getRightArticles, updateSight, unlinkPreviewMedia, linkPreviewMedia, unlinkRightArticle, deleteRightArticle, linkArticle, deleteRightArticleMedia, createLinkWithRightArticle, setFileToUpload, createNewRightArticle, updateRightArticles, } = editSightStore; const [previewMedia, setPreviewMedia] = useState(null); useEffect(() => { const fetchPreviewMedia = async () => { if (sight.common.preview_media) { const response = await authInstance.get( `/media/${sight.common.preview_media}` ); setPreviewMedia(response.data); } }; fetchPreviewMedia(); }, [sight.common.preview_media]); const handleUnlinkPreviewMedia = () => { unlinkPreviewMedia(); setPreviewMedia(null); }; const [uploadMediaOpen, setUploadMediaOpen] = useState(false); const { language } = languageStore; const [type, setType] = useState<"article" | "media">("media"); useEffect(() => { const fetchData = async () => { if (sight.common.id) { await getRightArticles(sight.common.id); } }; fetchData(); }, [sight.common.id]); const [activeArticleIndex, setActiveArticleIndex] = useState( null ); const [isSelectModalOpen, setIsSelectModalOpen] = useState(false); const [isSelectMediaModalOpen, setIsSelectMediaModalOpen] = useState(false); const [isDeleteArticleModalOpen, setIsDeleteArticleModalOpen] = useState(false); const open = Boolean(anchorEl); const handleDeleteArticle = () => { deleteRightArticle(sight[language].right[activeArticleIndex || 0].id); setActiveArticleIndex(null); }; const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; const handleSelectArticle = (index: number) => { setActiveArticleIndex(index); }; const handleCreateNew = async () => { try { const newArticleId = await createNewRightArticle(); handleClose(); const newIndex = sight[language].right.findIndex( (article) => article.id === newArticleId ); if (newIndex > -1) { setActiveArticleIndex(newIndex); setType("article"); } } catch (error) { console.error("Error creating new article:", error); } }; const handleSelectExisting = () => { setIsSelectModalOpen(true); handleClose(); }; const handleCloseSelectModal = () => { setIsSelectModalOpen(false); }; const handleArticleSelect = async (id: number) => { try { const linkedArticleId = await linkArticle(id); handleCloseSelectModal(); const newIndex = sight[language].right.findIndex( (article) => article.id === linkedArticleId ); if (newIndex > -1) { setActiveArticleIndex(newIndex); setType("article"); } } catch (error) { console.error("Error linking article:", error); } }; const handleMediaSelected = async (media: { id: string; filename: string; media_name?: string; media_type: number; }) => { await createLinkWithRightArticle( media, sight[language].right[activeArticleIndex || 0].id ); }; const handleSave = async () => { await updateSight(); toast.success("Достопримечательность сохранена"); }; const handleDragEnd = (result: DropResult) => { const { source, destination } = result; if (!destination) return; const sourceIndex = source.index; const destinationIndex = destination.index; if (sourceIndex === destinationIndex) return; const newRightArticles = [...sight[language].right]; const [movedArticle] = newRightArticles.splice(sourceIndex, 1); newRightArticles.splice(destinationIndex, 0, movedArticle); updateRightArticles(newRightArticles); }; return (

{sight[language].name}

setType("media")} className="w-full bg-green-200 p-4 rounded-2xl cursor-pointer text-sm hover:bg-gray-300 transition-all duration-300" > Предпросмотр медиа {(provided) => ( {sight[language].right.length > 0 ? sight[language].right.map( (article, index) => ( {(provided, snapshot) => ( { handleSelectArticle(index); setType("article"); }} > {article.heading} )} ) ) : null} {provided.placeholder} )} Создать новую Выбрать существующую статью {type === "article" && ( {activeArticleIndex !== null && ( <> updateRightArticleInfo( activeArticleIndex, language, e.target.value, sight[language].right[activeArticleIndex].body ) } variant="outlined" fullWidth /> updateRightArticleInfo( activeArticleIndex, language, sight[language].right[activeArticleIndex] .heading, value ) } /> { setFileToUpload(files[0]); setUploadMediaOpen(true); }} deleteMedia={deleteRightArticleMedia} setSelectMediaDialogOpen={() => { setIsSelectMediaModalOpen(true); }} /> )} )} {type === "media" && ( {sight.common.preview_media && ( <> {type === "media" && ( {previewMedia && ( <> )} {!previewMedia && ( { linkPreviewMedia(mediaId); }} onFilesDrop={() => {}} contextObjectName={sight[language].name} contextType="sight" isArticle={false} /> )} )} )} {!sight.common.preview_media && ( { linkPreviewMedia(mediaId); }} onFilesDrop={() => {}} contextObjectName={sight[language].name} contextType="sight" isArticle={false} /> )} )} {type === "article" && ( {activeArticleIndex !== null && ( {sight[language].right[activeArticleIndex].media.length > 0 ? ( ) : ( )} {sight[language].right[activeArticleIndex].heading || "Выберите статью"} {sight[language].right[activeArticleIndex].body ? ( ) : ( Предпросмотр статьи появится здесь )} {sight[language].right.length > 0 && sight[language].right.map((article, index) => ( ))} )} )}
setUploadMediaOpen(false)} contextObjectName={sight[language].name} contextType="sight" isArticle={true} articleName={ activeArticleIndex !== null ? sight[language].right[activeArticleIndex].heading : "Правая статья" } afterUpload={async (media) => { setUploadMediaOpen(false); setFileToUpload(null); await createLinkWithRightArticle( media, sight[language].right[activeArticleIndex || 0].id ); }} /> setIsDeleteArticleModalOpen(false)} onDelete={() => { handleDeleteArticle(); setIsDeleteArticleModalOpen(false); }} /> article.id)} /> setIsSelectMediaModalOpen(false)} onSelectMedia={handleMediaSelected} />
); } );