nitro-react/src/components/user-profile/UserProfileView.tsx

105 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-11-17 09:48:41 +01:00
import { RelationshipStatusInfoEvent, RelationshipStatusInfoMessageParser, UserCurrentBadgesComposer, UserCurrentBadgesEvent, UserProfileEvent, UserProfileParser, UserRelationshipsComposer } from '@nitrots/nitro-renderer';
2021-08-12 08:30:02 +02:00
import { FC, useCallback, useState } from 'react';
2021-11-17 09:48:41 +01:00
import { GetSessionDataManager, GetUserProfile, LocalizeText } from '../../api';
2022-02-21 06:01:26 +01:00
import { Column, Flex, Grid } from '../../common';
import { BatchUpdates, CreateMessageHook, SendMessageHook } from '../../hooks';
2021-08-12 08:30:02 +02:00
import { NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../layout';
2022-02-21 06:01:26 +01:00
import { BadgesContainerView } from './views/BadgesContainerView';
import { FriendsContainerView } from './views/FriendsContainerView';
import { GroupsContainerView } from './views/GroupsContainerView';
import { UserContainerView } from './views/UserContainerView';
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
export const UserProfileView: FC<{}> = props =>
2021-08-12 08:30:02 +02:00
{
2022-02-21 06:01:26 +01:00
const [ userProfile, setUserProfile ] = useState<UserProfileParser>(null);
const [ userBadges, setUserBadges ] = useState<string[]>([]);
const [ userRelationships, setUserRelationships ] = useState<RelationshipStatusInfoMessageParser>(null);
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
const onClose = () =>
2021-08-12 08:30:02 +02:00
{
2022-02-21 06:01:26 +01:00
BatchUpdates(() =>
{
setUserProfile(null);
setUserBadges([]);
setUserRelationships(null);
});
}
2021-08-30 19:54:24 +02:00
const onLeaveGroup = useCallback(() =>
{
2021-11-17 09:48:41 +01:00
if(userProfile && userProfile.id === GetSessionDataManager().userId)
{
GetUserProfile(userProfile.id);
}
2021-08-30 19:54:24 +02:00
}, [ userProfile ]);
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
const onUserCurrentBadgesEvent = useCallback((event: UserCurrentBadgesEvent) =>
2021-08-12 08:30:02 +02:00
{
const parser = event.getParser();
2022-02-21 06:01:26 +01:00
if(!userProfile || (parser.userId !== userProfile.id)) return;
setUserBadges(parser.badges);
}, [ userProfile ]);
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
CreateMessageHook(UserCurrentBadgesEvent, onUserCurrentBadgesEvent);
2021-08-12 08:30:02 +02:00
2021-08-13 09:46:37 +02:00
const OnUserRelationshipsEvent = useCallback((event: RelationshipStatusInfoEvent) =>
2021-08-12 08:30:02 +02:00
{
const parser = event.getParser();
2022-02-21 06:01:26 +01:00
if(!userProfile || (parser.userId !== userProfile.id)) return;
setUserRelationships(parser);
}, [ userProfile ]);
2021-08-12 08:30:02 +02:00
2021-08-13 09:46:37 +02:00
CreateMessageHook(RelationshipStatusInfoEvent, OnUserRelationshipsEvent);
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
const onUserProfileEvent = useCallback((event: UserProfileEvent) =>
2021-08-12 08:30:02 +02:00
{
const parser = event.getParser();
2021-08-30 19:54:24 +02:00
if(userProfile)
2021-08-12 09:16:19 +02:00
{
2022-02-21 06:01:26 +01:00
BatchUpdates(() =>
{
setUserProfile(null);
setUserBadges([]);
setUserRelationships(null);
});
2021-08-12 09:16:19 +02:00
}
2021-08-12 08:30:02 +02:00
setUserProfile(parser);
2022-02-21 06:01:26 +01:00
2021-08-12 08:30:02 +02:00
SendMessageHook(new UserCurrentBadgesComposer(parser.id));
SendMessageHook(new UserRelationshipsComposer(parser.id));
2022-02-21 06:01:26 +01:00
}, [ userProfile ]);
2021-08-12 08:30:02 +02:00
2022-02-21 06:01:26 +01:00
CreateMessageHook(UserProfileEvent, onUserProfileEvent);
2021-08-12 08:30:02 +02:00
2021-08-16 08:00:31 +02:00
if(!userProfile) return null;
2021-08-12 08:30:02 +02:00
return (
2022-02-21 06:01:26 +01:00
<NitroCardView className="user-profile" simple>
<NitroCardHeaderView headerText={ LocalizeText('extendedprofile.caption') } onCloseClick={ onClose } />
<NitroCardContentView>
<Grid>
<Column size={ 7 } className="user-container">
<UserContainerView userProfile={ userProfile } />
<BadgesContainerView badges={ userBadges } />
</Column>
<Column size={ 5 }>
{
userRelationships && <FriendsContainerView relationships={userRelationships} friendsCount={userProfile.friendsCount} />
}
</Column>
</Grid>
<Flex alignItems="center" className="rooms-button-container">
<i className="icon icon-rooms" /><span className="rooms-button">{LocalizeText('extendedprofile.rooms')}</span>
</Flex>
<GroupsContainerView itsMe={ userProfile.id === GetSessionDataManager().userId } groups={ userProfile.groups } onLeaveGroup={ onLeaveGroup } />
2021-08-22 08:37:03 +02:00
</NitroCardContentView>
</NitroCardView>
2021-08-12 08:30:02 +02:00
)
}