Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816import {useCallback, useMemo, useState} from 'react'import {StyleSheet, View} from 'react-native'import {type AppBskyActorDefs} from '@atproto/api'import {plural} from '@lingui/core/macro'import {Trans, useLingui} from '@lingui/react/macro'import {useNavigation, useNavigationState} from '@react-navigation/native'
import {useBrand} from '#/lib/community/BrandContext'import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher'import {useOpenComposer} from '#/lib/hooks/useOpenComposer'import {getCurrentRoute, isTab} from '#/lib/routes/helpers'import {makeProfileLink} from '#/lib/routes/links'import { type CommonNavigatorParams, type NavigationProp,} from '#/lib/routes/types'import {sanitizeDisplayName} from '#/lib/strings/display-names'import {isInvalidHandle, sanitizeHandle} from '#/lib/strings/handles'import {emitSoftReset} from '#/state/events'import {usePinnedFeedsInfos} from '#/state/queries/feed'import {useFetchHandle} from '#/state/queries/handle'import {useUnreadMessageCount} from '#/state/queries/messages/list-conversations'import {useUnreadNotifications} from '#/state/queries/notifications/unread'import {type FeedDescriptor} from '#/state/queries/post-feed'import {useProfilesQuery} from '#/state/queries/profile'import {type SessionAccount, useSession, useSessionApi} from '#/state/session'import {useLoggedOutViewControls} from '#/state/shell/logged-out'import {useSelectedFeed} from '#/state/shell/selected-feed'import {useCloseAllActiveElements} from '#/state/util'import {LoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'import {PressableWithHover} from '#/view/com/util/PressableWithHover'import {UserAvatar} from '#/view/com/util/UserAvatar'import {NavSignupCard} from '#/view/shell/NavSignupCard'import { atoms as a, tokens, useBreakpoints, useLayoutBreakpoints, useTheme, web,} from '#/alf'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import {type DialogControlProps} from '#/components/Dialog'import {ArrowBoxLeft_Stroke2_Corner0_Rounded as LeaveIcon} from '#/components/icons/ArrowBoxLeft'import { Bell_Filled_Corner0_Rounded as BellFilledIcon, Bell_Stroke2_Corner0_Rounded as BellIcon,} from '#/components/icons/Bell'import { Bookmark as BookmarkIcon, BookmarkFilled as BookmarkFilledIcon,} from '#/components/icons/Bookmark'import { BulletList_Filled_Corner0_Rounded as ListFilledIcon, BulletList_Stroke2_Corner0_Rounded as ListIcon,} from '#/components/icons/BulletList'import {type Props as SVGIconProps} from '#/components/icons/common'import {DotGrid3x1_Stroke2_Corner0_Rounded as EllipsisIcon} from '#/components/icons/DotGrid'import {EditBig_Stroke2_Corner2_Rounded as EditBigIcon} from '#/components/icons/EditBig'import { Hashtag_Filled_Corner0_Rounded as HashtagFilledIcon, Hashtag_Stroke2_Corner0_Rounded as HashtagIcon,} from '#/components/icons/Hashtag'import { Heart2_Filled_Stroke2_Corner0_Rounded as HeartFilledIcon, Heart2_Stroke2_Corner0_Rounded as HeartIcon,} from '#/components/icons/Heart2'import { HomeOpen_Filled_Corner0_Rounded as HomeFilledIcon, HomeOpen_Stoke2_Corner0_Rounded as HomeIcon,} from '#/components/icons/HomeOpen'import { MagnifyingGlass_Filled_Stroke2_Corner0_Rounded as MagnifyingGlassFilledIcon, MagnifyingGlass_Stroke2_Corner0_Rounded as MagnifyingGlassIcon,} from '#/components/icons/MagnifyingGlass'import { Message_Stroke2_Corner0_Rounded as MessageIcon, Message_Stroke2_Corner0_Rounded_Filled as MessageFilledIcon,} from '#/components/icons/Message'import {PlusLarge_Stroke2_Corner0_Rounded as PlusIcon} from '#/components/icons/Plus'import { SettingsGear2_Filled_Corner0_Rounded as SettingsFilledIcon, SettingsGear2_Stroke2_Corner0_Rounded as SettingsIcon,} from '#/components/icons/SettingsGear2'import { UserCircle_Filled_Corner0_Rounded as UserCircleFilledIcon, UserCircle_Stroke2_Corner0_Rounded as UserCircleIcon,} from '#/components/icons/UserCircle'import {CENTER_COLUMN_OFFSET, CENTER_COLUMN_WIDTH} from '#/components/Layout'import * as Menu from '#/components/Menu'import * as Prompt from '#/components/Prompt'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {type Events} from '#/analytics/metrics/types'import {useActorStatus} from '#/features/liveNow'import {router} from '#/routes'import {PlatformInfo} from '../../../../modules/expo-bluesky-swiss-army'
const LARGE_ELEMENT_SIZE = 48const NAV_ICON_WIDTH = 28
export const LEFT_NAV_STANDARD_WIDTH = 240export const LEFT_NAV_MINIMAL_WIDTH = 80const LEFT_NAV_PWI_WIDTH = 245
function ProfileCard({minimal}: {minimal: boolean}) { const {currentAccount, accounts} = useSession() const {logoutEveryAccount} = useSessionApi() const {isLoading, data} = useProfilesQuery({ handles: accounts.map(acc => acc.did), }) const profiles = data?.profiles const signOutPromptControl = Prompt.usePromptControl() const {t: l} = useLingui() const t = useTheme()
const profile = profiles?.find(p => p.did === currentAccount!.did) const otherAccounts = accounts .filter(acc => acc.did !== currentAccount!.did) .map(account => ({ account, profile: profiles?.find(p => p.did === account.did), }))
const {isActive: live} = useActorStatus(profile)
return ( <View style={[a.pb_md, !minimal && [a.w_full, a.align_start]]}> {!isLoading && profile ? ( <Menu.Root> <Menu.Trigger label={l`Switch accounts`}> {({props, state, control}) => { const active = state.hovered || state.focused || control.isOpen return ( <Button label={props.accessibilityLabel} {...props} style={[ a.w_full, a.transition_color, active ? t.atoms.bg_contrast_25 : a.transition_delay_50ms, a.rounded_full, a.justify_between, a.align_center, a.flex_row, {gap: 6}, !minimal && [a.pl_lg, a.pr_md], ]}> <View style={[ !PlatformInfo.getIsReducedMotionEnabled() && [ a.transition_transform, {transitionDuration: '250ms'}, !active && a.transition_delay_50ms, ], a.relative, a.z_10, active && { transform: [ {scale: !minimal ? 2 / 3 : 0.8}, {translateX: !minimal ? -22 : 0}, ], }, ]}> <UserAvatar avatar={profile.avatar} size={LARGE_ELEMENT_SIZE} type={profile?.associated?.labeler ? 'labeler' : 'user'} live={live} /> </View> {!minimal && ( <> <View style={[ a.flex_1, a.transition_opacity, !active && a.transition_delay_50ms, { marginLeft: tokens.space.xl * -1, opacity: active ? 1 : 0, }, ]}> <Text emoji style={[a.font_bold, a.text_sm, a.leading_snug]} numberOfLines={1}> {sanitizeDisplayName( profile.displayName || profile.handle, )} </Text> <Text style={[ a.text_xs, a.leading_snug, t.atoms.text_contrast_medium, ]} numberOfLines={1}> {sanitizeHandle(profile.handle, '@')} </Text> </View> <EllipsisIcon aria-hidden={true} style={[ t.atoms.text_contrast_medium, a.transition_opacity, {opacity: active ? 1 : 0}, ]} size="sm" /> </> )} </Button> ) }} </Menu.Trigger> <SwitchMenuItems accounts={otherAccounts} signOutPromptControl={signOutPromptControl} /> </Menu.Root> ) : ( <LoadingPlaceholder width={LARGE_ELEMENT_SIZE} height={LARGE_ELEMENT_SIZE} style={[a.rounded_full, !minimal && a.ml_lg]} /> )} <Prompt.Basic control={signOutPromptControl} title={l`Sign out?`} description={l`You will be signed out of all your accounts.`} onConfirm={() => logoutEveryAccount('Settings')} confirmButtonCta={l`Sign out`} cancelButtonCta={l`Cancel`} confirmButtonColor="negative" /> </View> )}
function SwitchMenuItems({ accounts, signOutPromptControl,}: { accounts: | { account: SessionAccount profile?: AppBskyActorDefs.ProfileViewDetailed }[] | undefined signOutPromptControl: DialogControlProps}) { const {t: l} = useLingui() const {setShowLoggedOut} = useLoggedOutViewControls() const closeEverything = useCloseAllActiveElements()
const onAddAnotherAccount = () => { setShowLoggedOut(true) closeEverything() }
return ( <Menu.Outer> {accounts && accounts.length > 0 && ( <> <Menu.Group> <Menu.LabelText> <Trans>Switch account</Trans> </Menu.LabelText> {accounts.map(other => ( <SwitchMenuItem key={other.account.did} account={other.account} profile={other.profile} /> ))} </Menu.Group> <Menu.Divider /> </> )} <SwitcherMenuProfileLink /> <Menu.Item label={l`Add another account`} onPress={onAddAnotherAccount}> <Menu.ItemIcon icon={PlusIcon} /> <Menu.ItemText> <Trans>Add another account</Trans> </Menu.ItemText> </Menu.Item> <Menu.Item label={l`Sign out`} onPress={signOutPromptControl.open}> <Menu.ItemIcon icon={LeaveIcon} /> <Menu.ItemText> <Trans>Sign out</Trans> </Menu.ItemText> </Menu.Item> </Menu.Outer> )}
function SwitcherMenuProfileLink() { const {t: l} = useLingui() const {currentAccount} = useSession() const navigation = useNavigation() const context = Menu.useMenuContext() const profileLink = currentAccount ? makeProfileLink(currentAccount) : '/' const [pathName] = useMemo(() => router.matchPath(profileLink), [profileLink]) const currentRouteInfo = useNavigationState(state => { if (!state) { return {name: 'Home'} } return getCurrentRoute(state) }) const isCurrent = useMemo(() => { if (currentRouteInfo.name === 'Profile') { return ( isTab(currentRouteInfo.name, pathName) && (currentRouteInfo.params as CommonNavigatorParams['Profile']).name === currentAccount?.handle ) } else { return isTab(currentRouteInfo.name, pathName) } }, [currentAccount?.handle, currentRouteInfo, pathName])
const onProfilePress = useCallback( (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => { if (e.ctrlKey || e.metaKey || e.altKey) { return } e.preventDefault() context.control.close() if (isCurrent) { emitSoftReset() } else { const [screen, params] = router.matchPath(profileLink) // @ts-expect-error TODO: type matchPath well enough that it can be plugged into navigation.navigate directly navigation.navigate(screen, params, {pop: true}) } }, [navigation, profileLink, isCurrent, context], ) return ( <Menu.Item label={l`Go to profile`} // @ts-expect-error The function signature differs on web -inb onPress={onProfilePress} href={profileLink}> <Menu.ItemIcon icon={UserCircleIcon} /> <Menu.ItemText> <Trans>Go to profile</Trans> </Menu.ItemText> </Menu.Item> )}
function SwitchMenuItem({ account, profile,}: { account: SessionAccount profile: AppBskyActorDefs.ProfileViewDetailed | undefined}) { const {t: l} = useLingui() const {onPressSwitchAccount, pendingDid} = useAccountSwitcher() const {isActive: live} = useActorStatus(profile)
return ( <Menu.Item disabled={!!pendingDid} style={[a.gap_sm, {minWidth: 150}]} key={account.did} label={l`Switch to ${sanitizeHandle( profile?.handle ?? account.handle, '@', )}`} onPress={() => void onPressSwitchAccount(account, 'SwitchAccount')}> <View> <UserAvatar avatar={profile?.avatar} size={20} type={profile?.associated?.labeler ? 'labeler' : 'user'} live={live} hideLiveBadge /> </View> <Menu.ItemText> {sanitizeHandle(profile?.handle ?? account.handle, '@')} </Menu.ItemText> </Menu.Item> )}
interface NavItemProps { count?: string hasNew?: boolean href: string icons: { inactive: React.ComponentType<SVGIconProps> active: React.ComponentType<SVGIconProps> } label: string minimal: boolean navItem: Events['nav:click']['item']}function NavItem({ count, hasNew, href, icons, label, minimal, navItem,}: NavItemProps) { const t = useTheme() const {t: l} = useLingui() const ax = useAnalytics() const {currentAccount} = useSession()
const [pathName] = useMemo(() => router.matchPath(href), [href]) const currentRouteInfo = useNavigationState(state => { if (!state) { return {name: 'Home'} } return getCurrentRoute(state) }) let isCurrent = currentRouteInfo.name === 'Profile' ? isTab(currentRouteInfo.name, pathName) && (currentRouteInfo.params as CommonNavigatorParams['Profile']).name === currentAccount?.handle : isTab(currentRouteInfo.name, pathName) const isRelated = currentRouteInfo.name.startsWith(pathName) const navigation = useNavigation<NavigationProp>() const onPressWrapped = useCallback( (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => { ax.metric('nav:click', {item: navItem, surface: 'leftNav'}) if (e.ctrlKey || e.metaKey || e.altKey) { return } e.preventDefault() if (isCurrent) { emitSoftReset() } else { const [screen, params] = router.matchPath(href) // @ts-expect-error TODO: type matchPath well enough that it can be plugged into navigation.navigate directly navigation.navigate(screen, params, {pop: true}) } }, [navigation, href, isCurrent, ax, navItem], )
const Icon = isCurrent || isRelated ? icons.active : icons.inactive
return ( <PressableWithHover style={[ a.flex_row, a.align_center, a.p_md, a.rounded_full, a.gap_sm, a.outline_inset_1, a.transition_color, ]} hoverStyle={t.atoms.bg_contrast_25} // @ts-expect-error the function signature differs on web -prf onPress={onPressWrapped} href={href} dataSet={{noUnderline: 1}} role="link" accessibilityLabel={label} accessibilityHint=""> <View style={[ a.align_center, a.justify_center, { width: 24, height: 24, }, a.z_10, ]}> <Icon aria-hidden={true} width={NAV_ICON_WIDTH} style={t.atoms.text} /> {typeof count === 'string' && count ? ( <View style={[ a.absolute, a.inset_0, {right: -20}, // more breathing room ]}> <Text accessibilityLabel={l`${plural(count, { one: '# unread item', other: '# unread items', })}`} accessibilityHint="" accessible={true} numberOfLines={1} style={[ a.absolute, a.text_xs, a.font_semi_bold, a.rounded_full, a.text_center, a.leading_tight, a.z_20, { top: '-10%', left: count.length === 1 ? 12 : 8, backgroundColor: t.palette.primary_500, color: t.palette.white, lineHeight: a.text_sm.fontSize, paddingHorizontal: 4, paddingVertical: 1, minWidth: 16, }, ]}> {count} </Text> </View> ) : hasNew ? ( <View style={[ a.absolute, a.rounded_full, a.z_20, { backgroundColor: t.palette.primary_500, width: 8, height: 8, right: -2, top: -4, }, ]} /> ) : null} </View> {!minimal && ( <Text style={[a.text_xl, isCurrent ? a.font_bold : a.font_normal]}> {label} </Text> )} </PressableWithHover> )}
function ComposeBtn({minimal}: {minimal: boolean}) { const {currentAccount} = useSession() const {getState} = useNavigation() const currentRoute = useNavigationState(state => state ? getCurrentRoute(state) : {name: 'Home'}, ) const selectedFeed = useSelectedFeed() const {data: pinnedFeedInfos} = usePinnedFeedsInfos() const contextualFeed = currentRoute.name === 'Home' ? (selectedFeed ?? pinnedFeedInfos?.[0]?.feedDescriptor) : currentRoute.name === 'ProfileFeed' ? (( currentRoute.params as | CommonNavigatorParams['ProfileFeed'] | undefined )?.resolvedFeed as FeedDescriptor | undefined) : undefined const {openComposer} = useOpenComposer(contextualFeed) const {t: l} = useLingui() const brand = useBrand() const {leftNavMinimal} = useLayoutBreakpoints() const [isFetchingHandle, setIsFetchingHandle] = useState(false) const fetchHandle = useFetchHandle()
const getProfileHandle = async () => { const routes = getState()?.routes const currentRoute = routes?.[routes?.length - 1]
if (currentRoute?.name === 'Profile') { let handle: string | undefined = ( currentRoute.params as CommonNavigatorParams['Profile'] ).name
if (handle.startsWith('did:')) { try { setIsFetchingHandle(true) handle = await fetchHandle(handle) } catch (e) { handle = undefined } finally { setIsFetchingHandle(false) } }
if ( !handle || handle === currentAccount?.handle || isInvalidHandle(handle) ) return undefined
return handle }
return undefined }
const onPressCompose = async () => openComposer({mention: await getProfileHandle(), logContext: 'Fab'})
return ( <View style={minimal ? [a.px_sm, a.pt_lg] : [a.flex_row, a.pl_md, a.pt_lg]}> <Button disabled={isFetchingHandle} label={l`Compose new post`} onPress={() => void onPressCompose()} size="large" color="primary" style={[ a.rounded_full, minimal && {width: LARGE_ELEMENT_SIZE, height: LARGE_ELEMENT_SIZE}, ]}> <ButtonIcon icon={EditBigIcon} size={minimal ? 'lg' : 'sm'} /> {!minimal && ( <ButtonText> {leftNavMinimal ? ( <Trans context="action">New post</Trans> ) : ( `New ${brand.messages.postButtonLabel}` )} </ButtonText> )} </Button> </View> )}
export function DesktopLeftNav({routeName}: {routeName: string}) { const {hasSession, currentAccount} = useSession() const {t: l} = useLingui() const {gtMobile} = useBreakpoints()
// splitview uses the minimal variant of the leftnav. unfortunately there's no easy // way to thread this data through because of the view hierarchy, so just check the route name const isMessagesRelatedScreen = routeName.startsWith('Messages') const {leftNavMinimal: leftNavMinimalBreakpoint, centerColumnOffset} = useLayoutBreakpoints() const numUnreadNotifications = useUnreadNotifications() const numUnreadMessages = useUnreadMessageCount()
const leftNavMinimal = isMessagesRelatedScreen || leftNavMinimalBreakpoint
if (!hasSession && !gtMobile) { return null }
return ( <View role="navigation" style={[ a.fixed, a.top_0, a.p_lg, styles.leftNav, !hasSession && !leftNavMinimal && {width: LEFT_NAV_PWI_WIDTH}, leftNavMinimal && [ {width: LEFT_NAV_MINIMAL_WIDTH}, a.h_full, a.align_center, web(a.overflow_x_hidden), ], { transform: [ { translateX: -(CENTER_COLUMN_WIDTH / 2) + (centerColumnOffset ? CENTER_COLUMN_OFFSET : 0) + (isMessagesRelatedScreen && !leftNavMinimalBreakpoint ? LEFT_NAV_MINIMAL_WIDTH - LEFT_NAV_STANDARD_WIDTH : 0), }, {translateX: '-100%'}, ...a.scrollbar_offset.transform, ], }, ]}> {hasSession ? ( <ProfileCard minimal={leftNavMinimal} /> ) : !leftNavMinimal ? ( <View style={[a.pt_xl]}> <NavSignupCard /> </View> ) : null} {hasSession && ( <> <NavItem label={l`Home`} href="/" navItem="home" minimal={leftNavMinimal} icons={{ inactive: HomeIcon, active: HomeFilledIcon, }} /> <NavItem label={l`Explore`} href="/search" navItem="search" minimal={leftNavMinimal} icons={{ inactive: MagnifyingGlassIcon, active: MagnifyingGlassFilledIcon, }} /> <NavItem label={l`Notifications`} href="/notifications" navItem="notifications" minimal={leftNavMinimal} count={numUnreadNotifications} icons={{ inactive: BellIcon, active: BellFilledIcon, }} /> <NavItem label={l`Chat`} href="/messages" navItem="chat" minimal={leftNavMinimal} count={numUnreadMessages.numUnread} hasNew={numUnreadMessages.hasNew} icons={{ inactive: MessageIcon, active: MessageFilledIcon, }} /> <NavItem label={l`Feeds`} href="/feeds" navItem="feeds" minimal={leftNavMinimal} icons={{ inactive: HashtagIcon, active: HashtagFilledIcon, }} /> <NavItem label={l`Lists`} href="/lists" navItem="lists" minimal={leftNavMinimal} icons={{ inactive: ListIcon, active: ListFilledIcon, }} /> <NavItem label={l({ message: 'Saved', context: 'link to bookmarks screen', })} href="/saved" navItem="saved" minimal={leftNavMinimal} icons={{ inactive: BookmarkIcon, active: BookmarkFilledIcon, }} /> <NavItem label={l`Profile`} href={makeProfileLink(currentAccount!)} navItem="profile" minimal={leftNavMinimal} icons={{ inactive: UserCircleIcon, active: UserCircleFilledIcon, }} /> <NavItem label={l`Settings`} href="/settings" navItem="settings" minimal={leftNavMinimal} icons={{ inactive: SettingsIcon, active: SettingsFilledIcon, }} /> <NavItem label={l`Support`} href="/support" navItem="support" minimal={leftNavMinimal} icons={{ inactive: HeartIcon, active: HeartFilledIcon, }} />
<ComposeBtn minimal={leftNavMinimal} /> </> )} </View> )}
const styles = StyleSheet.create({ leftNav: { left: '50%', width: LEFT_NAV_STANDARD_WIDTH, // @ts-expect-error web only maxHeight: '100vh', overflowY: 'auto', scrollbarWidth: 'thin', },})