Make better tabs components, apply to user page
This commit is contained in:
parent
e4f8c14fab
commit
43cb7114e3
|
@ -1,5 +1,6 @@
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
import { useRouter, NextRouter } from 'next/router'
|
||||||
import { ReactNode, useState } from 'react'
|
import { ReactNode, useState } from 'react'
|
||||||
import { Row } from './row'
|
import { Row } from './row'
|
||||||
import { track } from '@amplitude/analytics-browser'
|
import { track } from '@amplitude/analytics-browser'
|
||||||
|
@ -14,23 +15,23 @@ type Tab = {
|
||||||
badge?: string
|
badge?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Tabs(props: {
|
type TabProps = {
|
||||||
tabs: Tab[]
|
tabs: Tab[]
|
||||||
defaultIndex?: number
|
|
||||||
labelClassName?: string
|
labelClassName?: string
|
||||||
onClick?: (tabTitle: string, index: number) => void
|
onClick?: (tabTitle: string, index: number) => void
|
||||||
className?: string
|
className?: string
|
||||||
currentPageForAnalytics?: string
|
currentPageForAnalytics?: string
|
||||||
}) {
|
}
|
||||||
|
|
||||||
|
export function UncontrolledTabs(props: TabProps & { activeIndex: number }) {
|
||||||
const {
|
const {
|
||||||
tabs,
|
tabs,
|
||||||
defaultIndex,
|
activeIndex,
|
||||||
labelClassName,
|
labelClassName,
|
||||||
onClick,
|
onClick,
|
||||||
className,
|
className,
|
||||||
currentPageForAnalytics,
|
currentPageForAnalytics,
|
||||||
} = props
|
} = props
|
||||||
const [activeIndex, setActiveIndex] = useState(defaultIndex ?? 0)
|
|
||||||
const activeTab = tabs[activeIndex] as Tab | undefined // can be undefined in weird case
|
const activeTab = tabs[activeIndex] as Tab | undefined // can be undefined in weird case
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -51,7 +52,6 @@ export function Tabs(props: {
|
||||||
if (!tab.href) {
|
if (!tab.href) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
}
|
}
|
||||||
setActiveIndex(i)
|
|
||||||
onClick?.(tab.title, i)
|
onClick?.(tab.title, i)
|
||||||
}}
|
}}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
|
@ -80,3 +80,54 @@ export function Tabs(props: {
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ControlledTabs(props: TabProps & { defaultIndex?: number }) {
|
||||||
|
const { defaultIndex, onClick, ...rest } = props
|
||||||
|
const [activeIndex, setActiveIndex] = useState(defaultIndex ?? 0)
|
||||||
|
return (
|
||||||
|
<UncontrolledTabs
|
||||||
|
{...rest}
|
||||||
|
activeIndex={activeIndex}
|
||||||
|
onClick={(title, i) => {
|
||||||
|
setActiveIndex(i)
|
||||||
|
onClick?.(title, i)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const isTabSelected = (router: NextRouter, queryParam: string, tab: Tab) => {
|
||||||
|
const selected = router.query[queryParam]
|
||||||
|
if (typeof selected === 'string') {
|
||||||
|
return tab.title.toLowerCase() === selected
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function QueryControlledTabs(
|
||||||
|
props: TabProps & { defaultIndex?: number }
|
||||||
|
) {
|
||||||
|
const { tabs, defaultIndex, onClick, ...rest } = props
|
||||||
|
const router = useRouter()
|
||||||
|
const selectedIdx = tabs.findIndex((t) => isTabSelected(router, 'tab', t))
|
||||||
|
const activeIndex = selectedIdx !== -1 ? selectedIdx : defaultIndex ?? 0
|
||||||
|
return (
|
||||||
|
<UncontrolledTabs
|
||||||
|
{...rest}
|
||||||
|
tabs={tabs}
|
||||||
|
activeIndex={activeIndex}
|
||||||
|
onClick={(title, i) => {
|
||||||
|
router.replace(
|
||||||
|
{ query: { ...router.query, tab: title.toLowerCase() } },
|
||||||
|
undefined,
|
||||||
|
{ shallow: true }
|
||||||
|
)
|
||||||
|
onClick?.(title, i)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// legacy code that didn't know about any other kind of tabs imports this
|
||||||
|
export const Tabs = ControlledTabs
|
||||||
|
|
|
@ -22,7 +22,7 @@ import { Linkify } from './linkify'
|
||||||
import { Spacer } from './layout/spacer'
|
import { Spacer } from './layout/spacer'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { genHash } from 'common/util/random'
|
import { genHash } from 'common/util/random'
|
||||||
import { Tabs } from './layout/tabs'
|
import { QueryControlledTabs } from './layout/tabs'
|
||||||
import { UserCommentsList } from './comments-list'
|
import { UserCommentsList } from './comments-list'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
import { Comment, getUsersComments } from 'web/lib/firebase/comments'
|
import { Comment, getUsersComments } from 'web/lib/firebase/comments'
|
||||||
|
@ -64,12 +64,8 @@ export function UserLink(props: {
|
||||||
export const TAB_IDS = ['markets', 'comments', 'bets', 'groups']
|
export const TAB_IDS = ['markets', 'comments', 'bets', 'groups']
|
||||||
const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf()
|
const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf()
|
||||||
|
|
||||||
export function UserPage(props: {
|
export function UserPage(props: { user: User; currentUser?: User }) {
|
||||||
user: User
|
const { user, currentUser } = props
|
||||||
currentUser?: User
|
|
||||||
defaultTabTitle?: string | undefined
|
|
||||||
}) {
|
|
||||||
const { user, currentUser, defaultTabTitle } = props
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const isCurrentUser = user.id === currentUser?.id
|
const isCurrentUser = user.id === currentUser?.id
|
||||||
const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id)
|
const bannerUrl = user.bannerUrl ?? defaultBannerUrl(user.id)
|
||||||
|
@ -276,21 +272,9 @@ export function UserPage(props: {
|
||||||
<Spacer h={10} />
|
<Spacer h={10} />
|
||||||
|
|
||||||
{usersContracts !== 'loading' && contractsById && usersComments ? (
|
{usersContracts !== 'loading' && contractsById && usersComments ? (
|
||||||
<Tabs
|
<QueryControlledTabs
|
||||||
currentPageForAnalytics={'profile'}
|
currentPageForAnalytics={'profile'}
|
||||||
labelClassName={'pb-2 pt-1 '}
|
labelClassName={'pb-2 pt-1 '}
|
||||||
defaultIndex={
|
|
||||||
defaultTabTitle ? TAB_IDS.indexOf(defaultTabTitle) : 0
|
|
||||||
}
|
|
||||||
onClick={(tabName) => {
|
|
||||||
const tabId = tabName.toLowerCase()
|
|
||||||
const subpath = tabId === 'markets' ? '' : '?tab=' + tabId
|
|
||||||
// BUG: if you start on `/Bob/bets`, then click on Markets, use-query-and-sort-params
|
|
||||||
// rewrites the url incorrectly to `/Bob/bets` instead of `/Bob`
|
|
||||||
router.push(`/${user.username}${subpath}`, undefined, {
|
|
||||||
shallow: true,
|
|
||||||
})
|
|
||||||
}}
|
|
||||||
tabs={[
|
tabs={[
|
||||||
{
|
{
|
||||||
title: 'Markets',
|
title: 'Markets',
|
||||||
|
|
|
@ -31,9 +31,8 @@ export default function UserProfile(props: { user: User | null }) {
|
||||||
const { user } = props
|
const { user } = props
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { username, tab } = router.query as {
|
const { username } = router.query as {
|
||||||
username: string
|
username: string
|
||||||
tab?: string | undefined
|
|
||||||
}
|
}
|
||||||
const currentUser = useUser()
|
const currentUser = useUser()
|
||||||
|
|
||||||
|
@ -42,11 +41,7 @@ export default function UserProfile(props: { user: User | null }) {
|
||||||
if (user === undefined) return <div />
|
if (user === undefined) return <div />
|
||||||
|
|
||||||
return user ? (
|
return user ? (
|
||||||
<UserPage
|
<UserPage user={user} currentUser={currentUser || undefined} />
|
||||||
user={user}
|
|
||||||
currentUser={currentUser || undefined}
|
|
||||||
defaultTabTitle={tab}
|
|
||||||
/>
|
|
||||||
) : (
|
) : (
|
||||||
<Custom404 />
|
<Custom404 />
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user