"use client"; import React, { useEffect, useState, Suspense } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { useGlobalStore } from "@/shared/store/global"; import DashboardSidebar from "@/shared/components/DashboardSidebar"; import { SidebarItem, TabKey } from "@/shared/types/dashboard"; import { useSharedStore } from "@/shared/store/share"; import { formatCurrency } from "@/shared/utils/currency"; // Import dashboard content components import DashboardOverview from "@/components/dashboard/DashboardOverview"; import WalletManagement from "@/components/wallet/WalletManagement"; import UserAccountBalance from "@/components/dashboard/UserAccountBalance"; import { Home, Key, Wallet } from "lucide-react"; import { Avatar, Card, CardBody, Button, Divider, Link } from "@nextui-org/react"; import { RechargeModal } from "@/shared/components/modal/RechargeModal"; import { useTranslation } from "@/shared/lib/useTranslation"; import { ChangePasswordModal } from "@/components/common/ChangePasswordModal"; import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext"; import { RegisterType } from "@/shared/store/User"; import { EmailMode } from "@/shared/types/system"; const DashboardContent: React.FC = () => { const { t } = useTranslation(); const router = useRouter(); const searchParams = useSearchParams(); const [user_token, user] = useSharedStore((state) => [ state.user_token, state.user, ]); const { loginConfig, paymentChannels } = usePlatformConfig(); const [logOut, useGetUser] = useGlobalStore((state) => [ state.logOut, state.useGetUser, ]); const { data } = useGetUser(); // Recharge modal state const [isRechargeModalOpen, setIsRechargeModalOpen] = useState(false); const getInitialTab = (): TabKey => { const tabFromUrl = searchParams.get("tab") as TabKey; if (tabFromUrl && [TabKey.OVERVIEW, TabKey.WALLET, TabKey.USER_ACCOUNT].includes(tabFromUrl)) { return tabFromUrl; } return TabKey.OVERVIEW; }; const [activeTab, setActiveTab] = useState(getInitialTab); const [isChangePasswordModalOpen, setIsChangePasswordModalOpen] = React.useState(false); const handleChangePassword = () => { setIsChangePasswordModalOpen(true); }; const handleChangePasswordClose = () => { setIsChangePasswordModalOpen(false); }; useEffect(() => { if (!data) return; if (data.code === "1000001") { console.warn("user not found"); return; } }, [data]); // If user is not logged in, redirect to login page useEffect(() => { if (!user_token) { window.location.href = "/"; } }, [user_token]); const handleLogout = () => { // use global store logout method logOut(); window.location.href = "/"; }; // handle recharge with proper error handling and loading states const handleRecharge = async () => { setIsRechargeModalOpen(true); }; const sidebarItems: SidebarItem[] = [ { key: TabKey.OVERVIEW, icon: , label: "Dashboard", description: "Manage your integration and auth key", }, ]; const handleTabNavigate = (tab: TabKey | string) => { setActiveTab(tab as TabKey); // update url params if (tab === TabKey.OVERVIEW) { // when switch to dashboard, clear all query params, only keep base path router.push("/console", { scroll: false }); } else { // other tabs set corresponding tab params const params = new URLSearchParams(); params.set("tab", tab); router.push(`/console?${params.toString()}`, { scroll: false }); } }; return (
{/* Sidebar */} } className="w-full justify-start mb-2" onPress={handleChangePassword} > {t("Change Password")} ) } userProfilePanel={ user && (
{/* User Information Card */}

{user.user_name || "Jack Liu"}

{user.user_email || "dev@xpack.ai"}

{t("Balance: {{amount}}", { amount: "", }).replace("{{amount}}", "")} handleTabNavigate(TabKey.USER_ACCOUNT)} > {formatCurrency(user?.wallet?.balance || 0)}
{paymentChannels.length > 0 && ( )}
) } /> {/* Main Content */}
{activeTab === TabKey.OVERVIEW && } {activeTab === TabKey.WALLET && ( setIsRechargeModalOpen(true)} /> )} {activeTab === TabKey.USER_ACCOUNT && }
{/* recharge modal */} setIsRechargeModalOpen(false)} /> {/* Change Password Modal */}
); }; const DashboardMain: React.FC = () => { return ( Loading... } > ); }; export default DashboardMain;