From 28602f663539fa16fa50fdc6be917f3219de855a Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Thu, 5 Feb 2026 15:33:37 +0100 Subject: [PATCH] refactor: enhance tab navigation in OrganizationSettings by syncing URL with state and improving loading indicators --- components/settings/OrganizationSettings.tsx | 40 +++++++++++--------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/components/settings/OrganizationSettings.tsx b/components/settings/OrganizationSettings.tsx index aada2fe..e441e68 100644 --- a/components/settings/OrganizationSettings.tsx +++ b/components/settings/OrganizationSettings.tsx @@ -41,11 +41,20 @@ export default function OrganizationSettings() { const { user } = useAuth() const router = useRouter() const searchParams = useSearchParams() + // Initialize from URL, default to 'general' const [activeTab, setActiveTab] = useState<'general' | 'members' | 'billing' | 'audit'>(() => { const tab = searchParams.get('tab') - return tab === 'billing' || tab === 'members' || tab === 'audit' ? tab : 'general' + return (tab === 'billing' || tab === 'members' || tab === 'audit') ? tab : 'general' }) + // Sync URL with state without triggering navigation/reload + const handleTabChange = (tab: 'general' | 'members' | 'billing' | 'audit') => { + setActiveTab(tab) + const url = new URL(window.location.href) + url.searchParams.set('tab', tab) + window.history.replaceState({}, '', url) + } + const [showDeletePrompt, setShowDeletePrompt] = useState(false) const [deleteConfirm, setDeleteConfirm] = useState('') const [isDeleting, setIsDeleting] = useState(false) @@ -169,6 +178,9 @@ export default function OrganizationSettings() { } }, [currentOrgId, loadMembers]) + // Removed useEffect that syncs searchParams to activeTab to prevent flickering + // The initial state is already set from searchParams, and handleTabChange updates the URL manually + /* useEffect(() => { const tab = searchParams.get('tab') const validTab = (tab === 'billing' || tab === 'members' || tab === 'audit') ? tab : 'general' @@ -176,6 +188,7 @@ export default function OrganizationSettings() { setActiveTab(validTab) } }, [searchParams, activeTab]) + */ useEffect(() => { if (activeTab === 'billing' && currentOrgId) { @@ -335,10 +348,7 @@ export default function OrganizationSettings() { // We can find the current user's membership entry which has org name. const currentOrgName = members.find(m => m.user_id === user?.id)?.organization_name || 'Organization' - const handleTabChange = (tab: 'general' | 'members' | 'billing' | 'audit') => { - // setActiveTab(tab) // Let the useEffect handle the state update based on URL - router.push(`?tab=${tab}`) - } + // handleTabChange is defined above return (
@@ -561,9 +571,8 @@ export default function OrganizationSettings() {

Active Members

{isLoadingMembers ? ( -
-
- Loading members... +
+
) : members.length === 0 ? (
No members found.
@@ -643,9 +652,8 @@ export default function OrganizationSettings() {
{isLoadingSubscription ? ( -
-
- Loading subscription details... +
+
) : !subscription ? (
@@ -755,9 +763,8 @@ export default function OrganizationSettings() {

Invoice History

{isLoadingInvoices ? ( -
-
- Loading invoices... +
+
) : invoices.length === 0 ? (
No invoices found.
@@ -892,9 +899,8 @@ export default function OrganizationSettings() { {/* Table */}
{isLoadingAudit ? ( -
-
- Loading audit log... +
+
) : (auditEntries ?? []).length === 0 ? (
No audit events found.