Skip to content

Commit a57f8e0

Browse files
🔨 Switch Dashboard to use useOvermind (codesandbox#3486)
1 parent aefd73b commit a57f8e0

File tree

3 files changed

+47
-54
lines changed

3 files changed

+47
-54
lines changed

‎packages/app/src/app/pages/Dashboard/elements.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import RightIconBase from 'react-icons/lib/md/keyboard-arrow-right';
2+
import LeftIconBase from 'react-icons/lib/md/keyboard-arrow-left';
13
import styled, { css } from 'styled-components';
24

35
export const Container = styled.div`
@@ -7,6 +9,14 @@ export const Container = styled.div`
79
color: rgba(255, 255, 255, 0.8);
810
`;
911

12+
export const LeftIcon = styled(LeftIconBase)`
13+
color: #ffffff;
14+
`;
15+
16+
export const RightIcon = styled(RightIconBase)`
17+
color: #ffffff;
18+
`;
19+
1020
export const ShowSidebarButton = styled.button`
1121
display: none;
1222
transition: opacity 200ms ease;
@@ -22,7 +32,7 @@ const paddingTop = css`
2232
padding-top: 100px;
2333
`;
2434

25-
export const Sidebar = styled.div<{ active: boolean }>`
35+
export const SidebarContainer = styled.div<{ active: boolean }>`
2636
display: flex;
2737
box-sizing: border-box;
2838
@@ -47,7 +57,7 @@ export const Sidebar = styled.div<{ active: boolean }>`
4757
}
4858
`;
4959

50-
export const Content = styled.div`
60+
export const ContentContainer = styled.div`
5161
${paddingTop};
5262
5363
box-sizing: border-box;
Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,47 @@
1-
import React, { useState, useEffect } from 'react';
2-
import { useOvermind } from 'app/overmind';
3-
import RightIcon from 'react-icons/lib/md/keyboard-arrow-right';
4-
import LeftIcon from 'react-icons/lib/md/keyboard-arrow-left';
5-
import { withRouter, Redirect } from 'react-router-dom';
6-
import { Navigation } from 'app/pages/common/Navigation';
71
import { signInPageUrl } from '@codesandbox/common/lib/utils/url-generator';
2+
import React, { useState, useEffect, FunctionComponent } from 'react';
3+
import { withRouter, Redirect, RouteComponentProps } from 'react-router-dom';
4+
85
import { client } from 'app/graphql/client';
6+
import { useOvermind } from 'app/overmind';
7+
import { Navigation } from 'app/pages/common/Navigation';
98

10-
import { Sidebar } from './Sidebar';
119
import Content from './Content';
1210
import {
1311
Container,
14-
Content as ContentContainer,
15-
Sidebar as SidebarStyled,
12+
ContentContainer,
13+
LeftIcon,
14+
RightIcon,
15+
SidebarContainer,
1616
ShowSidebarButton,
1717
} from './elements';
18+
import { Sidebar } from './Sidebar';
1819

19-
const Dashboard = props => {
20-
const [showSidebar, setShowSidebar] = useState(false);
21-
20+
type Props = RouteComponentProps;
21+
const DashboardComponent: FunctionComponent<Props> = ({ history }) => {
2222
const {
23+
actions: {
24+
dashboard: { dashboardMounted },
25+
},
2326
state: { hasLogIn },
24-
actions: { dashboard },
2527
} = useOvermind();
28+
const [showSidebar, setShowSidebar] = useState(false);
2629

2730
useEffect(() => {
28-
dashboard.dashboardMounted();
29-
return () => {
30-
// Reset store so new visits get fresh data
31-
client.resetStore();
32-
};
33-
}, [dashboard]);
31+
dashboardMounted();
3432

35-
const onRouteChange = () => {
36-
setShowSidebar(false);
37-
};
38-
39-
const toggleSidebar = () => {
40-
setShowSidebar(!showSidebar);
41-
};
42-
43-
const { history } = props;
33+
// Reset store so new visits get fresh data
34+
return () => client.resetStore();
35+
}, [dashboardMounted]);
4436

4537
history.listen(({ state }) => {
46-
if (!!state && state.from === 'sandboxSearchFocused') {
38+
if (state?.from === 'sandboxSearchFocused') {
4739
return;
4840
}
4941

50-
onRouteChange();
42+
setShowSidebar(false);
5143
});
5244

53-
const DashboardContent = (
54-
<>
55-
<SidebarStyled active={showSidebar}>
56-
<Sidebar />
57-
<ShowSidebarButton onClick={toggleSidebar}>
58-
{showSidebar ? (
59-
<LeftIcon style={{ color: 'white' }} />
60-
) : (
61-
<RightIcon style={{ color: 'white' }} />
62-
)}
63-
</ShowSidebarButton>
64-
</SidebarStyled>
65-
66-
<ContentContainer>
67-
<Content />
68-
</ContentContainer>
69-
</>
70-
);
71-
7245
if (!hasLogIn) {
7346
return <Redirect to={signInPageUrl()} />;
7447
}
@@ -78,10 +51,20 @@ const Dashboard = props => {
7851
<Navigation float searchNoInput title="Dashboard" />
7952

8053
<div style={{ display: 'flex', overflow: 'hidden' }}>
81-
{DashboardContent}
54+
<SidebarContainer active={showSidebar}>
55+
<Sidebar />
56+
57+
<ShowSidebarButton onClick={() => setShowSidebar(show => !show)}>
58+
{showSidebar ? <LeftIcon /> : <RightIcon />}
59+
</ShowSidebarButton>
60+
</SidebarContainer>
61+
62+
<ContentContainer>
63+
<Content />
64+
</ContentContainer>
8265
</div>
8366
</Container>
8467
);
8568
};
8669

87-
export default withRouter(Dashboard);
70+
export const Dashboard = withRouter(DashboardComponent);

‎packages/app/src/app/pages/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Redirect, Route, Switch, withRouter } from 'react-router-dom';
1111

1212
import { ErrorBoundary } from './common/ErrorBoundary';
1313
import { Modals } from './common/Modals';
14-
import Dashboard from './Dashboard';
14+
import { Dashboard } from './Dashboard';
1515
import { DevAuthPage } from './DevAuth';
1616
import { Container, Content } from './elements';
1717
import { NewSandbox } from './NewSandbox';

0 commit comments

Comments
 (0)