Skip to content

Commit

Permalink
[Feature] Add content size to sidebar (#46)
Browse files Browse the repository at this point in the history
* chore(deps): add jotai

* chore(ui): add store for contentsize

* feat(ui): add content-size component

* chore: add sizeLoading to store

* refactor: move getSize to separate file

* feat: refetch size on file upload
  • Loading branch information
kevinanielsen authored Dec 20, 2023
1 parent 3ad9ac3 commit 1180913
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint": "^8.53.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.4",
"jotai": "^2.6.0",
"postcss": "^8.4.32",
"react-hot-toast": "^2.4.1",
"tailwindcss": "^3.3.6",
Expand Down
19 changes: 19 additions & 0 deletions ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Files from "./components/files";
import { Image, Upload as UploadIcon, Files as FilesIcon } from "lucide-react";
import Seperator from "./components/seperator";
import Upload from "./components/upload";
import ContentSize from "./components/content-size";

function App() {
return (
Expand Down Expand Up @@ -36,6 +37,7 @@ function App() {
</Link>
</li>
</ul>
<ContentSize />
</nav>
<main className="m-4 h-auto flex flex-col w-full">
<Route path="/images">{<Files type="images" />}</Route>
Expand Down
25 changes: 25 additions & 0 deletions ui/src/actions/getSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import axios from "axios";
import { SetStateAction } from "jotai";
import toast from "react-hot-toast";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SetAtom<Args extends any[], Result> = (...args: Args) => Result;

export const getSize = (
setSize: SetAtom<[SetStateAction<number>], void>,
setLoading: React.Dispatch<React.SetStateAction<boolean>>
) => {
setLoading(true);
axios
.get<{ cdn_size_bytes: number }>("/api/cdn/size")
.then((res) => {
setSize(res.data.cdn_size_bytes);
})
.catch((err) => {
toast.error("Error getting content size");
console.log(err);
})
.finally(() => {
setLoading(false);
});
};
40 changes: 40 additions & 0 deletions ui/src/components/content-size.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useAtom } from "jotai";
import { sizeAtom, sizeLoadingAtom } from "../store";
import { useEffect } from "react";
import { getSize } from "../actions/getSize";

const ContentSize = () => {
const [size, setSize] = useAtom(sizeAtom);
const [loading, setLoading] = useAtom(sizeLoadingAtom);

useEffect(() => {
getSize(setSize, setLoading);
}, [setSize, setLoading]);

if (loading)
return (
<div className="bottom-0 absolute mb-4">
<span>Total content size:</span>
<p>Loading...</p>
</div>
);

return (
<div className="bottom-0 absolute mb-4">
<span>Total content size:</span>
<p className="font-bold">
{size < 1000 && `${size} b`}
{999999 > size && size >= 1000 && `${Math.round(size / 100) / 10} KB`}
{1000000000 > size &&
size >= 1000000 &&
`${Math.round(size / 100000) / 10} MB`}
{1000000000000 > size &&
size >= 1000000000 &&
`${Math.round(size / 100000000) / 10} GB`}
{size >= 1000000000000 && `${Math.round(size / 100000000000) / 10} TB`}
</p>
</div>
);
};

export default ContentSize;
9 changes: 9 additions & 0 deletions ui/src/components/upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import Seperator from "./seperator";
import axios from "axios";
import toast from "react-hot-toast";
import { useParams } from "wouter";
import { sizeAtom, sizeLoadingAtom } from "../store";
import { useAtom } from "jotai";
import { getSize } from "../actions/getSize";

const Upload = () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setLoading] = useAtom(sizeLoadingAtom);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [__, setSize] = useAtom(sizeAtom);

const tab = useParams<{ tab: "images" | "docs" }>().tab;

if (tab === undefined || tab === null) {
Expand All @@ -30,6 +38,7 @@ const Upload = () => {
if (res.status === 200) {
toast.dismiss();
toast.success("Successfully uploaded file!");
getSize(setSize, setLoading);
}
})
.catch((err: Error) => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { atom } from "jotai";

export const sizeAtom = atom(0);
export const sizeLoadingAtom = atom(false);

0 comments on commit 1180913

Please sign in to comment.