|
| 1 | +import { Dispatch, FC, SetStateAction, useEffect, useMemo, useState } from 'react' |
| 2 | +import { useLocation } from 'react-router-dom' |
| 3 | + |
| 4 | +import { UserProfile } from '~/libs/core' |
| 5 | +import { PageTitle, TabsNavbar, TabsNavItem } from '~/libs/ui' |
| 6 | + |
| 7 | +import { getHashFromTabId, getTabIdFromHash, WalletAdminTabsConfig, WalletAdminTabViews } from './config' |
| 8 | +import { PaymentsTab } from './payments' |
| 9 | +import { HomeTab } from './home' |
| 10 | +import styles from './WalletAdminTabs.module.scss' |
| 11 | + |
| 12 | +interface WalletHomeProps { |
| 13 | + profile: UserProfile |
| 14 | +} |
| 15 | + |
| 16 | +const WalletAdminTabs: FC<WalletHomeProps> = (props: WalletHomeProps) => { |
| 17 | + const { hash }: { hash: string } = useLocation() |
| 18 | + |
| 19 | + const activeTabHash: string = useMemo<string>(() => getTabIdFromHash(hash), [hash]) |
| 20 | + |
| 21 | + const [activeTab, setActiveTab]: [string, Dispatch<SetStateAction<string>>] = useState<string>(activeTabHash) |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + setActiveTab(activeTabHash) |
| 25 | + }, [activeTabHash]) |
| 26 | + |
| 27 | + function handleTabChange(tabId: string): void { |
| 28 | + setActiveTab(tabId) |
| 29 | + window.location.hash = getHashFromTabId(tabId) |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className={styles.container}> |
| 34 | + <TabsNavbar defaultActive={activeTab} onChange={handleTabChange} tabs={WalletAdminTabsConfig} /> |
| 35 | + |
| 36 | + <PageTitle> |
| 37 | + {[ |
| 38 | + WalletAdminTabsConfig.find((tab: TabsNavItem) => tab.id === activeTab)?.title, |
| 39 | + 'Wallet', |
| 40 | + 'Topcoder', |
| 41 | + ].join(' | ')} |
| 42 | + </PageTitle> |
| 43 | + |
| 44 | + {activeTab === WalletAdminTabViews.home && <HomeTab profile={props.profile} />} |
| 45 | + |
| 46 | + {activeTab === WalletAdminTabViews.payments && <PaymentsTab profile={props.profile} />} |
| 47 | + </div> |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +export default WalletAdminTabs |
0 commit comments