Skip to content

Conversation

@Boy132
Copy link
Member

@Boy132 Boy132 commented Sep 24, 2025

Closes #1743

Copied from filaments cancel action.

@Boy132 Boy132 self-assigned this Sep 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 24, 2025

📝 Walkthrough

Walkthrough

Introduces previousUrl storage in EditFiles, updates redirects and cancel navigation to respect SPA mode via FilamentView::hasSpaMode, and uses previousUrl when available, otherwise falls back to the directory listing URL. Error paths now compute ListFiles URL and redirect with SPA awareness. Mount stores url()->previous().

Changes

Cohort / File(s) Summary
File edit navigation and SPA-aware redirects
app/Filament/Server/Resources/Files/Pages/EditFiles.php
- Add public property previousUrl initialized to null
- Import Filament\View\FilamentView and Livewire\Attributes\Js (as used)
- Store url()->previous() in mount()
- Replace direct redirects to ListFiles::getUrl([...]) with $this->redirect($url, FilamentView::hasSpaMode($url)) across success/error paths
- Update cancel action to use Alpine click handler preferring previousUrl, otherwise dirname path, selecting SPA vs full navigation

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant EditFiles Page
  participant FilamentView
  participant Router/Spa
  participant ListFiles Page

  rect rgb(245,248,255)
    note over EditFiles Page: mount()
    User->>EditFiles Page: Open file for editing
    EditFiles Page->>EditFiles Page: previousUrl = url()->previous()
  end

  alt Cancel clicked
    User->>EditFiles Page: Cancel (Alpine click)
    EditFiles Page->>EditFiles Page: target = previousUrl ?? ListFiles::getUrl(dirname(path))
    EditFiles Page->>FilamentView: hasSpaMode(target)?
    alt SPA mode
      EditFiles Page->>Router/Spa: navigate(target)
      Router/Spa->>ListFiles Page: Render
    else Full page
      EditFiles Page->>Router/Spa: redirect(target)
      Router/Spa->>ListFiles Page: Reload
    end
  end

  alt Error or Non-editable
    EditFiles Page->>EditFiles Page: url = ListFiles::getUrl(dirname(path))
    EditFiles Page->>FilamentView: hasSpaMode(url)?
    EditFiles Page->>Router/Spa: redirect(url) with SPA awareness
    Router/Spa->>ListFiles Page: Navigate
  end

  alt Save completes
    EditFiles Page->>EditFiles Page: url = ListFiles::getUrl(dirname(path))
    EditFiles Page->>FilamentView: hasSpaMode(url)?
    EditFiles Page->>Router/Spa: redirect(url) with SPA awareness
    Router/Spa->>ListFiles Page: Navigate
  end
