Skip to content

Commit 7361ce8

Browse files
🔨 Refactor, 🧠 Overmind Hactoberfest | Refactor /app/pages/Das… (codesandbox#2893)
🔨 Refactor, 🧠 Overmind Hactoberfest | Refactor /app/pages/Dashboard/Content/routes/RecentSandboxes/index.js to use Overmind, Typescript, and Apollo hooks
2 parents 1d2ca36 + c9c3a3e commit 7361ce8

File tree

4 files changed

+69
-75
lines changed

4 files changed

+69
-75
lines changed

‎packages/app/src/app/pages/Dashboard/Content/Sandboxes/index.tsx‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ import { Filters } from './Filters';
66
import { DashboardActions } from './Actions';
77
import { ITemplate } from './types';
88

9-
interface Props {
9+
interface IContentProps {
1010
sandboxes: any[];
11-
Header: React.ComponentType;
12-
SubHeader: React.ComponentType;
13-
ExtraElement: React.ComponentType;
11+
Header: React.ComponentType | string;
12+
SubHeader?: React.ComponentType;
13+
ExtraElement: React.ComponentType<IExtraElementProps>;
1414

15-
possibleTemplates: ITemplate[];
15+
possibleTemplates?: ITemplate[];
1616
isLoading?: boolean;
1717
hideOrder?: boolean;
1818
hideFilters?: boolean;
19-
page?: number;
19+
page?: number | string;
2020
actions?: any[];
2121
}
2222

23+
interface IExtraElementProps {
24+
style: React.CSSProperties;
25+
}
26+
2327
// eslint-disable-next-line react/prefer-stateless-function
24-
export class Content extends React.Component<Props> {
28+
export class Content extends React.Component<IContentProps> {
2529
render() {
2630
const {
2731
sandboxes,
@@ -40,7 +44,7 @@ export class Content extends React.Component<Props> {
4044
<Container>
4145
<HeaderContainer>
4246
<HeaderTitle>
43-
{Header}{' '}
47+
{Header}
4448
{sandboxes && !isLoading && (
4549
<span
4650
style={{

‎packages/app/src/app/pages/Dashboard/Content/index.js‎

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

4-
import RecentSandboxes from './routes/RecentSandboxes';
4+
import { RecentSandboxes } from './routes/RecentSandboxes';
55
import PathedSandboxes from './routes/PathedSandboxes';
66
import { Templates } from './routes/Templates';
77
import DeletedSandboxes from './routes/DeletedSandboxes';

‎packages/app/src/app/pages/Dashboard/Content/routes/RecentSandboxes/index.js‎

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react';
2+
import { Helmet } from 'react-helmet';
3+
import { useQuery } from '@apollo/react-hooks';
4+
5+
import { useOvermind } from 'app/overmind';
6+
import getMostUsedTemplate from '../../../utils/get-most-used-template';
7+
import { Content as Sandboxes } from '../../Sandboxes';
8+
import CreateNewSandbox from '../../CreateNewSandbox';
9+
import { RECENT_SANDBOXES_CONTENT_QUERY } from '../../../queries';
10+
11+
export const RecentSandboxes = () => {
12+
const { state } = useOvermind();
13+
const { loading, error, data } = useQuery(RECENT_SANDBOXES_CONTENT_QUERY, {
14+
variables: {
15+
orderField: state.dashboard.orderBy.field,
16+
orderDirection: state.dashboard.orderBy.order.toUpperCase(),
17+
},
18+
});
19+
20+
if (error) {
21+
return <div>Error!</div>;
22+
}
23+
24+
const sandboxes = loading ? [] : (data && data.me && data.me.sandboxes) || [];
25+
26+
let mostUsedTemplate = null;
27+
try {
28+
mostUsedTemplate = getMostUsedTemplate(sandboxes);
29+
} catch (e) {
30+
// Not critical
31+
}
32+
33+
// We want to hide all templates
34+
// TODO: make this a query variable for graphql and move the logic to the server
35+
const noTemplateSandboxes = sandboxes.filter(s => !s.customTemplate);
36+
return (
37+
<>
38+
<Helmet>
39+
<title>Recent Sandboxes - CodeSandbox</title>
40+
</Helmet>
41+
<Sandboxes
42+
isLoading={loading}
43+
Header="Recent Sandboxes"
44+
ExtraElement={({ style }) => (
45+
<CreateNewSandbox
46+
mostUsedSandboxTemplate={mostUsedTemplate}
47+
style={style}
48+
/>
49+
)}
50+
hideFilters
51+
sandboxes={noTemplateSandboxes}
52+
page="recent"
53+
/>
54+
</>
55+
);
56+
};

0 commit comments

Comments
 (0)