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 request lifecycle into a hook (#130)
Vishnu Srivatsava74266d97f204
diff --git a/src/App.tsx b/src/App.tsxindex 59bb2cc..5071496 100644--- a/src/App.tsx+++ b/src/App.tsx@@ -1,5 +1,5 @@import { useState, useCallback, useRef } from "react";-import { Channel, invoke } from "@tauri-apps/api/core";+import { invoke } from "@tauri-apps/api/core";import { formatBytes } from "./formatBytes";import { sameScan, parseNodeDetail } from "./scanView";import type { ScanResponse, NodeDetail, DeleteResult } from "./scanView";@@ -10,7 +10,7 @@ import { TerminalLog } from "./TerminalLog";import { appendLog, emptyLogHistory } from "./logHistory";import { useDevices } from "./useDevices";import { ScanningScreen } from "./ScanningScreen";-import type { ScanProgress } from "./ScanningScreen";+import { useScanRequest } from "./useScanRequest";import { SetupScreen } from "./SetupScreen";import { ResultScreen } from "./ResultScreen";import "./App.css";@@ -38,11 +38,9 @@ function App() {const actionPending = useRef(false);const confirmationRequest = useRef(0);const [confirmDelete, setConfirmDelete] = useState<(NodeDetail & { trigger: HTMLButtonElement }) | null>(null);- const [scanProgress, setScanProgress] = useState<ScanProgress | null>(null);- const [cancelling, setCancelling] = useState(false);- const activeScan = useRef<string | 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) => {@@ -50,39 +48,12 @@ function App() {setLogs((prev) => appendLog(prev, entry));}, []);- const requestScan = useCallback(async (path: string): Promise<ScanResponse> => {+ const requestScan = useCallback((path: string) => {snapshot.current = null;confirmationRequest.current += 1;setConfirmDelete(null);- const scanId = crypto.randomUUID();- activeScan.current = scanId;- setScanProgress(null);- setCancelling(false);- const onProgress = new Channel<ScanProgress>();- onProgress.onmessage = (progress) => {- if (activeScan.current === scanId && progress.scan_id === scanId) setScanProgress(progress);- void invoke("acknowledge_scan_progress", { scanId: progress.scan_id }).catch(() => {- // The completed/cancelled scan may already have released its channel.- });- };- try {- const raw = await invoke<string>("run_scan", { path, scanId, onProgress });- const parsed: ScanResponse = JSON.parse(raw);- if (parsed.status !== "ok") throw new Error("Scan returned non-ok status");- return parsed;- } finally {- if (activeScan.current === scanId) activeScan.current = null;- setCancelling(false);- }- }, []);-- const handleCancelScan = useCallback(async () => {- const scanId = activeScan.current;- if (!scanId) return;- setCancelling(true);- try { await invoke("cancel_scan", { scanId }); }- catch (err) { pushToast(String(err), "error"); setCancelling(false); }- }, [pushToast]);+ return runScan(path);+ }, [runScan]);// ── Connect flow ────────────────────────────────────────────────────────const handleConnect = useCallback(async () => {@@ -262,7 +233,7 @@ function App() {deviceSelection={deviceSelection}/>)}- {phase === "scanning" && <ScanningScreen statusText={statusText} progress={scanProgress} onCancel={handleCancelScan} cancelling={cancelling} />}+ {phase === "scanning" && <ScanningScreen statusText={statusText} progress={scanProgress} onCancel={cancelScan} cancelling={cancelling} />}{phase === "result" && scanData && (<ResultScreenkey={scanData.generation}diff --git a/src/ScanningScreen.tsx b/src/ScanningScreen.tsxindex d4b6bf5..9e33042 100644--- a/src/ScanningScreen.tsx+++ b/src/ScanningScreen.tsx@@ -1,17 +1,10 @@import { formatBytes } from "./formatBytes";+import type { ScanProgress } from "./useScanRequest";function formatNumber(n: number): string {return n.toLocaleString("en-US");}-export interface ScanProgress {- scan_id: string;- files: number;- directories: number;- total_size: number;- recent_names: string[];-}-export function ScanningScreen({ statusText, progress, onCancel, cancelling }: {statusText: string; progress: ScanProgress | null; onCancel: () => void; cancelling: boolean;}) {diff --git a/src/useScanRequest.ts b/src/useScanRequest.tsnew file mode 100644index 0000000..df54b48--- /dev/null+++ b/src/useScanRequest.ts@@ -0,0 +1,50 @@+import { useCallback, useRef, useState } from "react";+import { Channel, invoke } from "@tauri-apps/api/core";+import type { ScanResponse } from "./scanView";++export interface ScanProgress {+ scan_id: string;+ files: number;+ directories: number;+ total_size: number;+ recent_names: string[];+}++export function useScanRequest(onError: (message: string) => void) {+ const [scanProgress, setScanProgress] = useState<ScanProgress | null>(null);+ const [cancelling, setCancelling] = useState(false);+ const activeScan = useRef<string | null>(null);++ const runScan = useCallback(async (path: string): Promise<ScanResponse> => {+ const scanId = crypto.randomUUID();+ activeScan.current = scanId;+ setScanProgress(null);+ setCancelling(false);+ const onProgress = new Channel<ScanProgress>();+ onProgress.onmessage = (progress) => {+ if (activeScan.current === scanId && progress.scan_id === scanId) setScanProgress(progress);+ void invoke("acknowledge_scan_progress", { scanId: progress.scan_id }).catch(() => {+ // The completed/cancelled scan may already have released its channel.+ });+ };+ try {+ const raw = await invoke<string>("run_scan", { path, scanId, onProgress });+ const parsed: ScanResponse = JSON.parse(raw);+ if (parsed.status !== "ok") throw new Error("Scan returned non-ok status");+ return parsed;+ } finally {+ if (activeScan.current === scanId) activeScan.current = null;+ setCancelling(false);+ }+ }, []);++ const cancelScan = useCallback(async () => {+ const scanId = activeScan.current;+ if (!scanId) return;+ setCancelling(true);+ try { await invoke("cancel_scan", { scanId }); }+ catch (err) { onError(String(err)); setCancelling(false); }+ }, [onError]);++ return { scanProgress, cancelling, runScan, cancelScan };+}