Skip to content

refactor(file-browser): extract reusable deletion logic and abstract Termux URI validation - #2548

Open
AuDevTist1C wants to merge 1 commit into
Acode-Foundation:mainfrom
AuDevTist1C:cleanup/filebrowser-delete-logic
Open

refactor(file-browser): extract reusable deletion logic and abstract Termux URI validation#2548
AuDevTist1C wants to merge 1 commit into
Acode-Foundation:mainfrom
AuDevTist1C:cleanup/filebrowser-delete-logic

Conversation

@AuDevTist1C

Copy link
Copy Markdown
Contributor

This pull request refactors the file and folder deletion routines within src/pages/fileBrowser/fileBrowser.js. Previously, identical recursive deletion logic and post-deletion state cleanup were duplicated across both batch deletion (selection mode) and individual item removal (removeFile).

By consolidating these workflows into targeted helper functions (deleteDirOrFile and isTermuxUrl), this PR reduces code redundancy by ~26 lines, improves readability, and ensures consistent state maintenance across all deletion paths.


🛠️ Key Changes

1. Unified Deletion Logic (deleteDirOrFile)

  • Extracted Centralized Helper: Created an async function deleteDirOrFile(url, type) helper that handles both single files and directories.
  • Consolidated State Cleanup: Centralized side-effect cleanup routines directly into deleteDirOrFile:
    • Updating URIs of active editor tabs (helpers.updateUriOfAllActiveFiles).
    • Removing references from recents (recents.removeFolder / recents.removeFile).
    • Clearing items from open folder states (openFolder.removeItem).
    • Invalidating directory cache (delete cachedDir[url]).
  • Simplified Workflows: Replaced duplicated multi-line blocks in both batch deletion (for (const url of selectedItems) await deleteDirOrFile(url);) and single-file deletion (removeFile).

2. Termux URI Validation Abstraction (isTermuxUrl)

  • Extracted Helper: Created isTermuxUrl(url) to encapsulate string checking for the content://com.termux.documents/tree/ provider.
  • Improved Safety: Standardized string coercion (${url ?? ""}) to prevent runtime null/undefined evaluation errors.
  • Refactored Consumers: Replaced direct url.startsWith(...) calls in rename handlers and recursive deletion steps with isTermuxUrl().

3. Streamlined Recursive Termux Directory Traversal

  • Refactored deleteRecursively to pass fsOperation instances directly down the call chain instead of repeatedly constructing new instances from raw string URIs at each recursive step.

🔬 Implementation Details

Function Type Purpose
isTermuxUrl(url) Helper Safely checks whether a URI originates from Termux Document Provider.
deleteDirOrFile(url, type) Helper Asynchronously resolves stat/type checks, recursively deletes Termux trees or standard files, and performs state cleanup.

(PR name and description are AI generated (Gemini 3.6 Flash))

… Termux URL checking

Consolidate redundant file and directory deletion routines into a single reusable helper function to improve maintainability and reduce code duplication in fileBrowser.

* **Deletion Logic Consolidation:**
* Refactored duplicated recursive deletion routines across batch selection and individual item removal into a unified `deleteDirOrFile()` helper function.
* Standardized post-deletion state cleanup including cache invalidation, recents removal, and active editor URI updates.
* **Termux Traversal & Validation:**
* Extracted Termux URI checking into a dedicated `isTermuxUrl()` helper function for cleaner reuse in rename and deletion handlers.
* Streamlined recursive directory tree traversal for Termux storage backends by passing `fsOperation` instances directly.

(AI generated commit message)
@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Refactors file-browser deletion behavior without changing its external contract.

  • Extracts shared file and directory deletion into deleteDirOrFile.
  • Preserves editor, recents, open-folder, and directory-cache cleanup across single and batch deletion.
  • Extracts null-safe Termux URI detection into isTermuxUrl.
  • Passes filesystem operation objects through recursive Termux directory deletion.

Confidence Score: 5/5

The PR appears safe to merge, with the refactor preserving the existing deletion paths and cleanup behavior.

Termux recursion still operates bottom-up on filesystem objects bound to each child URL, while both single-item and batch deletion retain their previous type resolution, filesystem deletion, editor cleanup, recents cleanup, open-folder cleanup, and cache invalidation.

Important Files Changed

Filename Overview
src/pages/fileBrowser/fileBrowser.js Extracts shared deletion and Termux URI helpers while preserving the existing recursive deletion and post-delete cleanup behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Delete request] --> B{Type supplied?}
    B -->|Yes| C[Classify with helpers.isDir]
    B -->|No| D[Read fs.stat]
    C --> E{Termux directory?}
    D --> E
    E -->|Yes| F[Recursively delete children]
    F --> G[Delete directory]
    E -->|No| H[Delete filesystem item]
    G --> I[Clean editor, recents, folder, and cache state]
    H --> I
Loading

Reviews (1): Last reviewed commit: "refactor(file-browser): extract reusable..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant