From bfc2cec7d43eb8eaa46dd3f91084932381257059 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:35 -0400 Subject: refactor: excalidraw-app/ --- excalidraw-app/CustomStats.tsx | 85 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 excalidraw-app/CustomStats.tsx (limited to 'excalidraw-app/CustomStats.tsx') diff --git a/excalidraw-app/CustomStats.tsx b/excalidraw-app/CustomStats.tsx new file mode 100644 index 0000000..96ca55d --- /dev/null +++ b/excalidraw-app/CustomStats.tsx @@ -0,0 +1,85 @@ +import { useEffect, useState } from "react"; +import { debounce, getVersion, nFormatter } from "@excalidraw/excalidraw/utils"; +import { + getElementsStorageSize, + getTotalStorageSize, +} from "./data/localStorage"; +import { DEFAULT_VERSION } from "@excalidraw/excalidraw/constants"; +import { t } from "@excalidraw/excalidraw/i18n"; +import { copyTextToSystemClipboard } from "@excalidraw/excalidraw/clipboard"; +import type { NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types"; +import type { UIAppState } from "@excalidraw/excalidraw/types"; +import { Stats } from "@excalidraw/excalidraw"; + +type StorageSizes = { scene: number; total: number }; + +const STORAGE_SIZE_TIMEOUT = 500; + +const getStorageSizes = debounce((cb: (sizes: StorageSizes) => void) => { + cb({ + scene: getElementsStorageSize(), + total: getTotalStorageSize(), + }); +}, STORAGE_SIZE_TIMEOUT); + +type Props = { + setToast: (message: string) => void; + elements: readonly NonDeletedExcalidrawElement[]; + appState: UIAppState; +}; +const CustomStats = (props: Props) => { + const [storageSizes, setStorageSizes] = useState({ + scene: 0, + total: 0, + }); + + useEffect(() => { + getStorageSizes((sizes) => { + setStorageSizes(sizes); + }); + }, [props.elements, props.appState]); + useEffect(() => () => getStorageSizes.cancel(), []); + + const version = getVersion(); + let hash; + let timestamp; + + if (version !== DEFAULT_VERSION) { + timestamp = version.slice(0, 16).replace("T", " "); + hash = version.slice(21); + } else { + timestamp = t("stats.versionNotAvailable"); + } + + return ( + + {t("stats.version")} + { + try { + await copyTextToSystemClipboard(getVersion()); + props.setToast(t("toast.copyToClipboard")); + } catch {} + }} + title={t("stats.versionCopy")} + > + {timestamp} +
+ {hash} +
+ + {t("stats.storage")} + +
{t("stats.scene")}
+
{nFormatter(storageSizes.scene, 1)}
+
+ +
{t("stats.total")}
+
{nFormatter(storageSizes.total, 1)}
+
+
+ ); +}; + +export default CustomStats; -- cgit v1.2.3