2019-12-18 08:57:53 +07:00
|
|
|
import React, { useState, useCallback, useContext, useEffect, useMemo } from 'react';
|
2019-02-03 17:23:33 +07:00
|
|
|
import Add from '@material-ui/icons/Add';
|
2019-12-18 08:57:53 +07:00
|
|
|
import styled from '@emotion/styled';
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-08-25 19:34:27 +07:00
|
|
|
import { DetailContext } from '../../pages/Version';
|
2019-12-18 08:57:53 +07:00
|
|
|
import Tooltip from '../../muiComponents/Tooltip';
|
|
|
|
import Avatar from '../../muiComponents/Avatar';
|
|
|
|
import Box from '../../muiComponents/Box';
|
|
|
|
import Text from '../../muiComponents/Text';
|
|
|
|
import FloatingActionButton from '../../muiComponents/FloatingActionButton';
|
|
|
|
import { Theme } from '../../design-tokens/theme';
|
2019-10-08 03:19:18 +07:00
|
|
|
|
2019-12-18 08:57:53 +07:00
|
|
|
import getUniqueDeveloperValues from './get-unique-developer-values';
|
2019-08-12 12:06:10 +07:00
|
|
|
|
2019-12-18 08:57:53 +07:00
|
|
|
export const Fab = styled(FloatingActionButton)<{ theme?: Theme }>(props => ({
|
|
|
|
backgroundColor: props.theme && props.theme.palette.primary.main,
|
|
|
|
color: props.theme && props.theme.palette.white,
|
|
|
|
}));
|
|
|
|
|
|
|
|
export enum DeveloperType {
|
|
|
|
CONTRIBUTORS = 'contributors',
|
|
|
|
MAINTAINERS = 'maintainers',
|
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
interface Props {
|
2019-12-18 08:57:53 +07:00
|
|
|
type: DeveloperType;
|
2019-08-12 12:06:10 +07:00
|
|
|
visibleMax?: number;
|
2019-06-25 05:54:32 +07:00
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-12-18 08:57:53 +07:00
|
|
|
export const StyledText = styled(Text)<{ theme?: Theme }>(({ theme }) => ({
|
|
|
|
fontWeight: theme && theme.fontWeight.bold,
|
|
|
|
marginBottom: '10px',
|
|
|
|
textTransform: 'capitalize',
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledBox = styled(Box)({
|
|
|
|
'> *': {
|
|
|
|
margin: 5,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-08-12 12:06:10 +07:00
|
|
|
export const VISIBLE_MAX = 6;
|
|
|
|
|
2019-12-18 08:57:53 +07:00
|
|
|
const Developers: React.FC<Props> = ({ type, visibleMax = VISIBLE_MAX }) => {
|
|
|
|
const detailContext = useContext(DetailContext);
|
|
|
|
|
|
|
|
if (!detailContext) {
|
|
|
|
throw Error("The app's detail Context was not correct used");
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-12-18 08:57:53 +07:00
|
|
|
const developers = useMemo(() => getUniqueDeveloperValues(detailContext.packageMeta?.latest[type]), [
|
|
|
|
detailContext.packageMeta,
|
|
|
|
type,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const [visibleDevelopersMax, setVisibleDevelopersMax] = useState(visibleMax);
|
|
|
|
const [visibleDevelopers, setVisibleDevelopers] = useState(developers);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!developers) return;
|
|
|
|
setVisibleDevelopers(developers.slice(0, visibleDevelopersMax));
|
|
|
|
}, [developers, visibleDevelopersMax]);
|
|
|
|
|
|
|
|
const handleSetVisibleDevelopersMax = useCallback(() => {
|
|
|
|
setVisibleDevelopersMax(visibleDevelopersMax + VISIBLE_MAX);
|
|
|
|
}, [visibleDevelopersMax]);
|
|
|
|
|
|
|
|
if (!visibleDevelopers || !developers) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<StyledText variant={'subtitle1'}>{type}</StyledText>
|
|
|
|
<StyledBox display="flex" flexWrap="wrap" margin="10px 0 10px 0">
|
|
|
|
{visibleDevelopers.map(visibleDeveloper => (
|
|
|
|
<Tooltip key={visibleDeveloper.email} title={visibleDeveloper.name}>
|
|
|
|
<Avatar alt={visibleDeveloper.name} src={visibleDeveloper.avatar} />
|
|
|
|
</Tooltip>
|
|
|
|
))}
|
|
|
|
{visibleDevelopersMax < developers.length && (
|
|
|
|
<Fab onClick={handleSetVisibleDevelopersMax} size="small">
|
|
|
|
<Add />
|
|
|
|
</Fab>
|
|
|
|
)}
|
|
|
|
</StyledBox>
|
|
|
|
</>
|
|
|
|
);
|
2019-08-12 12:06:10 +07:00
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
export default Developers;
|