Skip to content

Commit 27e297d

Browse files
fullstackzachchristianalfoni
authored andcommitted
🔨 Refactor, 🧠 Overmind, refactored OpenedTabs to use Overmind (codesandbox#2769)
* refactored OpenedTabs to use Overmind * Rename index.js to index.tsx * fix types * Use named import and functional component type
1 parent 0d26ff3 commit 27e297d

File tree

6 files changed

+123
-25
lines changed

6 files changed

+123
-25
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/elements.js renamed to packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/elements.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import styled from 'styled-components';
22

3-
export const RedIcon = styled.span`
3+
export const RedIcon = styled.span<{
4+
width: number;
5+
height: number;
6+
}>`
47
color: ${props => props.theme.red};
58
width: ${props => props.width}px;
69
height: ${props => props.height}px;
710
`;
811

9-
export const SVGIcon = styled.span`
12+
export const SVGIcon = styled.span<{
13+
url: string;
14+
width: number;
15+
height: number;
16+
}>`
1017
background-image: url(${props => props.url});
1118
background-size: ${props => props.width}px;
1219
background-position: 0;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/index.js renamed to packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/index.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import React, { Component } from 'react';
2-
32
import ErrorIcon from 'react-icons/lib/md/error';
43

54
import { RedIcon, SVGIcon } from './elements';
65
import getIconURL from './GetIconURL';
76

8-
class GetIcon extends Component {
7+
type Props = {
8+
type: string;
9+
width?: number;
10+
height?: number;
11+
error?: boolean;
12+
};
13+
14+
class GetIcon extends Component<Props> {
915
state = { icon: null };
1016

1117
getIcon = async type => {
@@ -27,36 +33,29 @@ class GetIcon extends Component {
2733
}
2834

2935
render() {
30-
const { type, error, width, height } = this.props;
36+
const { error, width, height } = this.props;
3137
const { icon } = this.state;
3238

3339
if (error) {
3440
return (
35-
<RedIcon>
41+
<RedIcon width={width} height={height}>
3642
<ErrorIcon width={width} height={height} />
3743
</RedIcon>
3844
);
3945
}
40-
return <SVGIcon url={icon} type={type} width={width} height={height} />;
46+
return <SVGIcon url={icon} width={width} height={height} />;
4147
}
4248
}
4349

44-
function EntryIcon({
50+
const EntryIcon: React.FC<Props> = ({
4551
type,
4652
width = 16,
4753
height = 16,
4854
error,
49-
}: {
50-
type: string,
51-
width?: number,
52-
height?: number,
53-
error?: boolean,
54-
}) {
55-
return (
56-
<div style={{ display: 'inline-block', verticalAlign: 'middle' }}>
57-
<GetIcon type={type} error={error} width={width} height={height} />
58-
</div>
59-
);
60-
}
55+
}) => (
56+
<div style={{ display: 'inline-block', verticalAlign: 'middle' }}>
57+
<GetIcon type={type} error={error} width={width} height={height} />
58+
</div>
59+
);
6160

6261
export default EntryIcon;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/elements.js renamed to packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/elements.ts

File renamed without changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
2+
import { ModuleTab } from '@codesandbox/common/lib/types';
3+
import { useOvermind } from 'app/overmind';
4+
import EntryIcons from 'app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons';
5+
import { saveAllModules } from 'app/store/modules/editor/utils';
6+
// eslint-disable-next-line import/extensions
7+
import getType from 'app/utils/get-type.ts';
8+
import React from 'react';
9+
import CrossIcon from 'react-icons/lib/md/clear';
10+
import { EntryContainer } from '../elements';
11+
import { WorkspaceItem } from '../WorkspaceItem';
12+
import { CrossIconContainer, Dir, Title } from './elements';
13+
import SaveIcon from './SaveIcon';
14+
15+
export const OpenedTabs: React.FC = () => {
16+
const { state, actions } = useOvermind();
17+
const sandbox = state.editor.currentSandbox;
18+
const { currentModuleShortid } = state.editor;
19+
const moduleObject = {};
20+
sandbox.modules.forEach(m => {
21+
moduleObject[m.shortid] = m;
22+
});
23+
24+
const openModules = (state.editor.tabs as ModuleTab[])
25+
.map(t => moduleObject[t.moduleShortid])
26+
.filter(x => x);
27+
28+
return (
29+
<WorkspaceItem
30+
title="Open Tabs"
31+
actions={
32+
<SaveIcon
33+
disabled={state.editor.isAllModulesSynced}
34+
onClick={e => {
35+
if (e) {
36+
e.preventDefault();
37+
e.stopPropagation();
38+
}
39+
saveAllModules(state, actions);
40+
}}
41+
/>
42+
}
43+
>
44+
{openModules.map((m, i) => (
45+
<EntryContainer
46+
onClick={() => {
47+
actions.editor.moduleSelected({ id: m.id });
48+
}}
49+
active={currentModuleShortid === m.shortid}
50+
key={m.id}
51+
>
52+
<EntryIcons
53+
type={getType(m.title)}
54+
error={m.errors && m.errors.length > 0}
55+
/>
56+
<Title>{m.title}</Title>
57+
<Dir>
58+
{getModulePath(sandbox.modules, sandbox.directories, m.id)
59+
.replace('/', '')
60+
.replace(new RegExp(m.title + '$'), '')}
61+
</Dir>
62+
{currentModuleShortid !== m.shortid && (
63+
<CrossIconContainer
64+
onClick={e => {
65+
if (e) {
66+
e.preventDefault();
67+
e.stopPropagation();
68+
}
69+
70+
actions.editor.tabClosed(i);
71+
}}
72+
>
73+
<CrossIcon />
74+
</CrossIconContainer>
75+
)}
76+
</EntryContainer>
77+
))}
78+
</WorkspaceItem>
79+
);
80+
};

‎packages/app/src/app/pages/Sandbox/Editor/Workspace/WorkspaceItem/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Props = {
1515
keepState?: boolean;
1616
disabled?: boolean;
1717
defaultOpen?: boolean;
18-
actions?: React.Component<any, any>;
18+
actions?: React.ReactNode;
1919
style?: React.CSSProperties;
2020
showOverflow?: boolean;
2121
};

packages/app/src/app/pages/Sandbox/Editor/Workspace/elements.js renamed to packages/app/src/app/pages/Sandbox/Editor/Workspace/elements.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
import styled from 'styled-components';
21
import fadeIn from '@codesandbox/common/lib/utils/animation/fade-in';
2+
import styled from 'styled-components';
3+
4+
type ContainerStylesProps = {
5+
theme?: any;
6+
color?: string;
7+
alternative?: boolean;
8+
noTransition?: boolean;
9+
depth?: number;
10+
active?: boolean;
11+
editing?: boolean;
12+
nameValidationError?: string;
13+
rightColors?: string[];
14+
};
315

4-
export const getContainerStyles = props => {
16+
export const getContainerStyles = (props: ContainerStylesProps) => {
517
const { theme } = props;
618
const getSelectedColor = activeColor => {
719
// some have active as full white and should never be
@@ -94,7 +106,7 @@ export const getContainerStyles = props => {
94106
return styles;
95107
};
96108

97-
export const EntryContainer = styled.div`
109+
export const EntryContainer = styled.div<ContainerStylesProps>`
98110
${props => getContainerStyles(props)};
99111
`;
100112

@@ -152,7 +164,7 @@ export const IconArea = styled.div`
152164
${fadeIn(0)};
153165
`;
154166

155-
export const WorkspaceInputContainer = styled.div`
167+
export const WorkspaceInputContainer = styled.div<{ errorMessage?: string }>`
156168
display: inline-block;
157169
display: flex;
158170
overflow: visible;

0 commit comments

Comments
 (0)