refactor: simplify NavbarManager by removing admin context checks and redundant methods, enhancing clarity and maintainability while ensuring consistent navigation link registration

This commit is contained in:
2025-12-03 16:10:36 -05:00
parent eaff941318
commit 9fe3a8ef9b

View File

@@ -50,79 +50,26 @@ class NavbarManager extends Extension
public function boot()
{
// Don't run any navigation modifications in admin or console
if ($this->isAdminContext()) {
// Only register Filament resource in admin
$this->registerFilamentResource();
return;
}
// Register view service provider (frontend only)
// Register view service provider
app()->register(\Paymenter\Extensions\Others\NavbarManager\Providers\ViewServiceProvider::class);
// Register custom routes for navbar links
require __DIR__ . '/routes/web.php';
// Register event listeners for navigation - only for frontend
// Register event listeners for navigation - dodaće naše linkove uz postojeće
Event::listen('navigation', function () {
if (!$this->isAdminContext()) {
return $this->getNavigationLinks('primary');
}
return [];
return $this->getNavigationLinks('primary');
});
Event::listen('navigation.account-dropdown', function () {
if (!$this->isAdminContext()) {
return $this->getNavigationLinks('account');
}
return [];
return $this->getNavigationLinks('account');
});
Event::listen('navigation.dashboard', function () {
if (!$this->isAdminContext()) {
return $this->getNavigationLinks('dashboard');
}
return [];
return $this->getNavigationLinks('dashboard');
});
}
/**
* Check if we're in admin context
*/
private function isAdminContext(): bool
{
if (app()->runningInConsole()) {
return false; // Allow in console for migrations, etc.
}
try {
$currentPath = request()->path();
// Check for admin routes
if (str_starts_with($currentPath, 'admin') ||
str_contains($currentPath, '/admin/') ||
request()->is('admin') ||
request()->is('admin/*')) {
return true;
}
// Check for Filament routes
if (str_starts_with($currentPath, 'filament') ||
request()->is('filament/*')) {
return true;
}
return false;
} catch (\Exception $e) {
return false;
}
}
/**
* Register Filament resource
*/
private function registerFilamentResource(): void
{
// Register the Filament resource
try {
\Filament\Facades\Filament::serving(function () {
\Filament\Facades\Filament::getCurrentPanel()
@@ -131,9 +78,7 @@ class NavbarManager extends Extension
]);
});
} catch (\Exception $e) {
\Log::error('NavbarManager: Failed to register Filament resource', [
'error' => $e->getMessage()
]);
// Resource not yet available
}
}
@@ -303,61 +248,6 @@ class NavbarManager extends Extension
}
}
private function isValidIcon($icon)
{
// Basic validation for RemixIcon format
if (!preg_match('/^ri-[\w-]+$/', $icon)) {
return false;
}
// List of common valid RemixIcon names to avoid errors (without -line/-fill suffix)
$commonIcons = [
'ri-home', 'ri-user', 'ri-settings', 'ri-dashboard',
'ri-menu', 'ri-link', 'ri-external-link', 'ri-folder',
'ri-file', 'ri-edit', 'ri-delete-bin', 'ri-add',
'ri-navigation', 'ri-admin', 'ri-function', 'ri-archive-stack',
'ri-receipt', 'ri-customer-service', 'ri-settings-3', 'ri-megaphone',
'ri-shopping-bag', 'ri-check', 'ri-close', 'ri-arrow-down-s'
];
// Remove -line or -fill suffix if present to get base icon name
$baseIcon = preg_replace('/-(?:line|fill)$/', '', $icon);
// If it's in our common list, it's definitely valid
if (in_array($baseIcon, $commonIcons)) {
return true;
}
// For other icons, just check the format (basic validation)
// In a production environment, you might want to check if the SVG file actually exists
return true;
}
}
$children = [];
foreach ($link->children->where('is_active', true) as $child) {
if ($this->shouldShowLink($child)) {
$childFormatted = $this->formatLink($child);
if ($childFormatted) {
$children[] = $childFormatted;
}
}
}
if (!empty($children)) {
$formatted['children'] = $children;
}
}
return $formatted;
} catch (\Exception $e) {
\Log::error('NavbarManager: Error formatting link', [
'link_id' => $link->id ?? 'unknown',
'error' => $e->getMessage()
]);
return null;
}
}
private function isValidIcon($icon)
{
// Basic validation for RemixIcon format