From 625384a0cff6d6fc768e484d5bc1fc7b777fb7aa Mon Sep 17 00:00:00 2001 From: Metin Date: Mon, 28 Oct 2024 15:54:44 +0400 Subject: [PATCH] modal --- src/App.jsx | 17 +- src/components/Modal.jsx | 13 +- src/components/MusicPlayer.jsx | 37 +++ src/layouts/MP3Player.jsx | 472 ++++++++++++++++----------------- src/utils/RefreshLink.js | 26 +- 5 files changed, 296 insertions(+), 269 deletions(-) create mode 100644 src/components/MusicPlayer.jsx diff --git a/src/App.jsx b/src/App.jsx index 5af4051..0964ded 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,4 +1,4 @@ -import { useContext, useRef, useState } from "react"; +import { useContext, useEffect, useRef, useState } from "react"; import { toast, ToastContainer, Slide } from "react-toastify"; import { fetchMP3Data } from "./utils/ConvertToMP3"; @@ -10,8 +10,10 @@ import Footer from "./components/Footer"; import CustomSelect from "./components/ReactSelect"; import { MP3Context } from "./contexts/MP3Context"; import ResultLink from "./components/ResultLink"; +import Modal from "./components/Modal"; function App() { + const [modal, setModal] = useState(false); const inputUrl = useRef(null); const [loading, setLoading] = useState(false); const [convertType, setConvertType] = useState("mp3"); @@ -19,6 +21,10 @@ function App() { const options = [{ value: "mp3", label: "MP3" }]; const [lastConvertedMP3, setLastConvertedMP3] = useState(null); + const handleModal = () => { + setModal(false); + }; + const handleSelectChange = (e) => { setConvertType(e); }; @@ -40,6 +46,15 @@ function App() { return ( <> + {modal && ( + + )} +
diff --git a/src/components/Modal.jsx b/src/components/Modal.jsx index 737aa08..6e99cbc 100644 --- a/src/components/Modal.jsx +++ b/src/components/Modal.jsx @@ -2,12 +2,7 @@ import React from "react"; import { Transition } from "@headlessui/react"; import Button from "./Button"; -const Modal = ({ - isOpen = null, - onClose = () => {}, - children = "", - description = "", -}) => { +const Modal = ({ isOpen = null, onClose = () => {}, paragraph = "", description = "" }) => { return (
-
+

{description}

-

{children}

+

{paragraph}

+ ); + + // Progress Bar with Tooltip + const ProgressBar = ({ progress, onChange, onHover, tooltip }) => ( +
+
+ + {tooltip?.visible &&
{tooltip.time}
} +
+ ); + + // Loop and Volume Control + const LoopAndVolumeControls = ({ isLooping, onLoopToggle, volume, onVolumeChange, fileSize }) => ( +
+
+ ); + return (
@@ -322,17 +419,11 @@ const MP3Player = () => {
{loading ? ( -
-
- -
-
+ loadingScreen ) : Object.keys(mp3List).length === 0 ? ( -

Convert any music to be able to listen to it here.

+ noMp3ListMessage ) : Object.keys(filteredMP3s).length == 0 ? ( -

- No music matches your search. Try being more case-sensitive. -

+ noResultsMessage ) : (
    {filteredMP3s.map((key, index) => { @@ -354,148 +445,49 @@ const MP3Player = () => { ) : (
    - {/* Floating Player Toggle Button */}
    - {/* Play Button */} -
    - - {/* Container for the action buttons, aligned to the right */}
    -
    -
    -
    - - {/* Total Duration */} -

    Duration: {duration}

    -
    - - {/* Volume and Progress Bars Column */} -
    -
    - {/* Progress bar */} -
    - {/* Progress bar fill */} -
    - - {/* Movable round div at the end of the progress */} -
    - {/* Hidden progress bar input */} - handleProgressChange(e, index)} - className="absolute top-0 left-0 w-full h-2 appearance-none cursor-pointer bg-transparent z-10" - style={{ - opacity: 0, - }} - onMouseMove={(e) => handleProgressMouseMove(e, index)} - onMouseLeave={() => handleProgressMouseLeave(index)} - onTouchMove={(e) => handleProgressTouchMove(e, index)} - onTouchEnd={() => handleProgressTouchEnd(index)} - /> -
    - - {/* Tooltip */} - {tooltips[index]?.visible && ( -
    - {tooltips[index].time} -
    - )} -
    -
    - - {/* Loop Button and Volume Control */} -
    -
    - {/* Audio Element */} -
    )} @@ -515,7 +507,7 @@ const MP3Player = () => { onPrevious={handlePreviousTrack} onClosePlayer={() => handleFloatingPlayer(state.playingIndex)} onVolumeChange={(e) => handleVolumeChange(e, state.playingIndex)} - currentVolume={state.volumes[state.playingIndex] || 1} + currentVolume={mp3Object.volumes[state.playingIndex] || 1} /> )}
diff --git a/src/utils/RefreshLink.js b/src/utils/RefreshLink.js index f4ac485..d7264f9 100644 --- a/src/utils/RefreshLink.js +++ b/src/utils/RefreshLink.js @@ -4,21 +4,10 @@ import { refreshMP3 } from "../database/music/refreshMP3"; export const refreshMP3Link = async (itemUrl, youtubeID, setMp3List, toast, fileSize, authUser, loadingUser) => { try { - let rangeEnd; - const tenMB = 10485760; - - if (fileSize <= tenMB) { - rangeEnd = fileSize - 1; - console.log("File size is 10MB or less. Fetching the entire file for validation."); - } else { - rangeEnd = Math.floor(fileSize * 0.5) - 1; - console.log("File size is larger than 10MB. Fetching 50% of the file for validation."); - } - const response = await fetch(itemUrl, { method: "GET", headers: { - Range: `bytes=0-${rangeEnd}`, + Range: `bytes=0-${fileSize - 1}`, }, }); @@ -28,7 +17,7 @@ export const refreshMP3Link = async (itemUrl, youtubeID, setMp3List, toast, file } toast.info("Executing link refresh."); - await refresh(toast, youtubeID, setMp3List, authUser, loadingUser); + await refreshLink(toast, youtubeID, setMp3List, authUser, loadingUser); return false; } catch (error) { console.error("Error checking the link:", error.message); @@ -37,7 +26,7 @@ export const refreshMP3Link = async (itemUrl, youtubeID, setMp3List, toast, file } }; -const refresh = async (toast, youtubeID, setMp3List, authUser, loadingUser) => { +const refreshLink = async (toast, youtubeID, setMp3List, authUser, loadingUser) => { const allMP3s = await getAllMP3s(authUser, loadingUser); try { const options = { @@ -62,13 +51,14 @@ const refresh = async (toast, youtubeID, setMp3List, authUser, loadingUser) => { const mp3ToUpdate = updatedMP3s.find((mp3) => mp3.youtubeID === youtubeID); if (mp3ToUpdate) { - await refreshMP3(youtubeID, mp3ToUpdate).then(() => { - setMp3List(updatedMP3s); - toast.success("Link refreshed and replaced successfully!"); - }); + await refreshMP3(youtubeID, mp3ToUpdate); + setMp3List(updatedMP3s); + toast.success("Link refreshed and replaced successfully!"); + return true; } } catch (error) { toast.error("Failed to refresh the link. Please try again."); console.error("Error refreshing the link:", error); + return false; } };