p5.js-web-editor/client/modules/Mobile/MobileDashboardView.jsx

114 lines
3.1 KiB
React
Raw Normal View History

2020-07-24 23:43:07 +02:00
import React, { useState } from 'react';
2020-07-31 14:04:48 +02:00
import PropTypes, { string } from 'prop-types';
2020-07-24 23:43:07 +02:00
import styled from 'styled-components';
2020-07-31 14:04:48 +02:00
import { withRouter, Link } from 'react-router';
import Screen from '../../components/mobile/MobileScreen';
import Header from '../../components/mobile/Header';
import IconButton from '../../components/mobile/IconButton';
import { ExitIcon } from '../../common/icons';
2020-07-24 23:43:07 +02:00
import Footer from '../../components/mobile/Footer';
import { prop, remSize } from '../../theme';
2020-07-28 16:33:38 +02:00
import SketchList from '../IDE/components/SketchList';
import CollectionList from '../IDE/components/CollectionList';
import AssetList from '../IDE/components/AssetList';
import Content from './MobileViewContent';
import { SketchSearchbar } from '../IDE/components/Searchbar';
const EXAMPLE_USERNAME = 'p5';
2020-07-31 14:04:48 +02:00
const FooterTab = styled(Link)`
2020-07-28 16:33:38 +02:00
background: ${props => prop(props.selected ? 'backgroundColor' : 'MobilePanel.default.foreground')};
2020-07-24 23:43:07 +02:00
color: ${props => prop(`MobilePanel.default.${props.selected ? 'foreground' : 'background'}`)};
padding: ${remSize(16)};
width: 100%;
display: flex;
`;
const Subheader = styled.div`
.searchbar {
display: flex;
* {
border-radius: 0px;
}
}
.searchbar__input { width: 100%; }
`;
const FooterTabSwitcher = styled.div`
2020-07-24 23:43:07 +02:00
display: flex;
2020-07-28 16:33:38 +02:00
h3 { text-align: center; width: 100%; }
2020-07-24 23:43:07 +02:00
`;
2020-07-28 16:33:38 +02:00
const Panels = {
2020-07-31 14:04:48 +02:00
sketches: SketchList,
collections: CollectionList,
assets: AssetList
2020-07-28 16:33:38 +02:00
};
2020-07-24 23:43:07 +02:00
const renderPanel = (name, props) => (Component => (Component && <Component {...props} />))(Panels[name]);
2020-07-31 14:04:48 +02:00
const getPanel = (pathname) => {
const pathparts = pathname ? pathname.split('/') : [];
const matches = Object.keys(Panels).map(part => part.toLowerCase()).filter(part => pathparts.includes(part));
return matches && matches.length > 0 && matches[0];
};
2020-07-31 14:04:48 +02:00
const MobileDashboard = ({ params, location }) => {
2020-07-28 16:33:38 +02:00
const Tabs = Object.keys(Panels);
const { username } = params;
2020-07-31 14:04:48 +02:00
const { pathname } = location;
const isExamples = username === EXAMPLE_USERNAME;
2020-07-31 14:04:48 +02:00
const panel = getPanel(pathname);
2020-07-24 23:43:07 +02:00
return (
2020-07-31 14:04:48 +02:00
<Screen fullscreen key={pathname}>
<Header slim inverted title={isExamples ? 'Examples' : 'My Stuff'}>
2020-07-24 23:43:07 +02:00
<IconButton to="/mobile" icon={ExitIcon} aria-label="Return to ide view" />
</Header>
2020-07-28 16:33:38 +02:00
<Content slimheader>
<Subheader>
<SketchSearchbar />
</Subheader>
2020-07-31 14:04:48 +02:00
{renderPanel(panel, { username, key: pathname })}
2020-07-28 16:33:38 +02:00
</Content>
2020-07-24 23:43:07 +02:00
<Footer>
{!isExamples &&
<FooterTabSwitcher>
{Tabs.map(tab => (
<FooterTab
key={`tab-${tab}`}
2020-07-31 14:04:48 +02:00
selected={tab === panel}
to={pathname.replace(panel, tab)}
>
2020-07-31 14:04:48 +02:00
<h3>{(isExamples && tab === 'Sketches') ? 'Examples' : tab}</h3>
</FooterTab>))
}
</FooterTabSwitcher>
}
2020-07-24 23:43:07 +02:00
</Footer>
</Screen>);
};
MobileDashboard.propTypes = {
2020-07-31 14:04:48 +02:00
location: PropTypes.shape({
pathname: PropTypes.string.isRequired
}).isRequired,
params: PropTypes.shape({
username: PropTypes.string.isRequired
})
};
MobileDashboard.defaultProps = { params: {} };
export default withRouter(MobileDashboard);