|
1 | 1 | import React from 'react'; |
2 | 2 | import { Link as RouterLink } from 'react-router-dom'; |
3 | | -import { Element, List, ListAction, Link } from '@codesandbox/components'; |
| 3 | +import { useOvermind } from 'app/overmind'; |
| 4 | +import { |
| 5 | + Element, |
| 6 | + List, |
| 7 | + ListAction, |
| 8 | + Link, |
| 9 | + Avatar, |
| 10 | + Text, |
| 11 | + Menu, |
| 12 | + Stack, |
| 13 | + Icon, |
| 14 | +} from '@codesandbox/components'; |
4 | 15 | import css from '@styled-system/css'; |
5 | 16 |
|
6 | | -export const Sidebar = props => ( |
7 | | - <Element |
8 | | - as="aside" |
9 | | - {...props} |
10 | | - css={css({ |
11 | | - borderRight: '1px solid', |
12 | | - borderColor: 'sideBar.border', |
13 | | - width: [0, 0, 240], |
14 | | - flexShrink: 0, |
15 | | - display: ['none', 'none', 'block'], |
16 | | - })} |
17 | | - > |
18 | | - <List> |
19 | | - <ListAction> |
20 | | - <Link to="start" as={RouterLink}> |
21 | | - Start |
22 | | - </Link> |
23 | | - </ListAction> |
24 | | - <ListAction> |
25 | | - <Link to="drafts" as={RouterLink}> |
26 | | - Drafts |
27 | | - </Link> |
28 | | - </ListAction> |
29 | | - <ListAction> |
30 | | - <Link to="recent" as={RouterLink}> |
31 | | - Recent |
32 | | - </Link> |
33 | | - </ListAction> |
34 | | - <ListAction> |
35 | | - <Link to="all" as={RouterLink}> |
36 | | - All Sandboxes |
37 | | - </Link> |
38 | | - </ListAction> |
39 | | - <ListAction> |
40 | | - <Link to="templates" as={RouterLink}> |
41 | | - Templates |
42 | | - </Link> |
43 | | - </ListAction> |
44 | | - <ListAction> |
45 | | - <Link to="deleted" as={RouterLink}> |
46 | | - Recently Deleted |
47 | | - </Link> |
48 | | - </ListAction> |
49 | | - </List> |
50 | | - </Element> |
51 | | -); |
| 17 | +const SIDEBAR_WIDTH = 240; |
| 18 | + |
| 19 | +export const Sidebar = props => { |
| 20 | + const { |
| 21 | + state: { dashboard, user }, |
| 22 | + actions, |
| 23 | + } = useOvermind(); |
| 24 | + |
| 25 | + React.useEffect(() => { |
| 26 | + actions.dashboard.getTeams(); |
| 27 | + }, [actions.dashboard, user]); |
| 28 | + |
| 29 | + let activeAccount: { username: string; avatarUrl: string } = { |
| 30 | + username: null, |
| 31 | + avatarUrl: null, |
| 32 | + }; |
| 33 | + |
| 34 | + if (dashboard.activeTeam) { |
| 35 | + const team = dashboard.teams.find(t => t.id === dashboard.activeTeam); |
| 36 | + if (team) |
| 37 | + activeAccount = { |
| 38 | + username: team.name, |
| 39 | + avatarUrl: 'https://github.com/github.png', |
| 40 | + }; |
| 41 | + } else if (user) { |
| 42 | + activeAccount = { username: user.username, avatarUrl: user.avatarUrl }; |
| 43 | + } |
| 44 | + |
| 45 | + return ( |
| 46 | + <Element |
| 47 | + as="aside" |
| 48 | + {...props} |
| 49 | + css={css({ |
| 50 | + borderRight: '1px solid', |
| 51 | + borderColor: 'sideBar.border', |
| 52 | + width: [0, 0, SIDEBAR_WIDTH], |
| 53 | + flexShrink: 0, |
| 54 | + display: ['none', 'none', 'block'], |
| 55 | + })} |
| 56 | + > |
| 57 | + <List> |
| 58 | + <ListAction gap={2} css={{ padding: 0 }}> |
| 59 | + {user && ( |
| 60 | + <Menu> |
| 61 | + <Stack |
| 62 | + as={Menu.Button} |
| 63 | + justify="space-between" |
| 64 | + align="center" |
| 65 | + css={css({ |
| 66 | + width: '100%', |
| 67 | + height: 8, |
| 68 | + })} |
| 69 | + > |
| 70 | + <Stack gap={2} align="center"> |
| 71 | + <Avatar user={activeAccount} css={css({ size: 5 })} /> |
| 72 | + <Text size={4} weight="normal"> |
| 73 | + {activeAccount.username} |
| 74 | + </Text> |
| 75 | + </Stack> |
| 76 | + <Icon name="caret" size={8} /> |
| 77 | + </Stack> |
| 78 | + <Menu.List css={{ width: SIDEBAR_WIDTH }}> |
| 79 | + <Menu.Item |
| 80 | + css={{ textAlign: 'left' }} |
| 81 | + onSelect={() => actions.dashboard.setActiveTeam({ id: null })} |
| 82 | + > |
| 83 | + <Text size={3}>{user.username} (Personal)</Text> |
| 84 | + </Menu.Item> |
| 85 | + {dashboard.teams.map(team => ( |
| 86 | + <Menu.Item |
| 87 | + as={Menu.Item} |
| 88 | + css={{ textAlign: 'left' }} |
| 89 | + onSelect={() => |
| 90 | + actions.dashboard.setActiveTeam({ id: team.id }) |
| 91 | + } |
| 92 | + > |
| 93 | + <Text size={3}>{team.name}</Text> |
| 94 | + </Menu.Item> |
| 95 | + ))} |
| 96 | + </Menu.List> |
| 97 | + </Menu> |
| 98 | + )} |
| 99 | + </ListAction> |
| 100 | + <ListAction> |
| 101 | + <Link to="start" as={RouterLink}> |
| 102 | + Start |
| 103 | + </Link> |
| 104 | + </ListAction> |
| 105 | + <ListAction> |
| 106 | + <Link to="drafts" as={RouterLink}> |
| 107 | + Drafts |
| 108 | + </Link> |
| 109 | + </ListAction> |
| 110 | + <ListAction> |
| 111 | + <Link to="recent" as={RouterLink}> |
| 112 | + Recent |
| 113 | + </Link> |
| 114 | + </ListAction> |
| 115 | + <ListAction> |
| 116 | + <Link to="all" as={RouterLink}> |
| 117 | + All Sandboxes |
| 118 | + </Link> |
| 119 | + </ListAction> |
| 120 | + <ListAction> |
| 121 | + <Link to="templates" as={RouterLink}> |
| 122 | + Templates |
| 123 | + </Link> |
| 124 | + </ListAction> |
| 125 | + <ListAction> |
| 126 | + <Link to="deleted" as={RouterLink}> |
| 127 | + Recently Deleted |
| 128 | + </Link> |
| 129 | + </ListAction> |
| 130 | + </List> |
| 131 | + </Element> |
| 132 | + ); |
| 133 | +}; |
0 commit comments