-
Couldn't load subscription status.
- Fork 4
Feat / Adding tenant menu switcher #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SlimDeluxe
merged 8 commits into
DataLinx:main
from
thapacodes4u:feat/tenant-switcher-menu
Oct 21, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
549f53e
feat: adding brand tenant switcher menu
19392df
fix: reverting some of the changes
db7ba6f
fix: failing tests
4e48598
fix: refactor tenant switcher component and fix frontend panel detection
39ddca9
fix: fixing the issue of frontend link not showing
00a0a43
fix: resolving conflict
thapacodes4u fecc1a3
refactor: use Filament ViewComponent pattern for BrandWithTenantSwitcher
thapacodes4u 5f3c0c1
fix: resolving conflict
thapacodes4u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
resources/views/filament/components/brand-with-tenant-switcher.blade.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| @if ($shouldShowDropdown) | ||
| <div class="flex items-center gap-x-2"> | ||
| <a @if ($hasSpaMode) wire:navigate @endif href="{{ $getDashboardUrl }}" class="flex-1"> | ||
| <div class="fi-logo flex text-xl font-bold leading-5 tracking-tight text-gray-950 dark:text-white"> | ||
| {{ $getAppName }} | ||
| @if ($getCurrentTenant && $getCurrentTenantName !== $getAppName) | ||
| <span class="text-gray-500 dark:text-gray-400 text-sm font-normal ml-2"> | ||
| - {{ $getCurrentTenantName }} | ||
| </span> | ||
| @endif | ||
| </div> | ||
| </a> | ||
|
|
||
| <x-filament::dropdown teleport> | ||
| <x-slot name="trigger"> | ||
| <button type="button" | ||
| class="fi-tenant-menu-trigger group flex items-center justify-center rounded-lg p-1.5 text-sm font-medium outline-none transition duration-75 hover:bg-gray-100 focus-visible:bg-gray-100 dark:hover:bg-white/5 dark:focus-visible:bg-white/5"> | ||
| <x-filament::icon icon="heroicon-m-chevron-down" alias="panels::brand-tenant-menu.toggle-button" | ||
| class="h-4 w-4 text-gray-400 transition duration-75 group-hover:text-gray-500 group-focus-visible:text-gray-500 dark:text-gray-500 dark:group-hover:text-gray-400 dark:group-focus-visible:text-gray-400" /> | ||
| </button> | ||
| </x-slot> | ||
|
|
||
| <x-filament::dropdown.list> | ||
| @if ($canSwitchTenants) | ||
| @foreach ($getTenants as $tenant) | ||
| <x-filament::dropdown.list.item :href="route('filament.admin.pages.dashboard', ['tenant' => $tenant])" :image="filament()->getTenantAvatarUrl($tenant)" tag="a"> | ||
| {{ filament()->getTenantName($tenant) }} | ||
| </x-filament::dropdown.list.item> | ||
| @endforeach | ||
| @endif | ||
|
|
||
|
|
||
| @if ($hasFrontend) | ||
| @if ($canSwitchTenants) | ||
| <x-filament::dropdown.list.item tag="div" | ||
| class="border-t border-gray-200 dark:border-gray-700 my-1"></x-filament::dropdown.list.item> | ||
| @endif | ||
|
|
||
| <x-filament::dropdown.list.item :href="$getFrontendUrl" tag="a" target="_blank" | ||
| class="font-medium"> | ||
| <div class="flex items-center gap-2"> | ||
| <x-filament::icon icon="heroicon-s-globe-alt" | ||
| class="h-4 w-4 text-gray-500 dark:text-gray-400" /> | ||
| Frontend | ||
| <x-filament::icon icon="heroicon-s-arrow-top-right-on-square" | ||
| class="h-3 w-3 text-gray-400 dark:text-gray-500 ml-auto" /> | ||
| </div> | ||
| </x-filament::dropdown.list.item> | ||
| @endif | ||
| </x-filament::dropdown.list> | ||
| </x-filament::dropdown> | ||
| </div> | ||
| @else | ||
| <div class="fi-logo flex text-xl font-bold leading-5 tracking-tight text-gray-950 dark:text-white"> | ||
| {{ $getAppName }} | ||
| @if ($getCurrentTenant && $getCurrentTenantName !== $getAppName) | ||
| <span class="text-gray-500 dark:text-gray-400 text-sm font-normal ml-2"> | ||
| - {{ $getCurrentTenantName }} | ||
| </span> | ||
| @endif | ||
| </div> | ||
| @endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
|
|
||
| namespace Eclipse\Core\View\Components; | ||
|
|
||
| use Eclipse\Core\Services\Registry; | ||
| use Exception; | ||
| use Filament\Facades\Filament; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\View\Component; | ||
|
|
||
| class BrandWithTenantSwitcher extends Component | ||
| { | ||
| public function render() | ||
| { | ||
| return view('eclipse::filament.components.brand-with-tenant-switcher', [ | ||
| 'shouldShowDropdown' => $this->shouldShowDropdown(), | ||
| 'getAppName' => $this->getAppName(), | ||
| 'hasSpaMode' => $this->hasSpaMode(), | ||
| 'getDashboardUrl' => $this->getDashboardUrl(), | ||
| 'getCurrentTenant' => $this->getCurrentTenant(), | ||
| 'getCurrentTenantName' => $this->getCurrentTenantName(), | ||
| 'canSwitchTenants' => $this->canSwitchTenants(), | ||
| 'getTenants' => $this->getTenants(), | ||
| 'hasFrontend' => $this->hasFrontend(), | ||
| 'getFrontendUrl' => $this->getFrontendUrl(), | ||
| ]); | ||
| } | ||
|
|
||
| public function getAppName(): string | ||
| { | ||
| return Registry::getSite()->name ?? config('app.name'); | ||
| } | ||
|
|
||
| public function hasSpaMode(): bool | ||
| { | ||
| return Filament::getCurrentPanel()->hasSpaMode(); | ||
| } | ||
|
|
||
| public function getDashboardUrl(): string | ||
| { | ||
| return '/'.trim(Filament::getCurrentPanel()->getPath(), '/'); | ||
| } | ||
|
|
||
| public function getCurrentTenant(): ?Model | ||
| { | ||
| return filament()->getTenant(); | ||
| } | ||
|
|
||
| public function getCurrentTenantName(): ?string | ||
| { | ||
| $currentTenant = $this->getCurrentTenant(); | ||
|
|
||
| return $currentTenant ? filament()->getTenantName($currentTenant) : null; | ||
| } | ||
|
|
||
| public function getTenants(): array | ||
| { | ||
| if (! $this->isMultiSiteEnabled() || ! filament()->auth()->check()) { | ||
| return []; | ||
| } | ||
|
|
||
| $currentTenant = $this->getCurrentTenant(); | ||
|
|
||
| return array_filter( | ||
| filament()->getUserTenants(filament()->auth()->user()), | ||
| fn (Model $tenant): bool => ! $tenant->is($currentTenant), | ||
| ); | ||
| } | ||
|
|
||
| public function canSwitchTenants(): bool | ||
| { | ||
| return count($this->getTenants()) > 0; | ||
| } | ||
|
|
||
| public function hasFrontend(): bool | ||
| { | ||
| return collect(Filament::getPanels())->has('frontend'); | ||
| } | ||
|
|
||
| public function getFrontendUrl(): string | ||
| { | ||
| if (! $this->hasFrontend()) { | ||
| return config('app.url'); | ||
| } | ||
|
|
||
| try { | ||
| $currentTenant = $this->getCurrentTenant(); | ||
| if ($currentTenant?->domain) { | ||
| return "https://{$currentTenant->domain}"; | ||
| } else { | ||
| return config('app.url'); | ||
| } | ||
| } catch (Exception $e) { | ||
| return config('app.url'); | ||
| } | ||
| } | ||
|
|
||
| public function shouldShowDropdown(): bool | ||
| { | ||
| return $this->canSwitchTenants() || $this->hasFrontend(); | ||
| } | ||
|
|
||
| private function isMultiSiteEnabled(): bool | ||
| { | ||
| return config('eclipse.multi_site', false); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| use Eclipse\Core\Models\Site; | ||
| use Eclipse\Core\Models\User; | ||
| use Eclipse\Core\View\Components\BrandWithTenantSwitcher; | ||
|
|
||
| test('brand component renders without tenants', function () { | ||
| $response = $this->get('/admin/login'); | ||
|
|
||
| $response->assertStatus(200); | ||
| $response->assertSee(config('app.name')); | ||
| }); | ||
|
|
||
| test('brand component renders with multi-site enabled', function () { | ||
| config(['eclipse.multi_site' => true]); | ||
|
|
||
| $response = $this->get('/admin/login'); | ||
| $response->assertStatus(200); | ||
| $response->assertSee(config('app.name')); | ||
| }); | ||
|
|
||
| test('brand component dropdown behavior depends on frontend and tenant availability', function () { | ||
| $component = new BrandWithTenantSwitcher; | ||
|
|
||
| $hasFrontend = $component->hasFrontend(); | ||
| $canSwitchTenants = $component->canSwitchTenants(); | ||
| $shouldShowDropdown = $component->shouldShowDropdown(); | ||
|
|
||
| expect($shouldShowDropdown)->toBe($hasFrontend || $canSwitchTenants); | ||
|
|
||
| expect($hasFrontend)->toBeBool(); | ||
| expect($canSwitchTenants)->toBeBool(); | ||
| expect($shouldShowDropdown)->toBeBool(); | ||
| }); | ||
|
|
||
| test('tenant switcher logic handles multiple and single tenant scenarios', function () { | ||
| config(['eclipse.multi_site' => true]); | ||
|
|
||
| $user = User::factory()->create(); | ||
| $site1 = Site::factory()->create(['name' => 'Primary Site']); | ||
| $site2 = Site::factory()->create(['name' => 'Secondary Site']); | ||
|
|
||
| $user->sites()->attach([$site1->id, $site2->id]); | ||
|
|
||
| expect($user->sites)->toHaveCount(2); | ||
| expect($user->sites->pluck('name'))->toContain('Primary Site', 'Secondary Site'); | ||
|
|
||
| $singleSiteUser = User::factory()->create(); | ||
| $singleSite = Site::factory()->create(['name' => 'Only Site']); | ||
| $singleSiteUser->sites()->attach($singleSite->id); | ||
|
|
||
| expect($singleSiteUser->sites)->toHaveCount(1); | ||
| expect($singleSiteUser->sites->first()->name)->toBe('Only Site'); | ||
| }); | ||
|
|
||
| test('brand component renders app name correctly', function () { | ||
| $component = new BrandWithTenantSwitcher; | ||
|
|
||
| $appName = $component->getAppName(); | ||
|
|
||
| expect($appName)->toBe(config('app.name')); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.