@php use App\Filament\Pages\ChooseWorkspace; use App\Models\Tenant; use App\Models\User; use App\Models\WorkspaceMembership; use App\Services\Auth\WorkspaceRoleCapabilityMap; use App\Support\Auth\Capabilities; use App\Support\OperateHub\OperateHubShell; use App\Support\Workspaces\WorkspaceContext; use Filament\Facades\Filament; /** @var WorkspaceContext $workspaceContext */ $workspaceContext = app(WorkspaceContext::class); $workspace = $workspaceContext->currentWorkspace(request()); $user = auth()->user(); $canSeeAllWorkspaceTenants = false; if ($user instanceof User && $workspace) { $roles = WorkspaceRoleCapabilityMap::rolesWithCapability(Capabilities::WORKSPACE_MEMBERSHIP_MANAGE); $canSeeAllWorkspaceTenants = WorkspaceMembership::query() ->where('workspace_id', (int) $workspace->getKey()) ->where('user_id', (int) $user->getKey()) ->whereIn('role', $roles) ->exists(); } $tenants = collect(); if ($user instanceof User && $workspace) { if ($canSeeAllWorkspaceTenants) { $tenants = Tenant::query() ->where('workspace_id', (int) $workspace->getKey()) ->orderBy('name') ->get(); } else { $tenants = collect($user->getTenants(Filament::getCurrentOrDefaultPanel())) ->filter(fn ($tenant): bool => $tenant instanceof Tenant && (int) $tenant->workspace_id === (int) $workspace->getKey()) ->values(); } } $operateHubShell = app(OperateHubShell::class); $currentTenant = $operateHubShell->activeEntitledTenant(request()); $currentTenantName = $currentTenant instanceof Tenant ? $currentTenant->getFilamentName() : null; $hasAnyFilamentTenantContext = Filament::getTenant() instanceof Tenant; $route = request()->route(); $routeName = (string) ($route?->getName() ?? ''); $tenantQuery = request()->query('tenant'); $hasTenantQuery = is_string($tenantQuery) && trim($tenantQuery) !== ''; if ($currentTenantName === null && $hasTenantQuery) { $queriedTenant = $tenants->first(function ($tenant) use ($tenantQuery): bool { return $tenant instanceof Tenant && ((string) $tenant->external_id === (string) $tenantQuery || (string) $tenant->getKey() === (string) $tenantQuery); }); if ($queriedTenant instanceof Tenant) { $currentTenantName = $queriedTenant->getFilamentName(); } } $isTenantScopedRoute = $route?->hasParameter('tenant') || ($hasTenantQuery && str_starts_with($routeName, 'filament.admin.')); $lastTenantId = $workspaceContext->lastTenantId(request()); $canClearTenantContext = $hasAnyFilamentTenantContext || $lastTenantId !== null; @endphp @php $tenantLabel = $currentTenantName ?? 'All tenants'; $workspaceLabel = $workspace?->name ?? 'Select workspace'; $hasActiveTenant = $currentTenantName !== null; $workspaceUrl = $workspace ? route('admin.home') : ChooseWorkspace::getUrl(panel: 'admin'); $tenantTriggerLabel = $workspace ? $tenantLabel : 'Select tenant'; @endphp
{{-- Workspace label: standalone link --}} {{ $workspaceLabel }} @if ($workspace) @endif {{-- Dropdown trigger: tenant label + chevron --}}
{{-- Workspace section --}}
Workspace
{{ $workspaceLabel }}
Switch workspace
Workspace Home
@if ($workspace)
{{-- Tenant section --}}
Tenant scope
@if ($canSeeAllWorkspaceTenants) all visible @endif
@if ($isTenantScopedRoute)
{{ $currentTenantName ?? 'Tenant' }} locked
@else @if ($tenants->isEmpty())
{{ $canSeeAllWorkspaceTenants ? 'No tenants in this workspace.' : 'No accessible tenants.' }}
@else
@foreach ($tenants as $tenant) @php $isActive = $currentTenantName !== null && $tenant->getFilamentName() === $currentTenantName; @endphp
@csrf
@endforeach
@if ($canClearTenantContext)
@csrf
@endif @endif @endif
@else
Choose a workspace first.
@endif