diff --git a/src/components/LeaveSession.jsx b/src/components/LeaveSession.jsx index 10535a6..8a76ba3 100644 --- a/src/components/LeaveSession.jsx +++ b/src/components/LeaveSession.jsx @@ -1,5 +1,5 @@ -import React from "react"; import axios from "axios"; +import ConfirmModal from "./confirmModal"; const LeaveSession = ({ projectId }) => { const isGuest = localStorage.getItem(`project_${projectId}_guest`) === "true"; @@ -7,20 +7,32 @@ const LeaveSession = ({ projectId }) => { if (!isGuest) return null; - const handleLeave = async () => { - if ( - !confirm( - "Are you sure you want to leave? You won't be able to access this project again from this device." - ) - ) { - return; - } + const [modalState, setModalState] = React.useState({ + isOpen: false, + type: "", + title: "", + message: "", + onConfirm: null, + }); + + const handleLeave = () => { + setModalState({ + isOpen: true, + type: "confirm", + title: "Leave Session", + message: + "Are you sure you want to leave? You won't be able to access this project again from this device.", + onConfirm: performLeave, + }); + }; + const performLeave = async () => { + setModalState((prev) => ({ ...prev, isOpen: false })); try { await axios.post( `${serverUrl}/api/projects/${projectId}/leave-session`, {}, - { withCredentials: true } + { withCredentials: true }, ); // Clear local data @@ -28,11 +40,24 @@ const LeaveSession = ({ projectId }) => { localStorage.removeItem(`project_${projectId}_ws`); localStorage.removeItem(`project_${projectId}_guest`); - alert("Session ended. Redirecting..."); - window.location.href = "/"; + setModalState({ + isOpen: true, + type: "alert", + title: "Session Ended", + message: "Session ended. Redirecting...", + onConfirm: () => { + window.location.href = "/"; + }, + }); } catch (error) { console.error("Leave error:", error); - alert("Failed to leave session"); + setModalState({ + isOpen: true, + type: "alert", + title: "Error", + message: "Failed to leave session", + onConfirm: () => setModalState((prev) => ({ ...prev, isOpen: false })), + }); } }; @@ -52,6 +77,18 @@ const LeaveSession = ({ projectId }) => { Leave Session + { + if (modalState.onConfirm) modalState.onConfirm(); + else setModalState((prev) => ({ ...prev, isOpen: false })); + }} + onCancel={() => setModalState((prev) => ({ ...prev, isOpen: false }))} + /> ); }; diff --git a/src/components/easyMathInput.jsx b/src/components/easyMathInput.jsx index a95ad2d..edd381a 100644 --- a/src/components/easyMathInput.jsx +++ b/src/components/easyMathInput.jsx @@ -174,7 +174,11 @@ const EasyMathInput = ({ onClose, onInsert }) => { if (data.success && data.pdfUrl) return `${API_BASE_URL}${data.pdfUrl}`; throw new Error("No URL returned"); } catch (e) { - alert(`Error: ${e.message}`); + setErrorModal({ + isOpen: true, + title: "Compile Error", + message: `Error: ${e.message}`, + }); return null; } finally { setIsCompiling(false); @@ -263,7 +267,14 @@ const EasyMathInput = ({ onClose, onInsert }) => { }; const handleSaveEquation = async () => { - if (!saveFileName.trim()) return alert("Enter filename"); + if (!saveFileName.trim()) { + setErrorModal({ + isOpen: true, + title: "Name Required", + message: "Enter filename", + }); + return; + } try { await fetch(`${API_BASE_URL}/api/equations/save`, { method: "POST", @@ -273,7 +284,7 @@ const EasyMathInput = ({ onClose, onInsert }) => { setShowSaveDialog(false); loadSavedEquations(); } catch (e) { - alert(e.message); + setErrorModal({ isOpen: true, title: "Save Error", message: e.message }); } }; const handleDeleteEquation = async (fileName) => { @@ -1014,7 +1025,6 @@ const EasyMathInput = ({ onClose, onInsert }) => {