bigbuffet-rw/packages/pl-fe/src/layouts/profile-layout.tsx

136 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-04-29 15:59:30 -07:00
import React from 'react';
import { Helmet } from 'react-helmet-async';
2022-04-29 15:59:30 -07:00
import { FormattedMessage } from 'react-intl';
import { Redirect, useHistory } from 'react-router-dom';
import { useAccountLookup } from 'pl-fe/api/hooks/accounts/useAccountLookup';
import Column from 'pl-fe/components/ui/column';
import Layout from 'pl-fe/components/ui/layout';
import Tabs from 'pl-fe/components/ui/tabs';
import Header from 'pl-fe/features/account/components/header';
import LinkFooter from 'pl-fe/features/ui/components/link-footer';
2022-04-29 15:59:30 -07:00
import {
WhoToFollowPanel,
ProfileInfoPanel,
ProfileMediaPanel,
2022-04-29 19:05:39 -07:00
ProfileFieldsPanel,
2022-04-29 15:59:30 -07:00
SignUpPanel,
PinnedAccountsPanel,
AccountNotePanel,
} from 'pl-fe/features/ui/util/async-components';
import { useAppSelector, useFeatures, usePlFeConfig } from 'pl-fe/hooks';
import { getAcct } from 'pl-fe/utils/accounts';
2022-04-29 15:59:30 -07:00
interface IProfileLayout {
2022-04-29 15:59:30 -07:00
params?: {
username?: string;
};
children: React.ReactNode;
2022-04-29 15:59:30 -07:00
}
/** Layout to display a user's profile. */
const ProfileLayout: React.FC<IProfileLayout> = ({ params, children }) => {
2022-04-29 15:59:30 -07:00
const history = useHistory();
const username = params?.username || '';
2022-04-29 15:59:30 -07:00
const { account } = useAccountLookup(username, { withRelationship: true, withScrobble: true });
2022-04-29 15:59:30 -07:00
const me = useAppSelector(state => state.me);
const features = useFeatures();
const { displayFqn } = usePlFeConfig();
2022-04-29 15:59:30 -07:00
// Fix case of username
if (account && account.acct !== username) {
return <Redirect to={`/@${account.acct}`} />;
}
2022-04-29 15:59:30 -07:00
const tabItems = [
{
text: <FormattedMessage id='account.posts' defaultMessage='Posts' />,
to: `/@${username}`,
2022-04-29 15:59:30 -07:00
name: 'profile',
},
{
text: <FormattedMessage id='account.posts_with_replies' defaultMessage='Posts & replies' />,
to: `/@${username}/with_replies`,
2022-04-29 15:59:30 -07:00
name: 'replies',
},
{
text: <FormattedMessage id='account.media' defaultMessage='Media' />,
to: `/@${username}/media`,
2022-04-29 15:59:30 -07:00
name: 'media',
},
];
if (account) {
const ownAccount = account.id === me;
if (ownAccount || account.hide_favorites === false) {
2022-04-29 15:59:30 -07:00
tabItems.push({
text: <FormattedMessage id='navigation_bar.favourites' defaultMessage='Likes' />,
to: `/@${account.acct}/favorites`,
name: 'likes',
});
}
}
let activeItem;
const pathname = history.location.pathname.replace(`@${username}/`, '');
if (pathname.endsWith('/with_replies')) {
2022-04-29 15:59:30 -07:00
activeItem = 'replies';
} else if (pathname.endsWith('/media')) {
2022-04-29 15:59:30 -07:00
activeItem = 'media';
} else if (pathname.endsWith('/favorites')) {
2022-04-29 15:59:30 -07:00
activeItem = 'likes';
} else {
2022-04-29 15:59:30 -07:00
activeItem = 'profile';
}
const showTabs = !['/following', '/followers', '/pins'].some(path => pathname.endsWith(path));
2022-04-29 15:59:30 -07:00
return (
<>
{account?.local === false && (
<Helmet>
<meta content='noindex, noarchive' name='robots' />
</Helmet>
)}
2022-04-29 15:59:30 -07:00
<Layout.Main>
2023-04-11 05:55:17 -07:00
<Column size='lg' label={account ? `@${getAcct(account, displayFqn)}` : ''} withHeader={false}>
2022-04-29 15:59:30 -07:00
<div className='space-y-4'>
<Header key={`profile-header-${account?.id}`} account={account} />
<ProfileInfoPanel username={username} account={account} />
2022-04-29 15:59:30 -07:00
{account && showTabs && (
<Tabs key={`profile-tabs-${account.id}`} items={tabItems} activeItem={activeItem} />
2022-04-29 15:59:30 -07:00
)}
{children}
</div>
</Column>
</Layout.Main>
<Layout.Aside>
{!me && (
<SignUpPanel />
2022-04-29 15:59:30 -07:00
)}
{features.notes && account && account?.id !== me && (
<AccountNotePanel account={account} />
)}
<ProfileMediaPanel account={account} />
{(account && account.fields.length > 0) && (
<ProfileFieldsPanel account={account} />
2022-04-29 15:59:30 -07:00
)}
{(features.accountEndorsements && account && account.local) ? (
<PinnedAccountsPanel account={account} limit={5} />
) : me && features.suggestions && (
<WhoToFollowPanel limit={3} />
2022-04-29 15:59:30 -07:00
)}
<LinkFooter key='link-footer' />
</Layout.Aside>
</>
);
};
export { ProfileLayout as default };