medusa-plugin-imagekit
Version:
Imagekit file connector for Medusa v2
134 lines (133 loc) • 4.8 kB
JavaScript
import { jsxs, jsx } from "react/jsx-runtime";
import { toast } from "@medusajs/ui";
import { defineRouteConfig } from "@medusajs/admin-sdk";
import { useLoaderData } from "react-router-dom";
function LogoSquare() {
return /* @__PURE__ */ jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 512 512", children: [
/* @__PURE__ */ jsx("defs", {}),
/* @__PURE__ */ jsx("path", { d: "M0 0h512v512H0z", fill: "#0450d5" }),
/* @__PURE__ */ jsx("path", { fill: "#fff", fillRule: "evenodd", d: "M226.05 147.5c-6.78-7.34-10.16-15.81-10.16-27.1.56-10.73 4-19.2 11.29-27.11C235.08 86 243.55 82 254.85 82s19.15 4 26.53 11.29c7.91 7.91 11.86 16.38 11.86 27.11 0 11.29-4.52 19.76-11.86 27.1a38.79 38.79 0 0 1-27.66 10.73c-10.72 0-20.33-3.39-27.67-10.73Zm110.1 193.67c-38.39 104.46-184.07 132.69-157-2.82l31.62-153h66.63c-15.24 81.87-26.54 131.56-37.83 188.59-11.29 58.16 61 15.25 77.36-32.18l19.19-.57Z" })
] });
}
function formatBytes(bytes, decimals = 0) {
if (bytes === 0) return { value: "0", unit: "Bytes" };
const k = 1024;
const units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = parseFloat((bytes / Math.pow(k, i)).toFixed(decimals));
return {
value: value.toString(),
unit: units[i]
};
}
function formatUsageStatistics(data) {
return Object.keys(data).map((v) => {
switch (v) {
case "bandwidthBytes":
let bwb = formatBytes(data[v], 2);
return {
name: "Bandwidth",
value: bwb.value,
unit: bwb.unit
};
case "mediaLibraryStorageBytes":
let msb = formatBytes(data[v], 2);
return {
name: "Current Media library storage",
value: msb.value,
unit: msb.unit
};
case "videoProcessingUnitsCount":
return {
name: "Video processing units",
value: data[v]
};
case "extensionUnitsCount":
return {
name: "Extension units",
value: data[v]
};
case "originalCacheStorageBytes":
let ocsb = formatBytes(data[v], 2);
return {
name: "Current Original cache storage",
value: ocsb.value,
unit: ocsb.unit
};
}
});
}
function UsageStatistics({ data = {
bandwidthBytes: 0,
mediaLibraryStorageBytes: 0,
videoProcessingUnitsCount: 0,
extensionUnitsCount: 0,
originalCacheStorageBytes: 0
} }) {
const usageArr = formatUsageStatistics(data);
return /* @__PURE__ */ jsxs("div", { className: "w-full p-4", children: [
/* @__PURE__ */ jsx("h3", { className: "text-base font-semibold text-gray-900 dark:text-white", children: "Usage this month" }),
/* @__PURE__ */ jsx("dl", { className: "mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3", children: usageArr.map((item) => /* @__PURE__ */ jsxs("div", { className: "overflow-hidden rounded-lg bg-white dark:bg-zinc-800/50 px-4 py-5 shadow-sm sm:p-6", children: [
/* @__PURE__ */ jsx("dt", { title: item.name, className: "truncate text-sm font-medium text-gray-500 dark:text-white/50", children: item.name }),
/* @__PURE__ */ jsxs("dd", { className: "mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-white/80", children: [
item.value,
item.unit && /* @__PURE__ */ jsx("span", { className: "pl-2 text-sm", children: item.unit })
] })
] }, item.name)) })
] });
}
async function loader() {
const url = `${__BACKEND_URL__ || ""}/admin/plugin/imagekit/usage`;
const res = await fetch(url, {
method: "GET",
credentials: "include"
});
const { result } = await res.json();
return { useStatistics: result };
}
const ImagekitPage = () => {
const { useStatistics } = useLoaderData();
if (useStatistics.message) {
toast.error(useStatistics == null ? void 0 : useStatistics.message);
return /* @__PURE__ */ jsx("div", { className: "flex flex-col items-center", children: /* @__PURE__ */ jsx(UsageStatistics, {}) });
}
return /* @__PURE__ */ jsx("div", { className: "flex flex-col items-center", children: /* @__PURE__ */ jsx(UsageStatistics, { data: useStatistics }) });
};
const config = defineRouteConfig({
label: "Imagekit",
icon: LogoSquare
});
const widgetModule = { widgets: [] };
const routeModule = {
routes: [
{
Component: ImagekitPage,
path: "/imagekit",
loader
}
]
};
const menuItemModule = {
menuItems: [
{
label: config.label,
icon: config.icon,
path: "/imagekit",
nested: void 0
}
]
};
const formModule = { customFields: {} };
const displayModule = {
displays: {}
};
const plugin = {
widgetModule,
routeModule,
menuItemModule,
formModule,
displayModule
};
export {
plugin as default
};