Loading

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly states the main change: redirecting to the previous page when clicking "cancel" on the EditFiles page. It maps directly to the code changes that add a previousUrl property and update the cancel navigation to use the previous URL. The phrasing is concise and specific enough for a teammate scanning PRs to understand the primary change.
Linked Issues Check ✅ Passed The PR stores url()->previous() into a previousUrl property and uses it for the cancel action so users are returned to the exact prior page (including query parameters), which addresses the linked issue of preserving the file search query when returning from editing [#1743]. It also applies SPA-aware navigation via FilamentView::hasSpaMode to keep behavior consistent in single-page flows. One caveat is that several error-handling paths were updated to redirect to ListFiles::getUrl(['path' => dirname($this->path)]), which may not preserve search query parameters in those error flows, but the primary cancel-return path meets the issue objective.
Out of Scope Changes Check ✅ Passed All modifications are focused on navigation behavior in EditFiles: adding previousUrl, using url()->previous(), SPA-aware redirects, and updating cancel and error redirects; these changes are directly related to the linked issue's navigation objective. I do not see unrelated features or files modified outside navigation behavior. Therefore the changes are in-scope.
Description Check ✅ Passed The description references the linked issue (#1743) and notes the implementation source ("Copied from filaments cancel action"), so it is directly related to the changeset. Although brief, it correctly signals the intent to address the issue. Therefore it satisfies the lenient description check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
app/Filament/Server/Resources/Files/Pages/EditFiles.php (4)

59-60: Lock the new public property to prevent client-side tampering.

Expose it as read-only to Livewire by adding #[Locked].

-    public ?string $previousUrl = null;
+    #[Locked]
+    public ?string $previousUrl = null;

125-131: Harden cancel navigation and avoid SPA navigate to arbitrary previousUrl.

  • Use SPA vs full reload based on the panel’s SPA mode (no argument).
  • Prefer history.back() for preserving the search state.
  • For the no-referrer fallback, navigate to the listing URL (safe, internal) rather than previousUrl, which could be cross‑origin.
-                            ->alpineClickHandler(function () {
-                                $url = $this->previousUrl ?? ListFiles::getUrl(['path' => dirname($this->path)]);
-
-                                return FilamentView::hasSpaMode($url)
-                                    ? 'document.referrer ? window.history.back() : Livewire.navigate(' . Js::from($url) . ')'
-                                    : 'document.referrer ? window.history.back() : (window.location.href = ' . Js::from($url) . ')';
-                            }),
+                            ->alpineClickHandler(function () {
+                                $url = ListFiles::getUrl(['path' => dirname($this->path)]);
+
+                                return FilamentView::hasSpaMode()
+                                    ? 'document.referrer ? window.history.back() : Livewire.navigate(' . Js::from($url) . ')'
+                                    : 'document.referrer ? window.history.back() : (window.location.href = ' . Js::from($url) . ')';
+                            }),

182-184: Align error redirects with parameterless hasSpaMode().

Match the suggested change above for consistency and to avoid potential signature mismatches.

-                                    $this->redirect($url, FilamentView::hasSpaMode($url));
+                                    $this->redirect($url, FilamentView::hasSpaMode());

Same diff applies to each occurrence in these catch blocks.

Also applies to: 191-193, 200-202, 205-207


222-223: Sanitize previousUrl to same-origin before storing (optional).

Prevents navigating to external origins when no referrer is present, while still enabling history.back() to preserve the search state.

-        $this->previousUrl = url()->previous();
+        $prev = url()->previous();
+        if (parse_url($prev, PHP_URL_HOST) !== request()->getHost()) {
+            $prev = null;
+        }
+        $this->previousUrl = $prev;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a5858a6 and 72e47b7.

📒 Files selected for processing (1)
  • app/Filament/Server/Resources/Files/Pages/EditFiles.php (6 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
app/Filament/Server/Resources/Files/Pages/EditFiles.php (2)
app/Filament/Server/Resources/Files/Pages/ListFiles.php (1)
  • ListFiles (49-601)
app/Livewire/AlertBanner.php (2)
  • AlertBanner (14-100)
  • closable (72-77)
🔇 Additional comments (3)
app/Filament/Server/Resources/Files/Pages/EditFiles.php (3)

31-31: Import looks correct.

FilamentView facade is appropriate for SPA checks.


37-37: Good choice using Js::from for safe JS embedding.


100-102: Don't pass $url into FilamentView::hasSpaMode — verify the method signature

Confirm the signature in the installed Filament package (vendor/) or upstream; if it's parameterless, replace FilamentView::hasSpaMode($url) with FilamentView::hasSpaMode() to avoid an ArgumentCountError.

Occurrences to update: app/Filament/Server/Resources/Files/Pages/EditFiles.php — lines 101, 128, 183, 192, 201, 206.

Example replacement:

  • $this->redirect($url, FilamentView::hasSpaMode());
  • return FilamentView::hasSpaMode() ? 'document.referrer ? window.history.back() : Livewire.navigate(' . Js::from($url) . ')' : 'document.referrer ? window.history.back() : (window.location.href = ' . Js::from($url) . ')';

@Boy132 Boy132 merged commit 81178f8 into main Sep 28, 2025
25 checks passed
@Boy132 Boy132 deleted the boy132/edit-file-previous-url branch September 28, 2025 17:12
@github-actions github-actions bot locked and limited conversation to collaborators Sep 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve file search query after editing a file and returning

3 participants