From 34e7a271259b22a1a791363c5acfb0b90c202de0 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Wed, 30 Mar 2022 23:24:35 -0700 Subject: [PATCH] Standardize on a single Tabs component (#72) * Extract out Tabs component * Use tabs component on /home * Use Tabs on Fold pages too --- web/components/layout/tabs.tsx | 49 +++++++ web/pages/analytics.tsx | 40 +---- web/pages/fold/[...slugs]/index.tsx | 218 +++++++++++++--------------- web/pages/home.tsx | 83 +++++------ 4 files changed, 181 insertions(+), 209 deletions(-) create mode 100644 web/components/layout/tabs.tsx diff --git a/web/components/layout/tabs.tsx b/web/components/layout/tabs.tsx new file mode 100644 index 00000000..575a9f0b --- /dev/null +++ b/web/components/layout/tabs.tsx @@ -0,0 +1,49 @@ +import clsx from 'clsx' +import Link from 'next/link' +import { useState } from 'react' + +type Tab = { + title: string + tabIcon?: JSX.Element + content: JSX.Element + // If set, change the url to this href when the tab is selected + href?: string +} + +export function Tabs(props: { tabs: Tab[]; defaultIndex?: number }) { + const { tabs, defaultIndex } = props + const [activeIndex, setActiveIndex] = useState(defaultIndex ?? 0) + const activeTab = tabs[activeIndex] + + return ( +
+ + +
{activeTab.content}
+
+ ) +} diff --git a/web/pages/analytics.tsx b/web/pages/analytics.tsx index f7aac21b..7344c11b 100644 --- a/web/pages/analytics.tsx +++ b/web/pages/analytics.tsx @@ -9,6 +9,7 @@ import { } from '../components/analytics/charts' import { Col } from '../components/layout/col' import { Spacer } from '../components/layout/spacer' +import { Tabs } from '../components/layout/tabs' import { Page } from '../components/page' import { Title } from '../components/title' import { fromPropz, usePropz } from '../hooks/use-propz' @@ -250,45 +251,6 @@ export function CustomAnalytics(props: { ) } -type Tab = { - title: string - content: JSX.Element -} - -function Tabs(props: { tabs: Tab[]; defaultIndex: number }) { - const { tabs, defaultIndex } = props - const [activeTab, setActiveTab] = useState(tabs[defaultIndex]) - - return ( -
- - -
{activeTab.content}
-
- ) -} - export function FirebaseAnalytics() { // Edit dashboard at https://datastudio.google.com/u/0/reporting/faeaf3a4-c8da-4275-b157-98dad017d305/page/Gg3/edit diff --git a/web/pages/fold/[...slugs]/index.tsx b/web/pages/fold/[...slugs]/index.tsx index 01c4a471..093c2d85 100644 --- a/web/pages/fold/[...slugs]/index.tsx +++ b/web/pages/fold/[...slugs]/index.tsx @@ -40,6 +40,7 @@ import { useRecentBets } from '../../../hooks/use-bets' import { useRecentComments } from '../../../hooks/use-comments' import { LoadingIndicator } from '../../../components/loading-indicator' import { findActiveContracts } from '../../../components/feed/find-active-contracts' +import { Tabs } from '../../../components/layout/tabs' export const getStaticProps = fromPropz(getStaticPropz) export async function getStaticPropz(props: { params: { slugs: string[] } }) { @@ -169,6 +170,47 @@ export default function FoldPage(props: { )} + + + ) + + const activityTab = ( + + {user !== null && !fold.disallowMarketCreation && ( + + )} + {recentBets && recentComments ? ( + <> + + {activeContracts.length === 0 && ( +
+ No activity from matching markets.{' '} + {isCurator && 'Try editing to add more tags!'} +
+ )} + + ) : ( + + )} + + ) + + const leaderboardsTab = ( + - - - {(page === 'activity' || page === 'markets') && ( - - - {user !== null && !fold.disallowMarketCreation && ( - - )} - {page === 'activity' ? ( - recentBets && recentComments ? ( - <> - - {activeContracts.length === 0 && ( -
- No activity from matching markets.{' '} - {isCurator && 'Try editing to add more tags!'} -
- )} - - ) : ( - - ) - ) : ( - - )} - -
- )} - - {page === 'leaderboards' && ( - - - - )} + , + href: foldPath(fold, 'markets'), + }, + { + title: 'Leaderboards', + content: leaderboardsTab, + href: foldPath(fold, 'leaderboards'), + }, + ]} + /> ) } @@ -333,55 +310,56 @@ function FoldOverview(props: { fold: Fold; curator: User }) { ) } +function YourPerformance(props: { + traderScores: { [userId: string]: number } + creatorScores: { [userId: string]: number } + + user: User | null | undefined +}) { + const { traderScores, creatorScores, user } = props + + const yourTraderScore = user ? traderScores[user.id] : undefined + const yourCreatorScore = user ? creatorScores[user.id] : undefined + + return user ? ( + +
+ Your performance +
+
+ + + + + + + {yourCreatorScore && ( + + + + + )} + +
Trading profit{formatMoney(yourTraderScore ?? 0)}
Created market vol{formatMoney(yourCreatorScore)}
+
+ + ) : null +} + function FoldLeaderboards(props: { traderScores: { [userId: string]: number } creatorScores: { [userId: string]: number } topTraders: User[] topCreators: User[] user: User | null | undefined - yourPerformanceClassName?: string }) { - const { - traderScores, - creatorScores, - topTraders, - topCreators, - user, - yourPerformanceClassName, - } = props - - const yourTraderScore = user ? traderScores[user.id] : undefined - const yourCreatorScore = user ? creatorScores[user.id] : undefined + const { traderScores, creatorScores, topTraders, topCreators } = props const topTraderScores = topTraders.map((user) => traderScores[user.id]) const topCreatorScores = topCreators.map((user) => creatorScores[user.id]) return ( <> - {user && ( - -
- Your performance -
-
- - - - - - - {yourCreatorScore && ( - - - - - )} - -
Trading profit{formatMoney(yourTraderScore ?? 0)}
Created market vol{formatMoney(yourCreatorScore)}
-
- - )} - ('activity') - if (user === null) { Router.replace('/') return <> } + const activityContent = recentBets ? ( + + ) : ( + + ) + return ( @@ -98,51 +104,28 @@ const Home = (props: { - - -
-
setFeedMode('activity')} - > +
-
setFeedMode('explore')} - > + ), + content: activityContent, + }, + { + title: 'Explore', + tabIcon: (
-
-
- - - {feedMode === 'activity' && - (recentBets ? ( - - ) : ( - - ))} - - {feedMode === 'explore' && - (exploreContracts ? ( - - ) : ( - - ))} + ), + content: exploreContracts ? ( + + ) : ( + + ), + }, + ]} + />