Skip to content

Commit 51f8a61

Browse files
Refactor nav (codesandbox#3171)
* Refactor src/app/pages/Dashboard/Content/routes/PathedSandboxes/Navigation/elements to Typescript * Refactor src/app/pages/Dashboard/Content/routes/PathedSandboxes/Navigation/NavigationLink to Typescript * Refactor src/app/pages/Dashboard/Content/routes/PathedSandboxes/Navigation/index to Typescript * Refactor styled component NavigationLink to use fist/last-of-type instead of passing props Co-authored-by: null <brianchen8990@gmail.com>
1 parent 8fc41ff commit 51f8a61

File tree

5 files changed

+91
-96
lines changed

5 files changed

+91
-96
lines changed

packages/app/src/app/pages/Dashboard/Content/routes/PathedSandboxes/Navigation/NavigationLink.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import { DropTarget, ConnectDropTarget } from 'react-dnd';
3+
import {
4+
entryTarget,
5+
collectTarget,
6+
} from '../../../../Sidebar/SandboxesItem/folder-drop-target';
7+
import { NavigationLink as StyledLink } from './elements';
8+
9+
interface ICollectedProps {
10+
connectDropTarget: ConnectDropTarget;
11+
}
12+
13+
interface IOwnProps {
14+
teamId?: string;
15+
name: string;
16+
path: string;
17+
}
18+
19+
type Props = ICollectedProps & IOwnProps;
20+
21+
const Link: React.FC<Props> = ({ teamId, name, path, connectDropTarget }) =>
22+
connectDropTarget(
23+
<div>
24+
<StyledLink
25+
to={
26+
teamId
27+
? `/dashboard/teams/${teamId}/sandboxes${path}`
28+
: `/dashboard/sandboxes${path}`
29+
}
30+
>
31+
{name}
32+
</StyledLink>
33+
</div>
34+
);
35+
36+
// TODO: remove generic when entryTarget(DropTargetSpec) and collectTarget(DropTargetCollector) are typed
37+
export const NavigationLink = DropTarget<IOwnProps, ICollectedProps>(
38+
'SANDBOX',
39+
entryTarget,
40+
collectTarget
41+
)(Link);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled, { css } from 'styled-components';
1+
import styled from 'styled-components';
22
import { Link } from 'react-router-dom';
33

44
export const Container = styled.div`
@@ -23,24 +23,19 @@ export const NavigationLink = styled(Link)`
2323
margin-right: 0;
2424
}
2525
26-
${props =>
27-
props.first
28-
? css`
29-
margin-left: 0;
30-
`
31-
: css`
32-
margin-left: 0.5rem;
33-
`};
26+
margin-left: 0.5rem;
27+
&:first-of-type {
28+
margin-left: 0;
29+
}
30+
31+
&:last-of-type {
32+
color: white;
33+
}
3434
35-
${props =>
36-
props.last
37-
? css`
38-
color: white;
39-
`
40-
: css`
41-
&::after {
42-
content: '›';
43-
margin-left: 0.5rem;
44-
}
45-
`};
35+
&::after {
36+
content: '›';
37+
}
38+
&:last-of-type::after {
39+
content: none;
40+
}
4641
`;

packages/app/src/app/pages/Dashboard/Content/routes/PathedSandboxes/Navigation/index.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import { join } from 'path';
3+
4+
import { Container } from './elements';
5+
import { NavigationLink } from './NavigationLink';
6+
7+
interface INavigationProps {
8+
path: string;
9+
teamId?: string;
10+
}
11+
12+
export const Navigation: React.FC<INavigationProps> = ({ path, teamId }) => {
13+
const splittedPath = path === '/' ? [''] : path.split('/');
14+
15+
const paths: Array<{ url: string; name: string }> = splittedPath.reduce(
16+
(bases, next) => {
17+
if (next === '') {
18+
return [{ url: '/', name: teamId ? 'Team Sandboxes' : 'My Sandboxes' }];
19+
}
20+
21+
const baseUrl = bases[bases.length - 1].url;
22+
bases.push({ url: join(baseUrl, next), name: next });
23+
return bases;
24+
},
25+
[]
26+
);
27+
28+
return (
29+
<Container>
30+
{paths.map(({ name, url }) => (
31+
<NavigationLink teamId={teamId} name={name} path={url} key={url} />
32+
))}
33+
</Container>
34+
);
35+
};

0 commit comments

Comments
 (0)