-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add UI option for viewing file metadata (#108)
* refactor: move SetAtom type to separate file * chore: fix toasts * feat: add dialog trigger for metadata modal * chore: add missing dependency for useEffect * chore(ui): add filemetadata type * feat: add file-data-modal
- Loading branch information
1 parent
d923d0a
commit 9982727
Showing
6 changed files
with
114 additions
and
21 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
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,81 @@ | ||
import { useEffect, useState } from "react"; | ||
import { DialogContent, DialogHeader, DialogTitle } from "./ui/dialog"; | ||
import axios from "axios"; | ||
import toast from "react-hot-toast"; | ||
import { FileMetadata } from "@/types/fileMetadata"; | ||
import Seperator from "./seperator"; | ||
|
||
type TFileDataModalProps = { | ||
filename?: string; | ||
type?: "images" | "documents"; | ||
}; | ||
|
||
const FileDataModal: React.FC<TFileDataModalProps> = ({ filename, type }) => { | ||
const [data, setData] = useState<FileMetadata>(); | ||
useEffect(() => { | ||
if (filename && type) { | ||
axios | ||
.get<FileMetadata>( | ||
`/api/cdn/${type === "documents" ? "doc" : "image"}/${filename}` | ||
) | ||
.then((res) => { | ||
toast.dismiss(); | ||
if (res.status === 200) { | ||
setData(res.data); | ||
} | ||
}) | ||
.catch((err) => { | ||
toast.dismiss(); | ||
toast.error(err.message); | ||
}); | ||
} | ||
axios; | ||
}, [filename, type]); | ||
if (!data) | ||
return ( | ||
<DialogContent className="overflow-hidden"> | ||
<DialogHeader> | ||
<DialogTitle>{filename}</DialogTitle> | ||
</DialogHeader> | ||
Error fetching file data. | ||
</DialogContent> | ||
); | ||
return ( | ||
<DialogContent className="overflow-clip"> | ||
<DialogHeader className="w-[90%]"> | ||
<DialogTitle className="truncate">{filename}</DialogTitle> | ||
<Seperator /> | ||
</DialogHeader> | ||
|
||
<div className=""> | ||
<strong>Filename</strong> | ||
<p id="filename">{data.filename}</p> | ||
</div> | ||
<div className=""> | ||
<strong>File Size</strong> | ||
<p id="filename"> | ||
{data.file_size < 1000 && `${data.file_size} b`} | ||
{999999 > data.file_size && data.file_size >= 1000 && `${Math.round(data.file_size / 100) / 10} KB`} | ||
{1000000000 > data.file_size && | ||
data.file_size >= 1000000 && | ||
`${Math.round(data.file_size / 100000) / 10} MB`} | ||
{1000000000000 > data.file_size && | ||
data.file_size >= 1000000000 && | ||
`${Math.round(data.file_size / 100000000) / 10} GB`} | ||
{data.file_size >= 1000000000000 && | ||
`${Math.round(data.file_size / 100000000000) / 10} TB`} | ||
</p> | ||
</div> | ||
{type === "images" && ( | ||
<div className=""> | ||
<strong>Dimensions</strong> | ||
<p id="filename">Height: {data.height}px</p> | ||
<p id="filename">Width: {data.width}px</p> | ||
</div> | ||
|
||
)} | ||
</DialogContent> | ||
); | ||
}; | ||
|
||
export default FileDataModal; |
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,7 @@ | ||
export type FileMetadata = { | ||
download_url: string; | ||
file_size: number; | ||
filename: string; | ||
height?: number; | ||
width?: number; | ||
}; |
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,2 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type SetAtom<Args extends any[], Result> = (...args: Args) => Result; |