|
| 1 | +import React from 'react'; |
| 2 | +import history from 'app/utils/history'; |
| 3 | +import { useOvermind } from 'app/overmind'; |
| 4 | +import { Route } from 'react-router-dom'; |
| 5 | +import { Query } from 'react-apollo'; |
| 6 | +import Input from '@codesandbox/common/lib/components/Input'; |
| 7 | +import { Button } from '@codesandbox/common/lib/components/Button'; |
| 8 | +import PeopleIcon from 'react-icons/lib/md/people'; |
| 9 | + |
| 10 | +// @ts-ignore |
| 11 | +import { teamOverviewUrl } from '@codesandbox/common/lib/utils/url-generator'; |
| 12 | +import DashboardIcon from '-!svg-react-loader!@codesandbox/common/lib/icons/dashboard.svg'; |
| 13 | + |
| 14 | +import { Item } from './Item'; |
| 15 | +import { SandboxesItem } from './SandboxesItem'; |
| 16 | +import { TrashItem } from './TrashItem'; |
| 17 | +import { Items, CategoryHeader, SidebarStyled, InputWrapper } from './elements'; |
| 18 | +import { TEAMS_QUERY } from '../queries'; |
| 19 | +import { TemplateItem } from './TemplateItem'; |
| 20 | + |
| 21 | +const Sidebar = () => { |
| 22 | + const { |
| 23 | + state: { dashboard: dashboardState }, |
| 24 | + actions: { dashboard: dashboardAction }, |
| 25 | + } = useOvermind(); |
| 26 | + |
| 27 | + const handleSearchFocus = () => { |
| 28 | + history.push('/dashboard/search', { from: 'sandboxSearchFocused' }); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleSearchChange: React.ChangeEventHandler<HTMLInputElement> = e => { |
| 32 | + dashboardAction.searchChanged({ search: e.target.value }); |
| 33 | + }; |
| 34 | + |
| 35 | + return ( |
| 36 | + <SidebarStyled> |
| 37 | + <InputWrapper> |
| 38 | + <Input |
| 39 | + onFocus={handleSearchFocus} |
| 40 | + block |
| 41 | + value={dashboardState.filters.search} |
| 42 | + onChange={handleSearchChange} |
| 43 | + placeholder="Search my sandboxes" |
| 44 | + /> |
| 45 | + </InputWrapper> |
| 46 | + |
| 47 | + <Route |
| 48 | + path={[ |
| 49 | + '/dashboard/sandboxes/:path*', |
| 50 | + '/dashboard/teams/:teamId/sandboxes/:path*', |
| 51 | + '/', |
| 52 | + ]} |
| 53 | + > |
| 54 | + {({ location, match }) => { |
| 55 | + const testRegex = /\/dashboard.*?sandboxes/; |
| 56 | + |
| 57 | + const path = location.pathname.replace(testRegex, ''); |
| 58 | + const currentTeamId = match ? match.params.teamId : undefined; |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + {({ store: innerStore }) => ( |
| 63 | + <> |
| 64 | + <Items style={{ marginBottom: '1rem' }}> |
| 65 | + <Item |
| 66 | + Icon={DashboardIcon} |
| 67 | + path="/dashboard/recent" |
| 68 | + name="Overview" |
| 69 | + /> |
| 70 | + |
| 71 | + <SandboxesItem |
| 72 | + selectedSandboxes={innerStore.dashboard.selectedSandboxes} |
| 73 | + currentPath={path} |
| 74 | + currentTeamId={currentTeamId} |
| 75 | + openByDefault |
| 76 | + /> |
| 77 | + |
| 78 | + <TemplateItem currentPath={path} /> |
| 79 | + |
| 80 | + <TrashItem currentPath={path} /> |
| 81 | + </Items> |
| 82 | + |
| 83 | + <Query query={TEAMS_QUERY}> |
| 84 | + {({ loading, data, error }) => { |
| 85 | + if (loading) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + if (error || !data.me) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if (!(data && data.me)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + const { teams = [] } = data.me; |
| 98 | + |
| 99 | + return teams.map(({ id, name }) => ( |
| 100 | + <div key={id}> |
| 101 | + <Items> |
| 102 | + <CategoryHeader>{name}</CategoryHeader> |
| 103 | + <Item |
| 104 | + Icon={PeopleIcon} |
| 105 | + path={teamOverviewUrl(id)} |
| 106 | + name="Team Overview" |
| 107 | + /> |
| 108 | + |
| 109 | + <SandboxesItem |
| 110 | + whatTheFuck |
| 111 | + selectedSandboxes={ |
| 112 | + dashboardState.selectedSandboxes |
| 113 | + } |
| 114 | + currentPath={path} |
| 115 | + currentTeamId={currentTeamId} |
| 116 | + teamId={id} |
| 117 | + /> |
| 118 | + |
| 119 | + <TemplateItem currentPath={path} teamId={id} /> |
| 120 | + </Items> |
| 121 | + </div> |
| 122 | + )); |
| 123 | + }} |
| 124 | + </Query> |
| 125 | + </> |
| 126 | + )} |
| 127 | + </> |
| 128 | + ); |
| 129 | + }} |
| 130 | + </Route> |
| 131 | + |
| 132 | + <div style={{ margin: '2rem', fontSize: '.875rem' }}> |
| 133 | + <Button |
| 134 | + style={{ display: 'block' }} |
| 135 | + to="/dashboard/teams/new" |
| 136 | + small |
| 137 | + block |
| 138 | + > |
| 139 | + Create Team |
| 140 | + </Button> |
| 141 | + </div> |
| 142 | + </SidebarStyled> |
| 143 | + ); |
| 144 | +}; |
| 145 | + |
| 146 | +export default Sidebar; |
0 commit comments