generated from UofA-Blueprint/ScaffoldTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { File, X, Check, Trash } from "@phosphor-icons/react"; | ||
|
||
// Props definition for MediaUploadStatus | ||
interface MediaUploadStatusProps { | ||
fileName: string; | ||
fileSize: number; | ||
} | ||
|
||
// Format bytes into its corresponding format | ||
const formatBytes = (bytes: number, decimals = 2): string => { | ||
if (bytes === 0) return '0 Bytes'; | ||
const k = 1000; // Decimal multiple | ||
const dm = decimals < 0 ? 0 : decimals; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | ||
}; | ||
|
||
// Main function | ||
const MediaUploadStatus: React.FC<MediaUploadStatusProps> = ({ fileName, fileSize }) => { | ||
const [uploadProgress, setUploadProgress] = useState(70); // Example progress in percentage | ||
const [isFailed, setIsFailed] = useState(false); // Example failed condition | ||
const [isCompleted, setIsCompleted] = useState(false); | ||
const [showDoneStatus, setShowDoneStatus] = useState(false); | ||
|
||
// For the trash icon to appear when it complete | ||
useEffect(() => { | ||
let timer: ReturnType<typeof setTimeout>; | ||
if (isCompleted) { | ||
timer = setTimeout(() => { | ||
setShowDoneStatus(true); | ||
}, 2500); // 2.5 sec | ||
} else { | ||
setShowDoneStatus(false); | ||
} | ||
return () => clearTimeout(timer); // Clean up the timer when isCompleted changes | ||
}, [isCompleted]); | ||
|
||
// Condition to check if file is uploaded successfully | ||
useEffect(() => { | ||
if (uploadProgress === 100) { | ||
setIsCompleted(true); | ||
} else { | ||
setIsCompleted(false); | ||
} | ||
}, [uploadProgress]); | ||
|
||
// useState for isFailed will implement here! | ||
|
||
|
||
|
||
// useState for setUploadProgress will implement here! | ||
// This function expect to return the correct uploading percentage for the file | ||
|
||
|
||
return ( | ||
<div className= {`flex items-center rounded py-2 px-4 gap-2 h-full text-neutrals-dark-500 ${isFailed ? "bg-status-red-light" : "bg-neutrals-light-300"}`}> | ||
{/* File Icon */} | ||
<div> | ||
<File size={24} /> | ||
</div> | ||
|
||
{/* Middle Content: Filename, Progress Bar, Info Text */} | ||
<div className="flex-1"> | ||
{/*Filename*/} | ||
<div className="flex flex-col"> | ||
<div className="text-sm"> | ||
{fileName} | ||
</div> | ||
|
||
{showDoneStatus ? null : | ||
( | ||
<div className="relative mt-1"> | ||
{/* Progress bar */} | ||
{isFailed ? null : ( | ||
<div className="w-full bg-neutrals-dark-100 h-1 rounded-full"> | ||
<div className="bg-primary-main h-1 rounded-full" style={{ width: `${uploadProgress}%`}}></div> | ||
</div> | ||
) | ||
} | ||
|
||
{/* Uploading info text */} | ||
{isFailed ? ( | ||
<div className="flex justify-between text-[0.625rem] leading-[0.919rem] text-status-red-main font-normal"> | ||
<div>Failed to upload</div> | ||
</div> | ||
) : | ||
(isCompleted ? ( | ||
<div className="flex justify-between text-[0.625rem] leading-[0.919rem] text-neutrals-dark-200 font-normal mt-1.5"> | ||
<div>Complete</div> | ||
<div className=" text-status-green-main"> | ||
<Check size={16} /> | ||
</div> | ||
</div> | ||
) : | ||
( | ||
<div className="flex justify-between text-[0.625rem] leading-[0.919rem] text-neutrals-dark-200 font-normal mt-1.5"> | ||
<div>{formatBytes((fileSize * uploadProgress) / 100)} of {formatBytes(fileSize)}</div> | ||
<div>Uploading... {uploadProgress}%</div> | ||
</div> | ||
) | ||
) | ||
} | ||
</div> | ||
) | ||
} | ||
</div> | ||
</div> | ||
|
||
{/* X Button */} | ||
{isCompleted ? ( | ||
showDoneStatus ? ( | ||
<button className=" pl-2 text-neutrals-dark-200 hover:text-neutrals-dark-500 transition ease-in-out duration-300"> | ||
<Trash size={24} /> | ||
</button>) : null | ||
) : ( | ||
<button className=" pl-2 text-neutrals-dark-200 hover:text-neutrals-dark-500"> | ||
<X size={24} /> | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MediaUploadStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import MediaUploadStatus from "@/components/MediaUploadStatus"; | ||
|
||
|
||
// TODO: Add the +20% button here to test it and the fail to upload button | ||
// | ||
function MediaUploadStatusTestRoute() { | ||
return ( | ||
<div className="text-h2 flex flex-col items-center justify-center min-h-screen bg-primary-light"> | ||
<div> | ||
<h1>Is this what you looking for? </h1> | ||
{/* fileSize is in bytes */} | ||
{/* Play around with uploadProgress in percentage and isFailed variables in MediaUploadStatus.tsx */} | ||
<MediaUploadStatus fileName="Testing.png" fileSize={1000000000}/> | ||
</div> | ||
</div> | ||
|
||
) | ||
} | ||
export default MediaUploadStatusTestRoute; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters