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 scan session orchestration from App (#139)

Vishnu Srivatsava8fb46a60f936
diff --git a/src/App.tsx b/src/App.tsx
index f39fb71..c0732df 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,209 +1,27 @@
-import { useState, useCallback, useRef } from "react";
-import { deleteScanNode, getScanNode } from "./scanIpc";
-import { checkAdb, confirmDaemonShutdown, startDaemon, stopDaemon } from "./daemonIpc";
-import { formatBytes } from "./formatBytes";
-import { sameScan } from "./scanView";
-import type { ScanResponse, NodeDetail } from "./scanView";
+import { useState, useCallback } from "react";
import { DeleteConfirmation } from "./DeleteConfirmation";
import { ToastContainer } from "./ToastContainer";
import { useToasts } from "./useToasts";
import { TerminalLog } from "./TerminalLog";
import { appendLog, emptyLogHistory } from "./logHistory";
-import { useDevices } from "./useDevices";
import { ScanningScreen } from "./ScanningScreen";
-import { useScanRequest } from "./useScanRequest";
import { SetupScreen } from "./SetupScreen";
import { ResultScreen } from "./ResultScreen";
+import { useScanSession } from "./useScanSession";
import "./App.css";
-// ── Types ───────────────────────────────────────────────────────────────────
-
-/** One-line phase breakdown for the terminal log; null if the daemon predates #3. */
-const scanPhaseLog = (r: ScanResponse): string | null => {
- if (r.traverse_ms === undefined) return null;
- const kb = r.bytes_sent !== undefined ? `, ${(r.bytes_sent / 1024).toFixed(0)} KB sent` : "";
- return `[SCAN] ${r.streamed ? "Stream timings (overlap): scan" : "Phases: traverse"} ${r.traverse_ms}ms · sort ${r.sort_ms}ms · serialize ${r.serialize_ms}ms · send ${r.send_ms}ms${kb}`;
-};
-
-type AppPhase = "setup" | "connecting" | "scanning" | "result";
-
function App() {
- const [phase, setPhase] = useState<AppPhase>("setup");
- const deviceSelection = useDevices(phase === "setup");
- const { serial: selectedSerial, ready: deviceReady } = deviceSelection;
- const [rootMode, setRootMode] = useState(false);
- const [rootPath, setRootPath] = useState("/data");
- const [scanData, setScanData] = useState<ScanResponse | null>(null);
- const snapshot = useRef<ScanResponse | null>(null);
- const [busy, setBusy] = useState(false);
- const actionPending = useRef(false);
- const confirmationRequest = useRef(0);
- const [confirmDelete, setConfirmDelete] = useState<(NodeDetail & { trigger: HTMLButtonElement }) | null>(null);
- const [statusText, setStatusText] = useState("Initializing…");
const { toasts, pushToast, dismissToast, removeToast } = useToasts();
- const { scanProgress, cancelling, runScan, cancelScan } = useScanRequest(pushToast);
const [logs, setLogs] = useState(emptyLogHistory);
-
const addLog = useCallback((msg: string) => {
const entry = `[${new Date().toLocaleTimeString()}] ${msg}`;
- setLogs((prev) => appendLog(prev, entry));
+ setLogs(prev => appendLog(prev, entry));
}, []);
-
- const requestScan = useCallback((path: string) => {
- snapshot.current = null;
- confirmationRequest.current += 1;
- setConfirmDelete(null);
- return runScan(path);
- }, [runScan]);
-
- // ── Connect flow ────────────────────────────────────────────────────────
- const handleConnect = useCallback(async () => {
- if (actionPending.current || !deviceReady || !selectedSerial) return;
- actionPending.current = true;
- setPhase("connecting");
- const scanPath = rootMode ? rootPath : "/sdcard";
- let initialized = false;
- try {
- setStatusText("Checking ADB…");
- addLog("[ADB] Checking version...");
- const adbVersion = await checkAdb();
- addLog(`[ADB] Found: ${adbVersion}`);
-
- setStatusText(rootMode ? "Requesting root access…" : "Pushing daemon to device…");
- addLog(`[ADB] Starting an owned daemon session on ${selectedSerial}...`);
- await startDaemon(selectedSerial, rootMode);
- initialized = true;
- addLog("[SOCKET] Daemon started and connected via TCP tunnel!");
-
- setPhase("scanning");
- setStatusText(`Scanning ${scanPath} …`);
- addLog(`[SCAN] Requesting ${scanPath}${rootMode ? " with read-only root access" : ""}...`);
- const parsed = await requestScan(scanPath);
-
- if (parsed.status !== "ok") throw new Error("Scan returned non-ok status");
-
- snapshot.current = parsed;
- setScanData(parsed);
- setPhase("result");
- addLog(`[SCAN] Complete! ${parsed.total_files} files, ${parsed.scan_time_ms}ms.`);
- const phases = scanPhaseLog(parsed);
- if (phases) addLog(phases);
- } catch (err: unknown) {
- if (initialized) {
- try { await stopDaemon(); } catch { /* show the original connection error */ }
- }
- const message = err instanceof Error ? err.message : String(err);
- pushToast(message.split("\n", 1)[0], message === "Scan cancelled" ? "info" : "error");
- addLog(`${message === "Scan cancelled" ? "[SCAN]" : "[ERROR]"} ${message}`);
- setPhase("setup");
- } finally {
- actionPending.current = false;
- }
- }, [pushToast, addLog, rootMode, rootPath, requestScan, selectedSerial, deviceReady]);
-
- // ── Rescan ──────────────────────────────────────────────────────────────
- const handleRescan = useCallback(async () => {
- if (!scanData || actionPending.current) return;
- actionPending.current = true;
- const scanPath = scanData.root_path;
- setPhase("scanning");
- setStatusText(`Re-scanning ${scanPath} …`);
- addLog(`[SCAN] Re-scanning ${scanPath}...`);
- try {
- const parsed = await requestScan(scanPath);
- snapshot.current = parsed;
- setScanData(parsed);
- setPhase("result");
- addLog(`[SCAN] Complete! ${parsed.scan_time_ms}ms.`);
- const phases = scanPhaseLog(parsed);
- if (phases) addLog(phases);
- } catch (err: unknown) {
- const message = err instanceof Error ? err.message : String(err);
- pushToast(message, message === "Scan cancelled" ? "info" : "error");
- addLog(`${message === "Scan cancelled" ? "[SCAN]" : "[ERROR]"} ${message}`);
- // The failed generation is not an authoritative deletion root. Clear
- // old results rather than offering actions backed by an invalid scan.
- try { await stopDaemon(); } catch { /* retain the scan error */ }
- setScanData(null);
- setPhase("setup");
- } finally {
- actionPending.current = false;
- }
- }, [pushToast, addLog, scanData, requestScan]);
-
- // ── Disconnect ──────────────────────────────────────────────────────────
- const handleDisconnect = useCallback(async () => {
- if (actionPending.current) return;
- actionPending.current = true;
- setBusy(true);
- snapshot.current = null;
- confirmationRequest.current += 1;
- setConfirmDelete(null);
- addLog("[SOCKET] Disconnecting daemon...");
- try {
- await confirmDaemonShutdown();
- pushToast("Daemon shutdown acknowledged", "info");
- addLog("[SOCKET] Daemon acknowledged shutdown. App disconnected.");
- } catch (err) {
- const message = err instanceof Error ? err.message : String(err);
- pushToast("App disconnected; daemon shutdown could not be confirmed", "info");
- addLog(`[SOCKET] App disconnected; daemon shutdown could not be confirmed: ${message}`);
- } finally {
- setScanData(null);
- setPhase("setup");
- actionPending.current = false;
- setBusy(false);
- }
- }, [pushToast, addLog]);
-
- // Resolve confirmation details separately; destructive IPC accepts only IDs.
- const handleNuke = useCallback(async (nodeId: number, trigger: HTMLButtonElement) => {
- const scope = snapshot.current;
- if (!scope || scope.read_only || actionPending.current) return;
- const request = ++confirmationRequest.current;
- try {
- const detail = await getScanNode(scope, nodeId);
- if (request === confirmationRequest.current && !actionPending.current && sameScan(snapshot.current, scope) && detail.node.can_delete) {
- setConfirmDelete({ ...detail, trigger });
- }
- } catch (err) {
- if (request === confirmationRequest.current && sameScan(snapshot.current, scope)) pushToast(String(err), "error");
- }
- }, [pushToast]);
-
- const executeDelete = useCallback(async () => {
- const target = confirmDelete;
- confirmationRequest.current += 1;
- setConfirmDelete(null);
- if (!target || actionPending.current || !sameScan(snapshot.current, target)) return;
- actionPending.current = true;
- setBusy(true);
- addLog(`[DELETE] Requesting deletion of ${target.path}...`);
- try {
- const result = await deleteScanNode(target, target.node.id);
- if (!sameScan(snapshot.current, target)) return;
- snapshot.current = result.summary;
- setScanData(result.summary);
- pushToast(`Successfully deleted ${target.node.name}`, "success");
- addLog(`[DELETE] Success: ${result.message}`);
- addLog(`[DELETE] Scan updated (removed ${result.removed.files} files, ${result.removed.dirs} dirs, ${formatBytes(result.removed.size)})`);
- } catch (err: unknown) {
- if (!sameScan(snapshot.current, target)) return;
- const message = err instanceof Error ? err.message : String(err);
- snapshot.current = null;
- setScanData(null);
- setPhase("connecting");
- pushToast(`Delete failed: ${message}. Reconnect and rescan before continuing.`, "error");
- addLog(`[ERROR] Delete failed: ${message}`);
- // The daemon may have partially deleted a directory before reporting an
- // error. Discard its snapshot; never offer a blind retry with stale IDs.
- try { await stopDaemon(); } catch { /* retain the deletion error */ }
- setPhase("setup");
- } finally {
- actionPending.current = false;
- setBusy(false);
- }
- }, [confirmDelete, pushToast, addLog]);
+ const {
+ phase, deviceSelection, selectedSerial, rootMode, setRootMode, rootPath, setRootPath,
+ scanData, busy, statusText, scanProgress, cancelScan, cancelling, confirmDelete,
+ handleConnect, handleRescan, handleDisconnect, handleNuke, executeDelete, cancelDelete,
+ } = useScanSession(pushToast, addLog);
return (
<div className="h-screen flex flex-col bg-surface-0 overflow-hidden">
@@ -237,11 +55,10 @@ function App() {
<TerminalLog history={logs} />
- {/* Custom Confirm Modal */}
{confirmDelete && <DeleteConfirmation
path={confirmDelete.path}
trigger={confirmDelete.trigger}
- onCancel={() => { confirmationRequest.current += 1; setConfirmDelete(null); }}
+ onCancel={cancelDelete}
onConfirm={executeDelete}
/>}
</div>
diff --git a/src/useScanSession.ts b/src/useScanSession.ts
new file mode 100644
index 0000000..5c5d747
--- /dev/null
+++ b/src/useScanSession.ts
@@ -0,0 +1,200 @@
+import { useState, useCallback, useRef } from "react";
+import { deleteScanNode, getScanNode } from "./scanIpc";
+import { checkAdb, confirmDaemonShutdown, startDaemon, stopDaemon } from "./daemonIpc";
+import { formatBytes } from "./formatBytes";
+import { sameScan } from "./scanView";
+import type { ScanResponse, NodeDetail } from "./scanView";
+import { useDevices } from "./useDevices";
+import { useScanRequest } from "./useScanRequest";
+import type { Toast } from "./ToastContainer";
+
+type PushToast = (message: string, type?: Toast["type"]) => void;
+
+/** One-line phase breakdown for the terminal log; null if the daemon predates #3. */
+const scanPhaseLog = (r: ScanResponse): string | null => {
+ if (r.traverse_ms === undefined) return null;
+ const kb = r.bytes_sent !== undefined ? `, ${(r.bytes_sent / 1024).toFixed(0)} KB sent` : "";
+ return `[SCAN] ${r.streamed ? "Stream timings (overlap): scan" : "Phases: traverse"} ${r.traverse_ms}ms · sort ${r.sort_ms}ms · serialize ${r.serialize_ms}ms · send ${r.send_ms}ms${kb}`;
+};
+
+type AppPhase = "setup" | "connecting" | "scanning" | "result";
+
+export function useScanSession(pushToast: PushToast, addLog: (message: string) => void) {
+ const [phase, setPhase] = useState<AppPhase>("setup");
+ const deviceSelection = useDevices(phase === "setup");
+ const { serial: selectedSerial, ready: deviceReady } = deviceSelection;
+ const [rootMode, setRootMode] = useState(false);
+ const [rootPath, setRootPath] = useState("/data");
+ const [scanData, setScanData] = useState<ScanResponse | null>(null);
+ const snapshot = useRef<ScanResponse | null>(null);
+ const [busy, setBusy] = useState(false);
+ const actionPending = useRef(false);
+ const confirmationRequest = useRef(0);
+ const [confirmDelete, setConfirmDelete] = useState<(NodeDetail & { trigger: HTMLButtonElement }) | null>(null);
+ const [statusText, setStatusText] = useState("Initializing…");
+ const { scanProgress, cancelling, runScan, cancelScan } = useScanRequest(pushToast);
+ const requestScan = useCallback((path: string) => {
+ snapshot.current = null;
+ confirmationRequest.current += 1;
+ setConfirmDelete(null);
+ return runScan(path);
+ }, [runScan]);
+
+ // ── Connect flow ────────────────────────────────────────────────────────
+ const handleConnect = useCallback(async () => {
+ if (actionPending.current || !deviceReady || !selectedSerial) return;
+ actionPending.current = true;
+ setPhase("connecting");
+ const scanPath = rootMode ? rootPath : "/sdcard";
+ let initialized = false;
+ try {
+ setStatusText("Checking ADB…");
+ addLog("[ADB] Checking version...");
+ const adbVersion = await checkAdb();
+ addLog(`[ADB] Found: ${adbVersion}`);
+
+ setStatusText(rootMode ? "Requesting root access…" : "Pushing daemon to device…");
+ addLog(`[ADB] Starting an owned daemon session on ${selectedSerial}...`);
+ await startDaemon(selectedSerial, rootMode);
+ initialized = true;
+ addLog("[SOCKET] Daemon started and connected via TCP tunnel!");
+
+ setPhase("scanning");
+ setStatusText(`Scanning ${scanPath} …`);
+ addLog(`[SCAN] Requesting ${scanPath}${rootMode ? " with read-only root access" : ""}...`);
+ const parsed = await requestScan(scanPath);
+
+ snapshot.current = parsed;
+ setScanData(parsed);
+ setPhase("result");
+ addLog(`[SCAN] Complete! ${parsed.total_files} files, ${parsed.scan_time_ms}ms.`);
+ const phases = scanPhaseLog(parsed);
+ if (phases) addLog(phases);
+ } catch (err: unknown) {
+ if (initialized) {
+ try { await stopDaemon(); } catch { /* show the original connection error */ }
+ }
+ const message = err instanceof Error ? err.message : String(err);
+ pushToast(message.split("\n", 1)[0], message === "Scan cancelled" ? "info" : "error");
+ addLog(`${message === "Scan cancelled" ? "[SCAN]" : "[ERROR]"} ${message}`);
+ setPhase("setup");
+ } finally {
+ actionPending.current = false;
+ }
+ }, [pushToast, addLog, rootMode, rootPath, requestScan, selectedSerial, deviceReady]);
+
+ // ── Rescan ──────────────────────────────────────────────────────────────
+ const handleRescan = useCallback(async () => {
+ if (!scanData || actionPending.current) return;
+ actionPending.current = true;
+ const scanPath = scanData.root_path;
+ setPhase("scanning");
+ setStatusText(`Re-scanning ${scanPath} …`);
+ addLog(`[SCAN] Re-scanning ${scanPath}...`);
+ try {
+ const parsed = await requestScan(scanPath);
+ snapshot.current = parsed;
+ setScanData(parsed);
+ setPhase("result");
+ addLog(`[SCAN] Complete! ${parsed.scan_time_ms}ms.`);
+ const phases = scanPhaseLog(parsed);
+ if (phases) addLog(phases);
+ } catch (err: unknown) {
+ const message = err instanceof Error ? err.message : String(err);
+ pushToast(message, message === "Scan cancelled" ? "info" : "error");
+ addLog(`${message === "Scan cancelled" ? "[SCAN]" : "[ERROR]"} ${message}`);
+ // The failed generation is not an authoritative deletion root. Clear
+ // old results rather than offering actions backed by an invalid scan.
+ try { await stopDaemon(); } catch { /* retain the scan error */ }
+ setScanData(null);
+ setPhase("setup");
+ } finally {
+ actionPending.current = false;
+ }
+ }, [pushToast, addLog, scanData, requestScan]);
+
+ // ── Disconnect ──────────────────────────────────────────────────────────
+ const handleDisconnect = useCallback(async () => {
+ if (actionPending.current) return;
+ actionPending.current = true;
+ setBusy(true);
+ snapshot.current = null;
+ confirmationRequest.current += 1;
+ setConfirmDelete(null);
+ addLog("[SOCKET] Disconnecting daemon...");
+ try {
+ await confirmDaemonShutdown();
+ pushToast("Daemon shutdown acknowledged", "info");
+ addLog("[SOCKET] Daemon acknowledged shutdown. App disconnected.");
+ } catch (err) {
+ const message = err instanceof Error ? err.message : String(err);
+ pushToast("App disconnected; daemon shutdown could not be confirmed", "info");
+ addLog(`[SOCKET] App disconnected; daemon shutdown could not be confirmed: ${message}`);
+ } finally {
+ setScanData(null);
+ setPhase("setup");
+ actionPending.current = false;
+ setBusy(false);
+ }
+ }, [pushToast, addLog]);
+
+ // Resolve confirmation details separately; destructive IPC accepts only IDs.
+ const handleNuke = useCallback(async (nodeId: number, trigger: HTMLButtonElement) => {
+ const scope = snapshot.current;
+ if (!scope || scope.read_only || actionPending.current) return;
+ const request = ++confirmationRequest.current;
+ try {
+ const detail = await getScanNode(scope, nodeId);
+ if (request === confirmationRequest.current && !actionPending.current && sameScan(snapshot.current, scope) && detail.node.can_delete) {
+ setConfirmDelete({ ...detail, trigger });
+ }
+ } catch (err) {
+ if (request === confirmationRequest.current && sameScan(snapshot.current, scope)) pushToast(String(err), "error");
+ }
+ }, [pushToast]);
+
+ const executeDelete = useCallback(async () => {
+ const target = confirmDelete;
+ confirmationRequest.current += 1;
+ setConfirmDelete(null);
+ if (!target || actionPending.current || !sameScan(snapshot.current, target)) return;
+ actionPending.current = true;
+ setBusy(true);
+ addLog(`[DELETE] Requesting deletion of ${target.path}...`);
+ try {
+ const result = await deleteScanNode(target, target.node.id);
+ if (!sameScan(snapshot.current, target)) return;
+ snapshot.current = result.summary;
+ setScanData(result.summary);
+ pushToast(`Successfully deleted ${target.node.name}`, "success");
+ addLog(`[DELETE] Success: ${result.message}`);
+ addLog(`[DELETE] Scan updated (removed ${result.removed.files} files, ${result.removed.dirs} dirs, ${formatBytes(result.removed.size)})`);
+ } catch (err: unknown) {
+ if (!sameScan(snapshot.current, target)) return;
+ const message = err instanceof Error ? err.message : String(err);
+ snapshot.current = null;
+ setScanData(null);
+ setPhase("connecting");
+ pushToast(`Delete failed: ${message}. Reconnect and rescan before continuing.`, "error");
+ addLog(`[ERROR] Delete failed: ${message}`);
+ // The daemon may have partially deleted a directory before reporting an
+ // error. Discard its snapshot; never offer a blind retry with stale IDs.
+ try { await stopDaemon(); } catch { /* retain the deletion error */ }
+ setPhase("setup");
+ } finally {
+ actionPending.current = false;
+ setBusy(false);
+ }
+ }, [confirmDelete, pushToast, addLog]);
+
+ const cancelDelete = () => {
+ confirmationRequest.current += 1;
+ setConfirmDelete(null);
+ };
+
+ return {
+ phase, deviceSelection, selectedSerial, rootMode, setRootMode, rootPath, setRootPath,
+ scanData, busy, statusText, scanProgress, cancelScan, cancelling, confirmDelete,
+ handleConnect, handleRescan, handleDisconnect, handleNuke, executeDelete, cancelDelete,
+ };
+}