2022-03-31 05:35:20 +00:00
|
|
|
import {
|
|
|
|
HomeIcon,
|
|
|
|
SearchIcon,
|
|
|
|
BookOpenIcon,
|
|
|
|
DotsHorizontalIcon,
|
2022-04-04 04:20:41 +00:00
|
|
|
CashIcon,
|
2022-04-29 23:35:56 +00:00
|
|
|
HeartIcon,
|
2022-06-22 16:35:50 +00:00
|
|
|
UserGroupIcon,
|
2022-06-17 17:38:47 +00:00
|
|
|
TrendingUpIcon,
|
2022-06-29 17:33:20 +00:00
|
|
|
ChatIcon,
|
2022-03-31 05:35:20 +00:00
|
|
|
} from '@heroicons/react/outline'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import Link from 'next/link'
|
2022-07-22 07:03:16 +00:00
|
|
|
import Router, { useRouter } from 'next/router'
|
2022-07-07 20:41:50 +00:00
|
|
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
2022-07-17 20:17:31 +00:00
|
|
|
import { firebaseLogout, User } from 'web/lib/firebase/users'
|
2022-03-31 05:35:20 +00:00
|
|
|
import { ManifoldLogo } from './manifold-logo'
|
|
|
|
import { MenuButton } from './menu'
|
2022-05-30 00:33:41 +00:00
|
|
|
import { ProfileSummary } from './profile-menu'
|
2022-06-01 13:11:25 +00:00
|
|
|
import NotificationsIcon from 'web/components/notifications-icon'
|
2022-07-11 23:40:25 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-05-30 00:33:41 +00:00
|
|
|
import { IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { CreateQuestionButton } from 'web/components/create-question-button'
|
|
|
|
import { useMemberGroups } from 'web/hooks/use-group'
|
|
|
|
import { groupPath } from 'web/lib/firebase/groups'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { trackCallback, withTracking } from 'web/lib/service/analytics'
|
2022-07-15 12:52:08 +00:00
|
|
|
import { Group, GROUP_CHAT_SLUG } from 'common/group'
|
2022-06-29 17:33:20 +00:00
|
|
|
import { Spacer } from '../layout/spacer'
|
2022-07-07 20:41:50 +00:00
|
|
|
import { useUnseenPreferredNotifications } from 'web/hooks/use-notifications'
|
2022-07-05 23:18:37 +00:00
|
|
|
import { setNotificationsAsSeen } from 'web/pages/notifications'
|
2022-07-07 20:41:50 +00:00
|
|
|
import { PrivateUser } from 'common/user'
|
2022-07-11 23:40:25 +00:00
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-03-31 05:35:20 +00:00
|
|
|
|
2022-07-22 07:03:16 +00:00
|
|
|
const logout = async () => {
|
|
|
|
// log out, and then reload the page, in case SSR wants to boot them out
|
|
|
|
// of whatever logged-in-only area of the site they might be in
|
|
|
|
await withTracking(firebaseLogout, 'sign out')()
|
|
|
|
await Router.replace(Router.asPath)
|
|
|
|
}
|
|
|
|
|
2022-06-28 16:03:14 +00:00
|
|
|
function getNavigation() {
|
2022-05-18 15:52:12 +00:00
|
|
|
return [
|
|
|
|
{ name: 'Home', href: '/home', icon: HomeIcon },
|
2022-06-01 13:11:25 +00:00
|
|
|
{
|
|
|
|
name: 'Notifications',
|
|
|
|
href: `/notifications`,
|
|
|
|
icon: NotificationsIcon,
|
|
|
|
},
|
2022-06-02 20:35:28 +00:00
|
|
|
|
2022-07-21 22:23:55 +00:00
|
|
|
{ name: 'Leaderboards', href: '/leaderboards', icon: TrendingUpIcon },
|
|
|
|
|
2022-06-14 09:08:55 +00:00
|
|
|
...(IS_PRIVATE_MANIFOLD
|
|
|
|
? []
|
|
|
|
: [{ name: 'Get M$', href: '/add-funds', icon: CashIcon }]),
|
2022-05-18 15:52:12 +00:00
|
|
|
]
|
|
|
|
}
|
2022-04-03 20:48:25 +00:00
|
|
|
|
2022-05-30 00:33:41 +00:00
|
|
|
function getMoreNavigation(user?: User | null) {
|
|
|
|
if (IS_PRIVATE_MANIFOLD) {
|
|
|
|
return [{ name: 'Leaderboards', href: '/leaderboards' }]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return [
|
2022-06-13 04:34:11 +00:00
|
|
|
{ name: 'Charity', href: '/charity' },
|
2022-06-22 22:34:42 +00:00
|
|
|
{ name: 'Blog', href: 'https://news.manifold.markets' },
|
2022-05-30 00:33:41 +00:00
|
|
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
|
|
|
{ name: 'Twitter', href: 'https://twitter.com/ManifoldMarkets' },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-07-21 19:43:10 +00:00
|
|
|
{ name: 'Referrals', href: '/referrals' },
|
2022-06-13 04:34:11 +00:00
|
|
|
{ name: 'Charity', href: '/charity' },
|
2022-07-21 05:50:28 +00:00
|
|
|
{ name: 'Send M$', href: '/links' },
|
2022-05-30 00:33:41 +00:00
|
|
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
2022-06-14 17:02:21 +00:00
|
|
|
{ name: 'About', href: 'https://docs.manifold.markets/$how-to' },
|
2022-06-22 22:34:42 +00:00
|
|
|
{
|
|
|
|
name: 'Sign out',
|
|
|
|
href: '#',
|
2022-07-22 07:03:16 +00:00
|
|
|
onClick: logout,
|
2022-06-22 22:34:42 +00:00
|
|
|
},
|
2022-05-30 00:33:41 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-04-03 20:48:25 +00:00
|
|
|
const signedOutNavigation = [
|
2022-03-31 05:35:20 +00:00
|
|
|
{ name: 'Home', href: '/home', icon: HomeIcon },
|
2022-04-04 04:20:41 +00:00
|
|
|
{ name: 'Explore', href: '/markets', icon: SearchIcon },
|
2022-06-14 17:02:21 +00:00
|
|
|
{
|
|
|
|
name: 'About',
|
|
|
|
href: 'https://docs.manifold.markets/$how-to',
|
|
|
|
icon: BookOpenIcon,
|
|
|
|
},
|
2022-03-31 05:35:20 +00:00
|
|
|
]
|
|
|
|
|
2022-04-04 04:20:41 +00:00
|
|
|
const signedOutMobileNavigation = [
|
2022-06-14 17:02:21 +00:00
|
|
|
{
|
|
|
|
name: 'About',
|
|
|
|
href: 'https://docs.manifold.markets/$how-to',
|
|
|
|
icon: BookOpenIcon,
|
|
|
|
},
|
2022-06-29 17:33:20 +00:00
|
|
|
{ name: 'Charity', href: '/charity', icon: HeartIcon },
|
|
|
|
{ name: 'Leaderboards', href: '/leaderboards', icon: TrendingUpIcon },
|
|
|
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh', icon: ChatIcon },
|
2022-04-04 04:20:41 +00:00
|
|
|
]
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
const signedInMobileNavigation = [
|
2022-07-21 22:23:55 +00:00
|
|
|
{ name: 'Leaderboards', href: '/leaderboards', icon: TrendingUpIcon },
|
2022-06-14 09:08:55 +00:00
|
|
|
...(IS_PRIVATE_MANIFOLD
|
|
|
|
? []
|
|
|
|
: [{ name: 'Get M$', href: '/add-funds', icon: CashIcon }]),
|
2022-06-29 17:33:20 +00:00
|
|
|
{
|
|
|
|
name: 'About',
|
|
|
|
href: 'https://docs.manifold.markets/$how-to',
|
|
|
|
icon: BookOpenIcon,
|
|
|
|
},
|
2022-04-04 04:20:41 +00:00
|
|
|
]
|
|
|
|
|
2022-06-28 16:18:55 +00:00
|
|
|
function getMoreMobileNav() {
|
|
|
|
return [
|
2022-07-15 05:45:50 +00:00
|
|
|
...(IS_PRIVATE_MANIFOLD
|
|
|
|
? []
|
|
|
|
: [
|
2022-07-21 19:43:10 +00:00
|
|
|
{ name: 'Referrals', href: '/referrals' },
|
2022-07-15 05:45:50 +00:00
|
|
|
{ name: 'Charity', href: '/charity' },
|
2022-07-21 05:50:28 +00:00
|
|
|
{ name: 'Send M$', href: '/links' },
|
2022-07-15 05:45:50 +00:00
|
|
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
|
|
|
]),
|
2022-06-28 16:18:55 +00:00
|
|
|
{
|
|
|
|
name: 'Sign out',
|
|
|
|
href: '#',
|
2022-07-22 07:03:16 +00:00
|
|
|
onClick: logout,
|
2022-06-28 16:18:55 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-05-18 19:45:08 +00:00
|
|
|
export type Item = {
|
2022-03-31 05:35:20 +00:00
|
|
|
name: string
|
2022-07-04 03:43:18 +00:00
|
|
|
trackingEventName?: string
|
2022-03-31 05:35:20 +00:00
|
|
|
href: string
|
2022-06-29 17:33:20 +00:00
|
|
|
icon?: React.ComponentType<{ className?: string }>
|
2022-03-31 05:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function SidebarItem(props: { item: Item; currentPage: string }) {
|
|
|
|
const { item, currentPage } = props
|
|
|
|
return (
|
|
|
|
<Link href={item.href} key={item.name}>
|
|
|
|
<a
|
2022-06-15 21:34:34 +00:00
|
|
|
onClick={trackCallback('sidebar: ' + item.name)}
|
2022-03-31 05:35:20 +00:00
|
|
|
className={clsx(
|
|
|
|
item.href == currentPage
|
|
|
|
? 'bg-gray-200 text-gray-900'
|
|
|
|
: 'text-gray-600 hover:bg-gray-100',
|
|
|
|
'group flex items-center rounded-md px-3 py-2 text-sm font-medium'
|
|
|
|
)}
|
|
|
|
aria-current={item.href == currentPage ? 'page' : undefined}
|
|
|
|
>
|
2022-06-29 17:33:20 +00:00
|
|
|
{item.icon && (
|
|
|
|
<item.icon
|
|
|
|
className={clsx(
|
|
|
|
item.href == currentPage
|
|
|
|
? 'text-gray-500'
|
|
|
|
: 'text-gray-400 group-hover:text-gray-500',
|
|
|
|
'-ml-1 mr-3 h-6 w-6 flex-shrink-0'
|
|
|
|
)}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-31 05:35:20 +00:00
|
|
|
<span className="truncate">{item.name}</span>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function SidebarButton(props: {
|
|
|
|
text: string
|
|
|
|
icon: React.ComponentType<{ className?: string }>
|
|
|
|
children?: React.ReactNode
|
|
|
|
}) {
|
|
|
|
const { text, children } = props
|
2022-03-31 05:35:20 +00:00
|
|
|
return (
|
|
|
|
<a className="group flex items-center rounded-md px-3 py-2 text-sm font-medium text-gray-600 hover:cursor-pointer hover:bg-gray-100">
|
2022-06-22 16:35:50 +00:00
|
|
|
<props.icon
|
2022-03-31 05:35:20 +00:00
|
|
|
className="-ml-1 mr-3 h-6 w-6 flex-shrink-0 text-gray-400 group-hover:text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
<span className="truncate">{text}</span>
|
|
|
|
{children}
|
2022-03-31 05:35:20 +00:00
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function MoreButton() {
|
|
|
|
return <SidebarButton text={'More'} icon={DotsHorizontalIcon} />
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:47:50 +00:00
|
|
|
export default function Sidebar(props: { className?: string }) {
|
|
|
|
const { className } = props
|
2022-03-31 05:35:20 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const currentPage = router.pathname
|
2022-07-05 23:18:37 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
const user = useUser()
|
2022-07-07 20:41:50 +00:00
|
|
|
const privateUser = usePrivateUser(user?.id)
|
2022-07-17 20:17:31 +00:00
|
|
|
// usePing(user?.id)
|
|
|
|
|
2022-06-28 16:03:14 +00:00
|
|
|
const navigationOptions = !user ? signedOutNavigation : getNavigation()
|
2022-06-22 16:35:50 +00:00
|
|
|
const mobileNavigationOptions = !user
|
|
|
|
? signedOutMobileNavigation
|
|
|
|
: signedInMobileNavigation
|
2022-07-17 20:17:31 +00:00
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
const memberItems = (
|
2022-07-15 12:52:08 +00:00
|
|
|
useMemberGroups(
|
|
|
|
user?.id,
|
|
|
|
{ withChatEnabled: true },
|
|
|
|
{ by: 'mostRecentChatActivityTime' }
|
|
|
|
) ?? []
|
2022-07-13 21:11:22 +00:00
|
|
|
).map((group: Group) => ({
|
2022-06-22 16:35:50 +00:00
|
|
|
name: group.name,
|
2022-07-15 12:52:08 +00:00
|
|
|
href: `${groupPath(group.slug)}/${GROUP_CHAT_SLUG}`,
|
2022-06-22 16:35:50 +00:00
|
|
|
}))
|
2022-05-23 21:38:31 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
return (
|
2022-05-13 23:47:50 +00:00
|
|
|
<nav aria-label="Sidebar" className={className}>
|
2022-07-11 23:40:25 +00:00
|
|
|
<ManifoldLogo className="py-6" twoLine />
|
2022-06-29 17:33:20 +00:00
|
|
|
|
|
|
|
<CreateQuestionButton user={user} />
|
|
|
|
<Spacer h={4} />
|
2022-05-18 16:06:41 +00:00
|
|
|
{user && (
|
2022-06-29 17:33:20 +00:00
|
|
|
<div className="w-full" style={{ minHeight: 80 }}>
|
2022-05-14 01:07:44 +00:00
|
|
|
<ProfileSummary user={user} />
|
2022-05-18 16:06:41 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-03-31 05:35:20 +00:00
|
|
|
|
2022-06-22 22:34:42 +00:00
|
|
|
{/* Mobile navigation */}
|
2022-04-04 04:20:41 +00:00
|
|
|
<div className="space-y-1 lg:hidden">
|
|
|
|
{mobileNavigationOptions.map((item) => (
|
2022-06-22 16:35:50 +00:00
|
|
|
<SidebarItem key={item.href} item={item} currentPage={currentPage} />
|
2022-04-04 04:20:41 +00:00
|
|
|
))}
|
|
|
|
|
|
|
|
{user && (
|
|
|
|
<MenuButton
|
2022-06-28 16:18:55 +00:00
|
|
|
menuItems={getMoreMobileNav()}
|
2022-04-04 04:20:41 +00:00
|
|
|
buttonContent={<MoreButton />}
|
|
|
|
/>
|
|
|
|
)}
|
2022-07-20 19:36:23 +00:00
|
|
|
{/* Spacer if there are any groups */}
|
|
|
|
{memberItems.length > 0 && (
|
|
|
|
<hr className="!my-4 mr-2 border-gray-300" />
|
|
|
|
)}
|
2022-07-07 20:41:50 +00:00
|
|
|
{privateUser && (
|
|
|
|
<GroupsList
|
|
|
|
currentPage={router.asPath}
|
|
|
|
memberItems={memberItems}
|
|
|
|
privateUser={privateUser}
|
|
|
|
/>
|
|
|
|
)}
|
2022-04-04 04:20:41 +00:00
|
|
|
</div>
|
|
|
|
|
2022-06-22 22:34:42 +00:00
|
|
|
{/* Desktop navigation */}
|
2022-04-04 04:20:41 +00:00
|
|
|
<div className="hidden space-y-1 lg:block">
|
2022-06-29 17:33:20 +00:00
|
|
|
{navigationOptions.map((item) => (
|
|
|
|
<SidebarItem key={item.href} item={item} currentPage={currentPage} />
|
|
|
|
))}
|
2022-03-31 05:35:20 +00:00
|
|
|
<MenuButton
|
2022-05-30 00:33:41 +00:00
|
|
|
menuItems={getMoreNavigation(user)}
|
2022-03-31 05:35:20 +00:00
|
|
|
buttonContent={<MoreButton />}
|
|
|
|
/>
|
2022-06-29 17:33:20 +00:00
|
|
|
|
2022-06-29 22:51:11 +00:00
|
|
|
{/* Spacer if there are any groups */}
|
2022-07-20 19:36:23 +00:00
|
|
|
{memberItems.length > 0 && <hr className="!my-4 border-gray-300" />}
|
2022-07-07 20:41:50 +00:00
|
|
|
{privateUser && (
|
|
|
|
<GroupsList
|
|
|
|
currentPage={router.asPath}
|
|
|
|
memberItems={memberItems}
|
|
|
|
privateUser={privateUser}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-31 05:35:20 +00:00
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|
2022-06-29 17:33:20 +00:00
|
|
|
|
2022-07-05 23:18:37 +00:00
|
|
|
function GroupsList(props: {
|
|
|
|
currentPage: string
|
|
|
|
memberItems: Item[]
|
2022-07-07 20:41:50 +00:00
|
|
|
privateUser: PrivateUser
|
2022-07-05 23:18:37 +00:00
|
|
|
}) {
|
2022-07-07 20:41:50 +00:00
|
|
|
const { currentPage, memberItems, privateUser } = props
|
|
|
|
const preferredNotifications = useUnseenPreferredNotifications(
|
|
|
|
privateUser,
|
|
|
|
{
|
|
|
|
customHref: '/group/',
|
|
|
|
},
|
|
|
|
memberItems.length > 0 ? memberItems.length : undefined
|
|
|
|
)
|
2022-07-05 23:18:37 +00:00
|
|
|
|
|
|
|
// Set notification as seen if our current page is equal to the isSeenOnHref property
|
|
|
|
useEffect(() => {
|
2022-07-18 14:18:40 +00:00
|
|
|
const currentPageWithoutQuery = currentPage.split('?')[0]
|
|
|
|
const currentPageGroupSlug = currentPageWithoutQuery.split('/')[2]
|
2022-07-05 23:18:37 +00:00
|
|
|
preferredNotifications.forEach((notification) => {
|
2022-07-15 12:52:08 +00:00
|
|
|
if (
|
|
|
|
notification.isSeenOnHref === currentPage ||
|
2022-07-18 14:18:40 +00:00
|
|
|
// Old chat style group chat notif was just /group/slug
|
|
|
|
(notification.isSeenOnHref &&
|
|
|
|
currentPageWithoutQuery.includes(notification.isSeenOnHref)) ||
|
2022-07-15 12:52:08 +00:00
|
|
|
// They're on the home page, so if they've a chat notif, they're seeing the chat
|
|
|
|
(notification.isSeenOnHref?.endsWith(GROUP_CHAT_SLUG) &&
|
2022-07-18 14:18:40 +00:00
|
|
|
currentPageWithoutQuery.endsWith(currentPageGroupSlug))
|
2022-07-15 12:52:08 +00:00
|
|
|
) {
|
2022-07-05 23:18:37 +00:00
|
|
|
setNotificationsAsSeen([notification])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [currentPage, preferredNotifications])
|
|
|
|
|
2022-07-11 23:40:25 +00:00
|
|
|
const { height } = useWindowSize()
|
|
|
|
const [containerRef, setContainerRef] = useState<HTMLDivElement | null>(null)
|
|
|
|
const remainingHeight =
|
|
|
|
(height ?? window.innerHeight) - (containerRef?.offsetTop ?? 0)
|
|
|
|
|
2022-06-29 17:33:20 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SidebarItem
|
|
|
|
item={{ name: 'Groups', href: '/groups', icon: UserGroupIcon }}
|
|
|
|
currentPage={currentPage}
|
|
|
|
/>
|
|
|
|
|
2022-07-11 23:40:25 +00:00
|
|
|
<div
|
2022-07-13 13:57:51 +00:00
|
|
|
className="flex-1 space-y-0.5 overflow-auto"
|
2022-07-11 23:40:25 +00:00
|
|
|
style={{ height: remainingHeight }}
|
|
|
|
ref={setContainerRef}
|
|
|
|
>
|
2022-06-29 17:33:20 +00:00
|
|
|
{memberItems.map((item) => (
|
|
|
|
<a
|
2022-07-01 22:37:30 +00:00
|
|
|
key={item.href}
|
2022-06-29 17:33:20 +00:00
|
|
|
href={item.href}
|
2022-07-05 23:18:37 +00:00
|
|
|
className={clsx(
|
|
|
|
'group flex items-center rounded-md px-3 py-2 text-sm font-medium text-gray-600 hover:bg-gray-100 hover:text-gray-900',
|
|
|
|
preferredNotifications.some(
|
2022-07-14 23:03:08 +00:00
|
|
|
(n) =>
|
|
|
|
!n.isSeen &&
|
|
|
|
(n.isSeenOnHref === item.href ||
|
|
|
|
n.isSeenOnHref === item.href.replace('/chat', ''))
|
2022-07-05 23:18:37 +00:00
|
|
|
) && 'font-bold'
|
|
|
|
)}
|
2022-06-29 17:33:20 +00:00
|
|
|
>
|
2022-07-05 23:18:37 +00:00
|
|
|
<span className="truncate">{item.name}</span>
|
2022-06-29 17:33:20 +00:00
|
|
|
</a>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|