|
| 1 | +/* @flow */ |
| 2 | +import React, { useEffect } from 'react'; |
| 3 | +import Helmet from 'react-helmet'; |
| 4 | +import { Route, Switch, RouteComponentProps } from 'react-router-dom'; |
| 5 | +import MaxWidth from '@codesandbox/common/lib/components/flex/MaxWidth'; |
| 6 | +import Margin from '@codesandbox/common/lib/components/spacing/Margin'; |
| 7 | +import { |
| 8 | + profileSandboxesUrl, |
| 9 | + profileLikesUrl, |
| 10 | +} from '@codesandbox/common/lib/utils/url-generator'; |
| 11 | +import { useOvermind } from 'app/overmind'; |
| 12 | +import { NotFound } from 'app/pages/common/NotFound'; |
| 13 | +import Header from './Header'; |
| 14 | +import Navigation from './Navigation'; |
| 15 | +import Showcase from './Showcase'; |
| 16 | +import Sandboxes from './Sandboxes'; |
| 17 | +import { Container, Content } from './elements'; |
| 18 | + |
| 19 | +interface IProfileProps extends RouteComponentProps<{ username: string }> {} |
| 20 | + |
| 21 | +export const Profile: React.FC<IProfileProps> = ({ match }) => { |
| 22 | + const { |
| 23 | + state: { |
| 24 | + profile: { current: user, notFound }, |
| 25 | + }, |
| 26 | + actions: { |
| 27 | + profile: { profileMounted }, |
| 28 | + }, |
| 29 | + } = useOvermind(); |
| 30 | + const { username } = match.params; |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + profileMounted({ username }); |
| 34 | + }, [profileMounted, username]); |
| 35 | + |
| 36 | + if (notFound) { |
| 37 | + return <NotFound />; |
| 38 | + } |
| 39 | + |
| 40 | + if (!user) return <div />; |
| 41 | + |
| 42 | + return ( |
| 43 | + <Container> |
| 44 | + <Helmet> |
| 45 | + <title>{user.name || user.username} - CodeSandbox</title> |
| 46 | + </Helmet> |
| 47 | + <Header user={user} /> |
| 48 | + <Content> |
| 49 | + <MaxWidth> |
| 50 | + <Navigation |
| 51 | + username={user.username} |
| 52 | + sandboxCount={user.sandboxCount} |
| 53 | + likeCount={user.givenLikeCount} |
| 54 | + /> |
| 55 | + </MaxWidth> |
| 56 | + </Content> |
| 57 | + <MaxWidth width={1024}> |
| 58 | + <Margin horizontal={2} style={{ minHeight: '60vh' }}> |
| 59 | + <Switch> |
| 60 | + <Route path={match.url} exact render={() => <Showcase />} /> |
| 61 | + <Route |
| 62 | + path={`${profileSandboxesUrl(user.username)}/:page?`} |
| 63 | + // eslint-disable-next-line |
| 64 | + children={({ match: psMatch }) => ( |
| 65 | + <Sandboxes |
| 66 | + source="currentSandboxes" |
| 67 | + page={psMatch.params.page && +psMatch.params.page} |
| 68 | + baseUrl={profileSandboxesUrl(user.username)} |
| 69 | + /> |
| 70 | + )} |
| 71 | + /> |
| 72 | + <Route |
| 73 | + path={`${profileLikesUrl(user.username)}/:page?`} |
| 74 | + // eslint-disable-next-line |
| 75 | + children={({ match: plMatch }) => ( |
| 76 | + <Sandboxes |
| 77 | + source="currentLikedSandboxes" |
| 78 | + page={plMatch.params.page && +plMatch.params.page} |
| 79 | + baseUrl={profileLikesUrl(user.username)} |
| 80 | + /> |
| 81 | + )} |
| 82 | + /> |
| 83 | + </Switch> |
| 84 | + </Margin> |
| 85 | + </MaxWidth> |
| 86 | + </Container> |
| 87 | + ); |
| 88 | +}; |
0 commit comments