feat(dashboard): persist active tab in URL and keep active tab on reload
Deploy Canina / deploy (push) Successful in 1m30s

This commit is contained in:
parsa aghaei committed 2026-08-22 14:04:30 +03:30
1 parent a2b8afccc3
commit bf41ba038e
13 files changed
+17304 -9367

No files matched your search

+7 -1
View File
@@ -1,3 +1,4 @@
import { Suspense } from 'react';
import type { Metadata } from 'next';
import UserDashboard from "../../components/UserDashboard";
import { getPageMetadata } from "../../lib/seo";
@@ -12,6 +13,11 @@ export async function generateMetadata(): Promise<Metadata> {
}
export default function DashboardPage() {
return <UserDashboard />;
return (
<Suspense fallback={<div className="min-h-screen bg-medical-gray-50 flex items-center justify-center font-vazir text-xs text-medical-gray-400">در حال بارگذاری داشبورد...</div>}>
<UserDashboard />
</Suspense>
);
}
@@ -36,15 +36,30 @@ export default function UserDashboard() {
}, [isLoggedIn, router]);
const { orders } = useCartStore();
const [activeTab, setActiveTab] = React.useState<"profile" | "orders" | "wallet" | "addresses" | "tickets" | "pets" | "prescriptions" | "overview">("profile");
const validTabs = React.useMemo(() => ['profile', 'orders', 'wallet', 'addresses', 'tickets', 'pets', 'prescriptions'], []);
// Set tab from URL query param if present
// Initialize tab directly from URL or default to profile
const initialTab = (searchParams.get('tab') as "profile" | "orders" | "wallet" | "addresses" | "tickets" | "pets" | "prescriptions") || "profile";
const [activeTab, setActiveTab] = React.useState<"profile" | "orders" | "wallet" | "addresses" | "tickets" | "pets" | "prescriptions" | "overview">(
validTabs.includes(initialTab) ? initialTab : "profile"
);
// Handle tab click and update browser URL without full reloads
const handleTabChange = React.useCallback((tabId: "profile" | "orders" | "wallet" | "addresses" | "tickets" | "pets" | "prescriptions") => {
setActiveTab(tabId);
const newUrl = tabId === 'profile' ? '/dashboard' : `/dashboard?tab=${tabId}`;
window.history.replaceState(null, '', newUrl);
}, []);
// Sync tab if user navigates back/forward in browser history or searchParams change
useEffect(() => {
const tabParam = searchParams.get('tab');
if (tabParam && ['profile', 'orders', 'wallet', 'addresses', 'tickets', 'pets', 'prescriptions'].includes(tabParam)) {
if (tabParam && validTabs.includes(tabParam)) {
setActiveTab(tabParam as "profile" | "orders" | "wallet" | "addresses" | "tickets" | "pets" | "prescriptions");
} else if (!tabParam) {
setActiveTab("profile");
}
}, [searchParams]);
}, [searchParams, validTabs]);
const [isLoadingOrders, setIsLoadingOrders] = useState(false);
const [prescriptions, setPrescriptions] = useState<any[]>([]);
@@ -402,7 +417,7 @@ export default function UserDashboard() {
{tabs.map(tab => (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id as any)}
onClick={() => handleTabChange(tab.id as any)}
className={`flex items-center justify-center gap-2 px-3 sm:px-4 py-2.5 sm:py-3 rounded-xl sm:rounded-2xl transition-all font-bold text-xs sm:text-sm cursor-pointer ${activeTab === tab.id ? "bg-canina-blue text-white shadow-lg shadow-canina-blue/20" : "text-medical-gray-600 hover:bg-medical-gray-100 bg-medical-gray-50"}`}
>
<div className="flex-shrink-0">{tab.icon}</div>
@@ -1214,7 +1229,7 @@ export default function UserDashboard() {
</div>
{isWalletRechargeAllowed ? (
<button
onClick={() => { setActiveTab("wallet"); setIsTopUpModalOpen(true); }}
onClick={() => { handleTabChange("wallet"); setIsTopUpModalOpen(true); }}
disabled={isTopUpModalOpen}
className="mt-5 sm:mt-8 bg-canina-blue text-white w-full py-3.5 sm:py-4 px-4 rounded-2xl font-black hover:bg-white hover:text-canina-blue transition-all shadow-lg shadow-black/20 flex items-center justify-center gap-2 text-xs sm:text-sm border border-canina-blue cursor-pointer"
>