2022-03-04 12:53:37 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2022-02-26 12:34:07 +00:00
|
|
|
import fileSize from "pretty-bytes";
|
2022-02-23 10:49:57 +00:00
|
|
|
import { Link } from "gatsby";
|
2022-03-04 12:53:37 +00:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
import { useUser } from "../../contexts/user";
|
|
|
|
import useActivePlan from "../../hooks/useActivePlan";
|
|
|
|
import { ContainerLoadingIndicator } from "../LoadingIndicator";
|
2022-02-23 10:49:57 +00:00
|
|
|
|
|
|
|
import { GraphBar } from "./GraphBar";
|
|
|
|
import { UsageGraph } from "./UsageGraph";
|
|
|
|
|
2022-03-04 12:53:37 +00:00
|
|
|
const useUsageData = () => {
|
|
|
|
const { user } = useUser();
|
|
|
|
const { activePlan, error } = useActivePlan(user);
|
|
|
|
const { data: stats, error: statsError } = useSWR("user/stats");
|
2022-03-08 08:37:24 +00:00
|
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
2022-03-04 12:53:37 +00:00
|
|
|
const [usage, setUsage] = useState({});
|
|
|
|
|
2022-03-08 08:37:24 +00:00
|
|
|
const hasError = error || statsError;
|
|
|
|
const hasData = activePlan && stats;
|
|
|
|
|
2022-03-04 12:53:37 +00:00
|
|
|
useEffect(() => {
|
2022-03-08 08:37:24 +00:00
|
|
|
if (hasData || hasError) {
|
2022-03-04 12:53:37 +00:00
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
|
2022-03-08 08:37:24 +00:00
|
|
|
if (hasData && !hasError) {
|
2022-03-04 12:53:37 +00:00
|
|
|
setUsage({
|
|
|
|
filesUsed: stats?.numUploads,
|
|
|
|
filesLimit: activePlan?.limits?.maxNumberUploads,
|
|
|
|
storageUsed: stats?.totalUploadsSize,
|
|
|
|
storageLimit: activePlan?.limits?.storageLimit,
|
|
|
|
});
|
|
|
|
}
|
2022-03-08 08:37:24 +00:00
|
|
|
}, [hasData, hasError, stats, activePlan]);
|
2022-03-04 12:53:37 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
error: error || statsError,
|
|
|
|
loading,
|
|
|
|
usage,
|
|
|
|
};
|
|
|
|
};
|
2022-02-23 10:49:57 +00:00
|
|
|
|
2022-02-26 12:34:07 +00:00
|
|
|
const size = (bytes) => {
|
2022-03-04 12:53:37 +00:00
|
|
|
const text = fileSize(bytes ?? 0, { maximumFractionDigits: 0 });
|
2022-02-26 12:34:07 +00:00
|
|
|
const [value, unit] = text.split(" ");
|
|
|
|
|
|
|
|
return {
|
|
|
|
text,
|
|
|
|
value,
|
|
|
|
unit,
|
|
|
|
};
|
|
|
|
};
|
2022-02-23 10:49:57 +00:00
|
|
|
|
2022-03-04 12:53:37 +00:00
|
|
|
const ErrorMessage = () => (
|
|
|
|
<div className="flex text-palette-300 flex-col space-y-4 h-full justify-center items-center">
|
|
|
|
<p>We were not able to fetch the current usage data.</p>
|
|
|
|
<p>We'll try again automatically.</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2022-02-23 10:49:57 +00:00
|
|
|
export default function CurrentUsage() {
|
2022-03-04 12:53:37 +00:00
|
|
|
const { usage, error, loading } = useUsageData();
|
|
|
|
const storageUsage = size(usage.storageUsed);
|
|
|
|
const storageLimit = size(usage.storageLimit);
|
|
|
|
const filesUsedLabel = useMemo(() => ({ value: usage.filesUsed, unit: "files" }), [usage.filesUsed]);
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <ContainerLoadingIndicator />;
|
|
|
|
}
|
2022-02-26 12:34:07 +00:00
|
|
|
|
2022-03-04 12:53:37 +00:00
|
|
|
if (error) {
|
|
|
|
return <ErrorMessage />;
|
|
|
|
}
|
2022-02-23 10:49:57 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-02-26 12:34:07 +00:00
|
|
|
<h4>
|
|
|
|
{storageUsage.text} of {storageLimit.text}
|
|
|
|
</h4>
|
2022-02-23 10:49:57 +00:00
|
|
|
<p className="text-palette-400">
|
2022-03-04 12:53:37 +00:00
|
|
|
{usage.filesUsed} of {usage.filesLimit} files
|
2022-02-23 10:49:57 +00:00
|
|
|
</p>
|
|
|
|
<div className="relative mt-7 font-sans uppercase text-xs">
|
|
|
|
<div className="flex place-content-between">
|
|
|
|
<span>Storage</span>
|
2022-02-26 12:34:07 +00:00
|
|
|
<span>{storageLimit.text}</span>
|
2022-02-23 10:49:57 +00:00
|
|
|
</div>
|
|
|
|
<UsageGraph>
|
2022-03-04 12:53:37 +00:00
|
|
|
<GraphBar value={usage.storageUsed} limit={usage.storageLimit} label={storageUsage} />
|
|
|
|
<GraphBar value={usage.filesUsed} limit={usage.filesLimit} label={filesUsedLabel} />
|
2022-02-23 10:49:57 +00:00
|
|
|
</UsageGraph>
|
|
|
|
<div className="flex place-content-between">
|
|
|
|
<span>Files</span>
|
|
|
|
<span className="inline-flex place-content-between w-[37%]">
|
|
|
|
<Link
|
|
|
|
to="/upgrade"
|
|
|
|
className="text-primary underline-offset-3 decoration-dotted hover:text-primary-light hover:underline"
|
|
|
|
>
|
|
|
|
UPGRADE
|
|
|
|
</Link>{" "}
|
|
|
|
{/* TODO: proper URL */}
|
2022-03-04 12:53:37 +00:00
|
|
|
<span>{usage.filesLimit}</span>
|
2022-02-23 10:49:57 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|