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

Centralize scan catalog commands and response parsing (#136)

Vishnu Srivatsavadd7eaa526af4
diff --git a/src/App.tsx b/src/App.tsx
index 0fa9e69..f39fb71 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,8 +1,8 @@
import { useState, useCallback, useRef } from "react";
-import { invoke } from "@tauri-apps/api/core";
+import { deleteScanNode, getScanNode } from "./scanIpc";
import { checkAdb, confirmDaemonShutdown, startDaemon, stopDaemon } from "./daemonIpc";
import { formatBytes } from "./formatBytes";
-import { sameScan, parseNodeDetail, parseDeleteResult } from "./scanView";
+import { sameScan } from "./scanView";
import type { ScanResponse, NodeDetail } from "./scanView";
import { DeleteConfirmation } from "./DeleteConfirmation";
import { ToastContainer } from "./ToastContainer";
@@ -162,8 +162,7 @@ function App() {
if (!scope || scope.read_only || actionPending.current) return;
const request = ++confirmationRequest.current;
try {
- const raw = await invoke<string>("get_scan_node", { generation: scope.generation, revision: scope.revision, nodeId });
- const detail = parseNodeDetail(raw, scope, nodeId);
+ const detail = await getScanNode(scope, nodeId);
if (request === confirmationRequest.current && !actionPending.current && sameScan(snapshot.current, scope) && detail.node.can_delete) {
setConfirmDelete({ ...detail, trigger });
}
@@ -181,14 +180,8 @@ function App() {
setBusy(true);
addLog(`[DELETE] Requesting deletion of ${target.path}...`);
try {
- const raw = await invoke<string>("delete_item", {
- generation: target.generation, revision: target.revision, nodeId: target.node.id,
- });
+ const result = await deleteScanNode(target, target.node.id);
if (!sameScan(snapshot.current, target)) return;
- const result = parseDeleteResult(JSON.parse(raw));
- if (result.summary.generation !== target.generation || result.summary.revision !== target.revision + 1) {
- throw new Error("Deletion returned an unexpected scan snapshot");
- }
snapshot.current = result.summary;
setScanData(result.summary);
pushToast(`Successfully deleted ${target.node.name}`, "success");
diff --git a/src/FileTypes.tsx b/src/FileTypes.tsx
index 6bc24bd..86530e1 100644
--- a/src/FileTypes.tsx
+++ b/src/FileTypes.tsx
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";
-import { invoke } from "@tauri-apps/api/core";
-import { FILE_TYPES, FILE_TYPE_LABELS, parseFileTypes } from "./fileTypeSummary";
+import { getFileTypes } from "./scanIpc";
+import { FILE_TYPES, FILE_TYPE_LABELS } from "./fileTypeSummary";
import type { FileTypesResponse } from "./fileTypeSummary";
import type { ScanResponse } from "./scanView";
import { formatBytes } from "./formatBytes";
@@ -12,8 +12,7 @@ export function FileTypes({ scope, busy }: { scope: ScanResponse; busy: boolean
const [state, setState] = useState<{ key: string; data?: FileTypesResponse; error?: string }>({ key: "" });
useEffect(() => {
let current = true;
- void invoke<string>("get_file_types", { generation, revision }).then(raw => {
- const data = parseFileTypes(raw, { generation, revision, total_files, total_size });
+ void getFileTypes({ generation, revision, total_files, total_size }).then(data => {
if (current) setState({ key, data });
}).catch(error => {
if (current) setState({ key, error: String(error) });
diff --git a/src/LargestFiles.tsx b/src/LargestFiles.tsx
index 5e15181..2b66c23 100644
--- a/src/LargestFiles.tsx
+++ b/src/LargestFiles.tsx
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
-import { invoke } from "@tauri-apps/api/core";
-import { parseLargestFiles } from "./largestFilesSummary";
+import { getLargestFiles } from "./scanIpc";
import type { LargestResponse } from "./largestFilesSummary";
import type { ScanResponse } from "./scanView";
import { formatBytes } from "./formatBytes";
@@ -18,8 +17,7 @@ export function LargestFiles({ scope, busy, onZoom, onNuke }: {
const [state, setState] = useState<{ key: string; data?: LargestResponse; error?: string }>({ key: "" });
useEffect(() => {
let current = true;
- void invoke<string>("get_largest_files", { generation, revision, limit }).then(raw => {
- const data = parseLargestFiles(raw, { generation, revision, root_path, read_only, total_files, total_size }, limit);
+ void getLargestFiles({ generation, revision, root_path, read_only, total_files, total_size }, limit).then(data => {
if (current) setState({ key, data });
}).catch(error => {
if (current) setState({ key, error: String(error) });
diff --git a/src/ScanHistory.tsx b/src/ScanHistory.tsx
index 4e061f0..aaf8947 100644
--- a/src/ScanHistory.tsx
+++ b/src/ScanHistory.tsx
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
-import { invoke } from "@tauri-apps/api/core";
-import { parseScanComparison } from "./scanComparison";
+import { getScanComparison } from "./scanIpc";
import type { ScanComparison } from "./scanComparison";
import type { ScanResponse } from "./scanView";
import { formatBytes } from "./formatBytes";
@@ -22,8 +21,7 @@ export function ScanHistory({ scope }: { scope: ScanResponse }) {
const [state, setState] = useState<{ key: string; data?: ScanComparison; error?: string }>({ key: "" });
useEffect(() => {
let current = true;
- void invoke<string>("get_scan_comparison", { generation, revision }).then(raw => {
- const data = parseScanComparison(raw, { generation, revision, total_files, total_dirs, total_size });
+ void getScanComparison({ generation, revision, total_files, total_dirs, total_size }).then(data => {
if (current) setState({ key, data });
}).catch(() => {
// The comparison is context, not a scan result; a failed query must not
diff --git a/src/SearchFiles.tsx b/src/SearchFiles.tsx
index b796905..e55205c 100644
--- a/src/SearchFiles.tsx
+++ b/src/SearchFiles.tsx
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
-import { invoke } from "@tauri-apps/api/core";
-import { parseSearchResults } from "./searchResults";
+import { getSearchResults } from "./scanIpc";
import type { SearchResponse } from "./searchResults";
import type { ScanResponse } from "./scanView";
import { formatBytes } from "./formatBytes";
@@ -26,8 +25,7 @@ export function SearchFiles({ scope, busy, onZoom, onNuke }: {
useEffect(() => {
if (!query) return;
let current = true;
- void invoke<string>("get_search_results", { generation, revision, query, skip }).then(raw => {
- const data = parseSearchResults(raw, { generation, revision, root_path, read_only, total_size }, query, skip);
+ void getSearchResults({ generation, revision, root_path, read_only, total_size }, query, skip).then(data => {
if (current) setState({ key, data });
}).catch(error => {
if (current) setState({ key, error: String(error) });
diff --git a/src/scanIpc.ts b/src/scanIpc.ts
new file mode 100644
index 0000000..4b28cef
--- /dev/null
+++ b/src/scanIpc.ts
@@ -0,0 +1,46 @@
+import { invoke } from "@tauri-apps/api/core";
+import { parseDeleteResult, parseNodeDetail, parseScanView } from "./scanView";
+import type { PageCursor, ScanScope } from "./scanView";
+import { parseLargestFiles } from "./largestFilesSummary";
+import { parseSearchResults } from "./searchResults";
+import { parseFileTypes } from "./fileTypeSummary";
+import { parseScanComparison } from "./scanComparison";
+
+export async function getLargestFiles(scope: Parameters<typeof parseLargestFiles>[1], limit: number) {
+ const raw = await invoke<string>("get_largest_files", { generation: scope.generation, revision: scope.revision, limit });
+ return parseLargestFiles(raw, scope, limit);
+}
+
+export async function getSearchResults(scope: Parameters<typeof parseSearchResults>[1], query: string, skip: number) {
+ const raw = await invoke<string>("get_search_results", { generation: scope.generation, revision: scope.revision, query, skip });
+ return parseSearchResults(raw, scope, query, skip);
+}
+
+export async function getFileTypes(scope: Parameters<typeof parseFileTypes>[1]) {
+ const raw = await invoke<string>("get_file_types", { generation: scope.generation, revision: scope.revision });
+ return parseFileTypes(raw, scope);
+}
+
+export async function getScanComparison(scope: Parameters<typeof parseScanComparison>[1]) {
+ const raw = await invoke<string>("get_scan_comparison", { generation: scope.generation, revision: scope.revision });
+ return parseScanComparison(raw, scope);
+}
+
+export async function getScanView(scope: ScanScope, nodeId: number, cursor: PageCursor | null) {
+ const raw = await invoke<string>("get_scan_view", { generation: scope.generation, revision: scope.revision, nodeId, cursor });
+ return parseScanView(raw, scope, nodeId);
+}
+
+export async function getScanNode(scope: Parameters<typeof parseNodeDetail>[1], nodeId: number) {
+ const raw = await invoke<string>("get_scan_node", { generation: scope.generation, revision: scope.revision, nodeId });
+ return parseNodeDetail(raw, scope, nodeId);
+}
+
+export async function deleteScanNode(scope: ScanScope, nodeId: number) {
+ const raw = await invoke<string>("delete_item", { generation: scope.generation, revision: scope.revision, nodeId });
+ const result = parseDeleteResult(JSON.parse(raw));
+ if (result.summary.generation !== scope.generation || result.summary.revision !== scope.revision + 1) {
+ throw new Error("Deletion returned an unexpected scan snapshot");
+ }
+ return result;
+}
diff --git a/src/useScanView.ts b/src/useScanView.ts
index 5663c44..227d8cb 100644
--- a/src/useScanView.ts
+++ b/src/useScanView.ts
@@ -1,6 +1,6 @@
import { useEffect, useReducer, useRef, useState } from "react";
-import { invoke } from "@tauri-apps/api/core";
-import { parseScanView, viewKey, viewReducer } from "./scanView";
+import { getScanView } from "./scanIpc";
+import { viewKey, viewReducer } from "./scanView";
import type { PageCursor, ScanScope } from "./scanView";
export function useScanView(scope: ScanScope, nodeId: number, enabled: boolean) {
@@ -21,10 +21,7 @@ export function useScanView(scope: ScanScope, nodeId: number, enabled: boolean)
dispatch({ type: "start", request: id, key });
const queryCursor = cursorDirection !== undefined && cursorNodeId !== undefined
? { direction: cursorDirection, node_id: cursorNodeId } : null;
- void invoke<string>("get_scan_view", {
- generation, revision, nodeId, cursor: queryCursor,
- }).then(raw => {
- const page = parseScanView(raw, { generation, revision }, nodeId);
+ void getScanView({ generation, revision }, nodeId, queryCursor).then(page => {
if (current) dispatch({ type: "success", request: id, page });
}).catch(error => {
if (current) dispatch({ type: "error", request: id, error: String(error) });