105 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-08-24 15:09:42 +02:00
/**
2017-03-18 17:34:00 +01:00
*
* LeftMenuLink
*
*/
2016-08-24 15:09:42 +02:00
2019-04-03 19:02:19 +02:00
import React from 'react';
import { startsWith, upperFirst } from 'lodash';
import PropTypes from 'prop-types';
2017-08-17 15:39:09 +02:00
import { FormattedMessage } from 'react-intl';
2017-08-21 15:12:53 +02:00
import { Link } from 'react-router-dom';
2019-12-06 11:09:48 +01:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import en from '../../translations/en.json';
2019-09-17 15:23:29 +02:00
import Li from './Li';
2016-08-24 15:09:42 +02:00
/* eslint-disable */
2019-04-03 13:18:43 +02:00
function LeftMenuLink(props) {
const isLinkActive = startsWith(
props.location.pathname.replace('/admin', '').concat('/'),
props.destination
.replace(props.suffixUrlToReplaceForLeftMenuHighlight, '')
.concat('/')
2019-04-03 13:18:43 +02:00
);
2017-07-21 18:15:43 +02:00
2019-04-03 13:18:43 +02:00
const plugin =
props.source !== 'content-manager' && props.source !== '' ? (
2019-09-17 15:23:29 +02:00
<div className="plugin">
2019-04-03 13:18:43 +02:00
<span>{upperFirst(props.source.split('-').join(' '))}</span>
</div>
) : (
2019-04-03 13:18:43 +02:00
''
);
2019-04-03 13:18:43 +02:00
// Check if messageId exists in en locale to prevent warning messages
const content = en[props.label] ? (
<FormattedMessage
id={props.label}
2019-04-03 19:02:19 +02:00
defaultMessage="{label}"
2019-04-03 13:18:43 +02:00
values={{
label: `${props.label}`,
}}
2019-09-17 15:23:29 +02:00
className="linkLabel"
2019-04-03 13:18:43 +02:00
/>
) : (
2019-09-17 15:23:29 +02:00
<span className="linkLabel">{props.label}</span>
2019-04-03 13:18:43 +02:00
);
2019-01-10 16:11:58 +01:00
2019-04-03 13:18:43 +02:00
// Icon.
2019-12-06 11:09:48 +01:00
const icon = <FontAwesomeIcon className={`linkIcon`} icon={props.icon} />;
2019-01-10 16:11:58 +01:00
2019-04-03 13:18:43 +02:00
// Create external or internal link.
const link = props.destination.includes('http') ? (
<a
2019-09-17 15:23:29 +02:00
className={`link ${isLinkActive ? 'linkActive' : ''}`}
2019-04-03 13:18:43 +02:00
href={props.destination}
2019-04-03 19:02:19 +02:00
target="_blank"
2019-07-02 08:37:41 +02:00
rel="noopener noreferrer"
2019-04-03 13:18:43 +02:00
>
{icon}
{content}
</a>
) : (
<Link
2019-09-17 15:23:29 +02:00
className={`link ${isLinkActive ? 'linkActive' : ''}`}
2019-04-03 13:18:43 +02:00
to={{
pathname: props.destination,
search: props.source ? `?source=${props.source}` : '',
}}
>
{icon}
{content}
</Link>
);
return (
2019-09-17 15:23:29 +02:00
<Li>
2019-04-03 13:18:43 +02:00
{link}
{plugin}
2019-09-17 15:23:29 +02:00
</Li>
2019-04-03 13:18:43 +02:00
);
2016-08-24 15:09:42 +02:00
}
LeftMenuLink.propTypes = {
destination: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
2019-04-03 13:18:43 +02:00
location: PropTypes.shape({
pathname: PropTypes.string,
}).isRequired,
2019-06-11 12:26:58 +02:00
pluginSuffixUrl: PropTypes.string,
2017-11-27 17:27:16 +01:00
source: PropTypes.string,
suffixUrlToReplaceForLeftMenuHighlight: PropTypes.string,
2017-11-27 17:27:16 +01:00
};
LeftMenuLink.defaultProps = {
2019-06-11 12:26:58 +02:00
pluginSuffixUrl: '',
2017-11-27 17:27:16 +01:00
source: '',
suffixUrlToReplaceForLeftMenuHighlight: '',
2016-08-24 15:09:42 +02:00
};
2019-04-03 19:02:19 +02:00
export default LeftMenuLink;