import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import Label from './Label';
import Item from './Item';
const Group = styled.div`
margin: 0 0 2rem;
`;
function capitalizeChannels(name) {
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
}
function getTabFromRoute(route, channel) {
const match = route.match(/^.*\/(.*)\/.+$/);
if (match && match[1]) {
const currentTab = match[1];
Eif (channel.supportedTabs.includes(currentTab)) {
return currentTab;
}
}
return channel.supportedTabs[0];
}
const Channels = ({ channels, ...props }) =>
<Group>
{channels.length > 0 && <div>
<Label>Channels</Label>
{channels.map(channel =>
<Item href={`/${channel.service}/${getTabFromRoute(props.route, channel)}/${channel.id}`} {...props}>{capitalizeChannels(channel.service)}</Item>,
)}
</div>
}
</Group>;
Channels.propTypes = {
channels: PropTypes.arrayOf(
PropTypes.shape({
profileId: PropTypes.string,
service: PropTypes.string,
supportedTabs: PropTypes.arrayOf(PropTypes.string),
}),
),
route: PropTypes.string.isRequired,
};
Channels.defaultProps = {
channels: [],
};
export default Channels;
|