Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Extract toast state from App (#124)

Vishnu Srivatsavafb2dd22391d6
diff --git a/src/App.tsx b/src/App.tsx
index 68a55d9..039bca6 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -5,7 +5,7 @@ import { sameScan, parseNodeDetail } from "./scanView";
import type { ScanResponse, NodeDetail, DeleteResult } from "./scanView";
import { IconNuke } from "./Icons";
import { ToastContainer } from "./ToastContainer";
-import type { Toast } from "./ToastContainer";
+import { useToasts } from "./useToasts";
import { TerminalLog } from "./TerminalLog";
import { appendLog, emptyLogHistory } from "./logHistory";
import { useDevices } from "./useDevices";
@@ -30,10 +30,6 @@ function formatNumber(n: number): string {
return n.toLocaleString("en-US");
}
-// ── Toast System ────────────────────────────────────────────────────────────
-
-let toastId = 0;
-
// ── Scanning Screen ─────────────────────────────────────────────────────────
interface ScanProgress {
@@ -109,7 +105,7 @@ function App() {
const [cancelling, setCancelling] = useState(false);
const activeScan = useRef<string | null>(null);
const [statusText, setStatusText] = useState("Initializing…");
- const [toasts, setToasts] = useState<Toast[]>([]);
+ const { toasts, pushToast, dismissToast, removeToast } = useToasts();
const [logs, setLogs] = useState(emptyLogHistory);
const addLog = useCallback((msg: string) => {
@@ -117,22 +113,6 @@ function App() {
setLogs((prev) => appendLog(prev, entry));
}, []);
- // ── Toast helpers ───────────────────────────────────────────────────────
- const pushToast = useCallback((message: string, type: Toast["type"] = "error") => {
- const id = ++toastId;
- setToasts((prev) => [...prev, { id, message, type }]);
- }, []);
-
- const dismissToast = useCallback((id: number) => {
- setToasts((prev) =>
- prev.map((t) => (t.id === id && !t.exiting ? { ...t, exiting: true } : t))
- );
- }, []);
-
- const removeToast = useCallback((id: number) => {
- setToasts((prev) => prev.filter((t) => t.id !== id));
- }, []);
-
const requestScan = useCallback(async (path: string): Promise<ScanResponse> => {
snapshot.current = null;
confirmationRequest.current += 1;
diff --git a/src/useToasts.ts b/src/useToasts.ts
new file mode 100644
index 0000000..9c03087
--- /dev/null
+++ b/src/useToasts.ts
@@ -0,0 +1,24 @@
+import { useCallback, useRef, useState } from "react";
+import type { Toast } from "./ToastContainer";
+
+export function useToasts() {
+ const nextId = useRef(0);
+ const [toasts, setToasts] = useState<Toast[]>([]);
+
+ const pushToast = useCallback((message: string, type: Toast["type"] = "error") => {
+ const id = ++nextId.current;
+ setToasts((prev) => [...prev, { id, message, type }]);
+ }, []);
+
+ const dismissToast = useCallback((id: number) => {
+ setToasts((prev) =>
+ prev.map((t) => (t.id === id && !t.exiting ? { ...t, exiting: true } : t))
+ );
+ }, []);
+
+ const removeToast = useCallback((id: number) => {
+ setToasts((prev) => prev.filter((t) => t.id !== id));
+ }, []);
+
+ return { toasts, pushToast, dismissToast, removeToast };
+}